자바 문자 인터페이스 기반 간이 계산대
간이 계산대 의 실현 은 어렵 지 않 고 주로 이런 몇 가지 유형 을 실현 한다.
상품 (상품 을 번호,명칭,가격 에 따라 보관)상품 센터 (상품 을 보관 하고 상품 을 추가 할 수 있 으 며 상품 정 보 를 수정 하여 상품 의 존재 여 부 를 판단 하거나 선반 이 가득 찼 거나 상품 을 인쇄 하 는 등 기능)주문 종류 Order (주문 서 를 처리 하고 주로 구 매 기능 을 실현 하 며 주문 을 하고 총 가 를 계산한다)
기능 실현
인터페이스 초기 화
상품 을 진열 하 다.
상품 정보 수정
상품
되 돌아 와 서 계산 기능 에 들 어 갑 니 다.
상품 및 수량 을 선택 하여 계산 하 다
주문 취소
주문 확인
복귀 및 종료
기능 분석
코드 전시
package com.bittech;
import java.time.LocalDate;
import java.util.Scanner;
/**
* Author:weiwei
* description:
* Creat:2019/5/5
**/
public class CheckStand {
public static Scanner scanner = new Scanner(System.in);
public static void helpInfo() {
System.out.println("============== =============");
System.out.println(" [U] [S] [A] [Q] ");
System.out.println(" U S A Q ");
System.out.println("============================================");
}
public static void quit() {
System.out.println("===========================================");
System.out.println(" ");
System.out.println("===========================================");
System.exit(0);
}
public static void usageInfo() {
System.out.println("================ ====================");
System.out.println(" [S] [A] [D] [L] [R] ");
System.out.println(" S A D L R ");
System.out.println("===========================================");
}
public static void about() {
System.out.println("================== =====================");
System.out.println(" : ");
System.out.println(" : ");
System.out.println(" :weiwei ");
System.out.println(" :v0.0.1 ");
System.out.println(" :[email protected] ");
System.out.println("==========================================");
}
public static void settingInfo() {
System.out.println("================= ==================");
System.out.println(" [S] [A] [D] [U] [R] ");
System.out.println(" S A D U R ");
System.out.println("===========================================");
}
public static void usage() {
usageInfo();
GoodsCenter.printGoods();
Order order = new Order();
while(true){
String line = scanner.nextLine();
switch(line.trim()){
case "S":{
order.printOrder();
break;
}
case "A":{
System.out.println(" [ ][ ] ( :1 2 ):");
String value = scanner.nextLine();
String[] infoArray = value.split(" ");
if(infoArray != null && (infoArray.length == 2)){
Goods goods = GoodsCenter.getGoods(Integer.parseInt(infoArray[0]));
if(goods != null){
order.add(goods,Integer.parseInt(infoArray[1]));
order.printOrder();
break;
}
}
System.out.println(" ");
break;
}
case "D":{
System.out.println(" [ ]( :1 2 ):");
String value = scanner.nextLine();
String[] infoArray = value.split(" ");
if (infoArray != null && (infoArray.length == 2)) {
Goods goods = GoodsCenter.getGoods(Integer.parseInt(infoArray[0]));
if (goods != null) {
order.cance(goods, Integer.parseInt(infoArray[1]));
order.printOrder();
break;
}
}
System.out.println(" ");
break;
}
case "L": {
GoodsCenter.printGoods();
break;
}
case "R": {
return;
}
default: {
usageInfo();
}
}
}
}
public static void setting() {
settingInfo();
if (GoodsCenter.isFull()) {
System.out.println("! , ");
}
while (true) {
String line = scanner.nextLine();
switch (line.toUpperCase()) {
case "S": {
GoodsCenter.printGoods();
break;
}
case "A": {
System.out.println(" ( :1 1.4):");
Goods goods = readGoods();
if (goods == null) {
System.out.println("! ");
break;
}
if (GoodsCenter.isFull()) {
System.out.println("! , ");
} else if (GoodsCenter.isExist(goods)) {
System.out.println("! , ");
} else {
GoodsCenter.addGoods(goods);
GoodsCenter.printGoods();
}
break;
}
case "D": {
System.out.println(" ( :1 ):");
Goods goods = readGoods();
if (goods == null) {
System.out.println(" ");
break;
}
if (GoodsCenter.isPutaway(goods)) {
GoodsCenter.soldOutGoods(goods);
GoodsCenter.printGoods();
} else {
System.out.println(" , ");
}
break;
}
case "U": {
System.out.println(" ( :1 1.4 )");
Goods goods = readGoods();
if (goods == null) {
System.out.println(" ");
break;
}
if (GoodsCenter.isPutaway(goods)) {
GoodsCenter.modifyGoods(goods);
GoodsCenter.printGoods();
} else {
System.out.println(" , ");
}
break;
}
case "R": {
return;
}
default: {
settingInfo();
}
}
}
}
public static Goods readGoods() {
String value = scanner.nextLine();
String[] infoArray = value.split(" ");
if (infoArray != null && (infoArray.length == 3 || infoArray.length == 1)) {
if (infoArray.length == 3) {
Goods goods = new Goods(Integer.parseInt(infoArray[0]), infoArray[1], Double.parseDouble(infoArray[2]));
return goods;
}
if (infoArray.length == 1) {
Goods goods = new Goods(Integer.parseInt(infoArray[0]), "", 0.0D);
return goods;
}
}
return null;
}
public static void main(String[] args) {
helpInfo();
while (true) {
String line = scanner.nextLine();
switch (line.trim().toUpperCase()) {
case "U":
usage();
helpInfo();
break;
case "S":
setting();
helpInfo();
break;
case "A":
about();
break;
case "Q":
quit();
break;
default:
helpInfo();
}
}
}
}
GoodsCenter 클래스
class GoodsCenter {
//
private static String placeholder = "--";
//
private static int maxGoods = 10;
//
private static Goods[] goodsArray;
//
static {
goodsArray = new Goods[maxGoods];
for (int i = 0; i < goodsArray.length; i++) {
goodsArray[i] = new Goods(i + 1, "--", 0.0D);
}
}
private GoodsCenter() {
}
public static int getMaxGoods() {
return maxGoods;
}
//
public static void addGoods(Goods goods) {
for (int i = 0; i < goodsArray.length; i++) {
Goods temp = goodsArray[i];
if (temp.getId() == goods.getId()) {
temp.setName(goods.getName());
temp.setPrice(goods.getPrice());
break;
}
}
}
//
public static void soldOutGoods(Goods goods) {
for (int i = 0; i < goodsArray.length; i++) {
Goods temp = goodsArray[i];
if (temp.getId() == goods.getId()) {
temp.setName(placeholder);
temp.setPrice(0.0D);
break;
}
}
}
//
public static void modifyGoods(Goods goods) {
for (int i = 0; i < goodsArray.length; i++) {
Goods temp = goodsArray[i];
if (temp.getId() == goods.getId()) {
temp.setName(goods.getName());
temp.setPrice(goods.getPrice());
break;
}
}
}
//
public static boolean isExist(Goods goods) {
for (int i = 0; i < goodsArray.length; i++) {
Goods temp = goodsArray[i];
if (temp.getId() == goods.getId() && temp.getName().equals(goods.getName())) {
return true;
}
}
return false;
}
//
public static boolean isPutaway(Goods goods) {
for (int i = 0; i < goodsArray.length; i++) {
Goods temp = goodsArray[i];
if (temp.getId() == goods.getId() && !temp.getName().equals(placeholder)) {
return true;
}
}
return false;
}
//
public static boolean isFull(){
for(int i =0;i<goodsArray.length;i++){
if(goodsArray[i].getName().equals(placeholder)){
return false;
}
}
return true;
}
public static Goods getGoods(int id){
for(int i = 0;i<goodsArray.length;i++){
Goods temp = goodsArray[i];
if(temp.getId() == id && !temp.getName().equals(placeholder)){
return goodsArray[i];
}
}
return null;
}
//
public static void printGoods(){
System.out.println("============= ================");
System.out.println("\t" + " " + "\t" +" " + "\t" + " ");
for(int i = 0;i<goodsArray.length;i++){
Goods temp = goodsArray[i];
String name = temp.getName();
if(name.equals(placeholder)){
name = name + "[ ]";
}
System.out.println("\t" + temp.getId() + "\t" + temp.getName() + "\t" + temp.getPrice());
}
System.out.println("=========================================");
}
}
Goods 류
class Goods{
//
private int id;
//
private String name;
//
private double price;
public Goods(int id,String name,double price){
this.id = id;
this.name = name;
this.price = price;
}
public int getId(){
return this.id;
}
public int getIndex(){
return this.getId()-1;
}
public String getName(){
return this.name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice(){
return this.price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString(){
return String.format("[%2d] %s %.2f",this.getId(),this.getName(),this.getPrice());
}
}
Order 클래스
class Order{
private static int orderId = 0;
private int id;
private Goods[] items;
private int[] itmesNumber;
private int currentIndex;
public Order(){
this.id = ++orderId;
this.items = new Goods[GoodsCenter.getMaxGoods()];
this.itmesNumber = new int[GoodsCenter.getMaxGoods()];
this.currentIndex = -1;
}
public void add(Goods goods,int count){
int index = goods.getIndex();
this.items[index] = goods;
this.itmesNumber[index] += count;
}
public void cance(Goods goods,int count){
int index = goods.getIndex();
int value = this.itmesNumber[index]-count;
if(value > 0){
this.itmesNumber[index] = value;
}else{
this.items[index] = null;
this.itmesNumber[index] = 0;
}
}
public int getSize(){
return this.currentIndex+1;
}
public double getTotalPrice(){
double tatalPrivce = 0;
for(int i =0;i<this.items.length;i++){
Goods goods = this.items[i];
if(goods != null){
tatalPrivce += (this.itmesNumber[goods.getIndex()] * goods.getPrice());
}
}
return tatalPrivce;
}
public int getId(){
return this.id;
}
public void printOrder(){
System.out.println("========================");
System.out.println(" " + this.getId() );
System.out.println(" " + LocalDate.now().toString());
System.out.println("========================");
System.out.println(" ");
for(int i = 0;i<this.items.length;i++){
Goods goods = this.items[i];
if(goods != null){
int count = this.itmesNumber[goods.getIndex()];
if(count <= 0){
continue;
}
System.out.println(String.format("%2d\t%s\t%d\t%.2f",goods.getId(),goods.getName(),count,goods.getPrice() ));
}
}
System.out.println("=========================");
System.out.println(String.format(" :%2f",this.getTotalPrice()));
System.out.println("=========================");
}
}
프로젝트 총화이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.