评论

收藏

[Java] Java基于字符界面的简易收银台

编程语言 编程语言 发布于:2021-09-16 12:57 | 阅读数:545 | 评论:0

这篇文章主要为大家详细介绍了Java基于字符界面的简易收银台,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
用java实现简易收银台,供大家参考,具体内容如下
简易收银台的实现并不难,主要实现这几个类:

  • 商品类 goods (将商品根据编号,名称,价格存放)
  • 商品中心类 goodscenter (存放商品,可以添加商品,下架商品,修改商品信息,判断商品是否存在或者货架已满,打印商品等功能)
  • 订单类 order (处理订单,主要实现买单功能,下单,计算总价)
功能实现
初始化界面
DSC0000.png

商品上架
DSC0001.jpg

修改商品信息
DSC0002.jpg

下架商品
DSC0003.jpg

返回并进入买单功能
DSC0004.jpg

选择商品及数量进行买单
DSC0005.jpg

取消订单
DSC0006.jpg

查看订单
DSC0007.jpg

返回并退出
DSC0008.jpg

功能分析
DSC0009.jpg

代码展示
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("      意见反馈:liusz0501@163.com     ");
    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类实现,代码量不多,简单易懂
  • 有弊端存在,就是用数组存放商品,容易出现数组越界异常,而且如果商品多的话用数组存储也是极其不方便的
  • 还有就是未使用到数据库,商品信息,订单信息的保存有很多不方便的地方,如果建立连接了数据库,这个问题就解决了
目前能力只能实现到这了,希望可以再努力一下,将数据库加入到项目中,让它的易用性再提升更多。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持CodeAE代码之家
原文链接:https://blog.csdn.net/weixin_43224539/article/details/89855873

关注下面的标签,发现更多相似文章