电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Java基于字符界面的簡易收銀臺

瀏覽:14日期:2022-08-09 17:45:51

用Java實現簡易收銀臺,供大家參考,具體內容如下

簡易收銀臺的實現并不難,主要實現這幾個類:

商品類 Goods (將商品根據編號,名稱,價格存放) 商品中心類 GoodsCenter (存放商品,可以添加商品,下架商品,修改商品信息,判斷商品是否存在或者貨架已滿,打印商品等功能) 訂單類 Order (處理訂單,主要實現買單功能,下單,計算總價) 功能實現

初始化界面

Java基于字符界面的簡易收銀臺

商品上架

Java基于字符界面的簡易收銀臺

修改商品信息

Java基于字符界面的簡易收銀臺

下架商品

Java基于字符界面的簡易收銀臺

返回并進入買單功能

Java基于字符界面的簡易收銀臺

選擇商品及數量進行買單

Java基于字符界面的簡易收銀臺

取消訂單

Java基于字符界面的簡易收銀臺

查看訂單

Java基于字符界面的簡易收銀臺

返回并退出

Java基于字符界面的簡易收銀臺

功能分析

Java基于字符界面的簡易收銀臺

代碼展示

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('%2dt%st%dt%.2f',goods.getId(),goods.getName(),count,goods.getPrice() )); }}System.out.println('=========================');System.out.println(String.format('總價:%2f',this.getTotalPrice()));System.out.println('========================='); }}項目總結 用常用String類,Scanner類實現,代碼量不多,簡單易懂 有弊端存在,就是用數組存放商品,容易出現數組越界異常,而且如果商品多的話用數組存儲也是極其不方便的 還有就是未使用到數據庫,商品信息,訂單信息的保存有很多不方便的地方,如果建立連接了數據庫,這個問題就解決了

目前能力只能實現到這了,希望可以再努力一下,將數據庫加入到項目中,讓它的易用性再提升更多。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 校车_校车价格_19座幼儿园校车_幼儿园校车_大鼻子校车 | 高低温万能试验机-复合材料万能试验机-馥勒仪器 | 方源木业官网-四川木门-全国木门专业品牌 | 东莞工作服_东莞工作服定制_工衣订做_东莞厂服 | 回收二手冲床_金丰旧冲床回收_协易冲床回收 - 大鑫机械设备 | 山东风淋室_201/304不锈钢风淋室净化设备厂家-盛之源风淋室厂家 翻斗式矿车|固定式矿车|曲轨侧卸式矿车|梭式矿车|矿车配件-山东卓力矿车生产厂家 | 南京PVC快速门厂家南京快速卷帘门_南京pvc快速门_世界500强企业国内供应商_南京美高门业 | 油漆辅料厂家_阴阳脚线_艺术漆厂家_内外墙涂料施工_乳胶漆专用防霉腻子粉_轻质粉刷石膏-魔法涂涂 | 专注氟塑料泵_衬氟泵_磁力泵_卧龙泵阀_化工泵专业品牌 - 梭川泵阀 | 航空障碍灯_高中低光强航空障碍灯_民航许可认证航空警示灯厂家-东莞市天翔航天科技有限公司 | 板框压滤机-隔膜压滤机-厢式压滤机生产厂家-禹州市君工机械设备有限公司 | 河南空气能热水器-洛阳空气能采暖-洛阳太阳能热水工程-洛阳润达高科空气能商行 | 制样机-密封锤式破碎机-粉碎机-智能马弗炉-南昌科鑫制样 | 工业硝酸钠,硝酸钠厂家-淄博「文海工贸」 | 818手游网_提供当下热门APP手游_最新手机游戏下载 | 建筑资质代办_工程施工资质办理_资质代办公司_北京众聚企服 | 废旧物资回收公司_广州废旧设备回收_报废设备物资回收-益美工厂设备回收公司 | 全自动在线分板机_铣刀式在线分板机_曲线分板机_PCB分板机-东莞市亿协自动化设备有限公司 | 油罐车_加油机_加油卷盘_加油机卷盘_罐车人孔盖_各类球阀_海底阀等车用配件厂家-湖北华特专用设备有限公司 | 餐饮小吃技术培训-火锅串串香培训「何小胖培训」_成都点石成金[官网] | 衬塑管道_衬四氟管道厂家-淄博恒固化工设备有限公司 | 炉门刀边腹板,焦化设备配件,焦化焦炉设备_沧州瑞创机械制造有限公司 | 武汉高低温试验箱_恒温恒湿试验箱厂家-武汉蓝锐环境科技有限公司 | Trimos测长机_测高仪_TESA_mahr,WYLER水平仪,PWB对刀仪-德瑞华测量技术(苏州)有限公司 | 净化车间装修_合肥厂房无尘室设计_合肥工厂洁净工程装修公司-安徽盛世和居装饰 | 北京遮阳网-防尘盖土网-盖土草坪-迷彩网-防尘网生产厂家-京兴科技 | 早报网| 北京发电车出租-发电机租赁公司-柴油发电机厂家 - 北京明旺盛安机电设备有限公司 | 铆钉机|旋铆机|东莞旋铆机厂家|鸿佰专业生产气压/油压/自动铆钉机 | 皮带机-带式输送机价格-固定式胶带机生产厂家-河南坤威机械 | 风信子发稿-专注为企业提供全球新闻稿发布服务 | 污水处理设备维修_污水处理工程改造_机械格栅_过滤设备_气浮设备_刮吸泥机_污泥浓缩罐_污水处理设备_污水处理工程-北京龙泉新禹科技有限公司 | 大流量卧式砂磨机_强力分散机_双行星双动力混合机_同心双轴搅拌机-莱州市龙跃化工机械有限公司 | 茶楼装修设计_茶馆室内设计效果图_云臻轩茶楼装饰公司 | 冷藏车厂家|冷藏车价格|小型冷藏车|散装饲料车厂家|程力专用汽车股份有限公司销售十二分公司 | 垃圾处理设备_餐厨垃圾处理设备_厨余垃圾处理设备_果蔬垃圾处理设备-深圳市三盛环保科技有限公司 | 黄石妇科医院_黄石东方女子医院_黄石东方妇产医院怎么样 | 别墅图纸超市|别墅设计图纸|农村房屋设计图|农村自建房|别墅设计图纸及效果图大全 | 数控走心机-走心机价格-双主轴走心机-宝宇百科 | 一体化污水处理设备,一体化污水设备厂家-宜兴市福源水处理设备有限公司 | 不锈钢拉手厂家|浴室门拉手厂家|江门市蓬江区金志翔五金制品有限公司 |