자바 문자 인터페이스 기반 간이 계산대

19525 단어 자바계산대
자바 로 간단 한 계산 대 를 실현 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
간이 계산대 의 실현 은 어렵 지 않 고 주로 이런 몇 가지 유형 을 실현 한다.
상품 (상품 을 번호,명칭,가격 에 따라 보관)상품 센터 (상품 을 보관 하고 상품 을 추가 할 수 있 으 며 상품 정 보 를 수정 하여 상품 의 존재 여 부 를 판단 하거나 선반 이 가득 찼 거나 상품 을 인쇄 하 는 등 기능)주문 종류 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("=========================");
    }
}
프로젝트 총화
  • 자주 사용 하 는 String 류,Scanner 류 로 이 루어 지고 코드 의 양 이 많 지 않 으 며 간단 하고 이해 하기 쉽다
  • 4.567917.단점 이 존재 합 니 다.바로 배열 로 상품 을 보관 하 는 것 입 니 다.배열 의 크로스 오 버 이상 이 나타 나 기 쉽 고 상품 이 많 으 면 배열 로 저장 하 는 것 도 매우 불편 합 니 다4.567917.그리고 데이터 베이스,상품 정보,주문 정보의 저장 에 불편 한 부분 이 많 습 니 다.데이터 베 이 스 를 연결 하면 이 문 제 는 해결 되 었 습 니 다현재 능력 은 여기까지 밖 에 실현 되 지 않 았 으 니 데이터 베 이 스 를 프로젝트 에 추가 하여 용이 성 을 더욱 향상 시 킬 수 있 기 를 바 랍 니 다.
    이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

    좋은 웹페이지 즐겨찾기