Java applet project ------ simple character-based interface of the cash register

Using Java to achieve a simple cash register

Easy checkout is not difficult to achieve, the main achievement of these categories:

  • Commodity Goods  (merchandise according to store number, name, price)
  • Product Center class GoodsCenter  (storage goods, goods may be added, shelf products, modified product information, determines whether or not the presence or shelves full of goods, commodities printing function)
  • Order class of the Order  (order processing, to realize the main functions of pay, single, calculate the total price)

Function to achieve

                                           Initialization interface                                                                                              

                                                  

                                             Product was added to  

                                                    

                             Modify product information

                                                    

                          Of Stock

                                                   

                           Return and pay into the function

                                                   

                   Select a product and quantity were to pay

                                                      

                           cancel order

                                                     

                             check order

                                                       

                              Return and exit

                                                 

 Functional Analysis

  

 Module division

                            

 Code shows:

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

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 category

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

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("=========================");
    }
}

Project Summary

  • By conventional String class, class implements Scanner, code volume, straightforward
  • There are drawbacks, it is to use an array to store merchandise, prone to an array bounds exception, and if more goods, then use an array of storage is extremely inconvenient
  • There is not used to a database, product information, order information save a lot of places inconvenient, if a database connection is established, this problem will be solved

Currently only the ability to achieve this, and hope more effort, will be added to the database project, it's ease of use and then promote more

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_43224539/article/details/89855873