最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java基于字符界面的簡(jiǎn)易收銀臺(tái)

 更新時(shí)間:2021年06月25日 17:26:13   作者:#define微光  
這篇文章主要為大家詳細(xì)介紹了Java基于字符界面的簡(jiǎn)易收銀臺(tái),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

用Java實(shí)現(xiàn)簡(jiǎn)易收銀臺(tái),供大家參考,具體內(nèi)容如下

簡(jiǎn)易收銀臺(tái)的實(shí)現(xiàn)并不難,主要實(shí)現(xiàn)這幾個(gè)類(lèi):

  • 商品類(lèi) Goods (將商品根據(jù)編號(hào),名稱(chēng),價(jià)格存放)
  • 商品中心類(lèi) GoodsCenter (存放商品,可以添加商品,下架商品,修改商品信息,判斷商品是否存在或者貨架已滿(mǎn),打印商品等功能)
  • 訂單類(lèi) Order (處理訂單,主要實(shí)現(xiàn)買(mǎi)單功能,下單,計(jì)算總價(jià))

功能實(shí)現(xiàn)

初始化界面

商品上架  

修改商品信息

下架商品

 

返回并進(jìn)入買(mǎi)單功能

 選擇商品及數(shù)量進(jìn)行買(mǎi)單

取消訂單

 

查看訂單

 

返回并退出

功能分析

代碼展示

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("==============歡迎使用簡(jiǎn)易收銀臺(tái)=============");
        System.out.println("    [U]使用   [S]設(shè)置  [A]關(guān)于  [Q]退出      ");
        System.out.println("          輸入 U  S  A  Q  進(jìn)行操作          ");
        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("================買(mǎi)單功能====================");
        System.out.println(" [S]查看  [A]下單  [D]取消  [L]瀏覽   [R]返回");
        System.out.println("      輸入 S   A   D   L   R   進(jìn)行操作     ");
        System.out.println("===========================================");
 
    }
 
    public static void about() {
        System.out.println("==================關(guān)于=====================");
        System.out.println("          名稱(chēng):簡(jiǎn)易收銀臺(tái)                   ");
        System.out.println("          功能:基于字符界面的收銀臺(tái)操作       ");
        System.out.println("          作者:weiwei                      ");
        System.out.println("          版本:v0.0.1                      ");
        System.out.println("          意見(jiàn)反饋:liusz0501@163.com       ");
        System.out.println("==========================================");
 
 
    }
 
    public static void settingInfo() {
        System.out.println("=================設(shè)置功能==================");
        System.out.println(" [S]查看  [A]上架  [D]下架  [U]修改  [R]返回 ");
        System.out.println("     輸入  S    A   D  U   R 進(jìn)行操作       ");
        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("請(qǐng)輸入下單信息[編號(hào)][數(shù)量] (格式如: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("請(qǐng)按照格式要求輸入信息");
                    break;
                }
                case "D":{
                    System.out.println("請(qǐng)輸入取消信息[編號(hào) 數(shù)量](如下格式: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("請(qǐng)按照格式要求輸入信息");
                    break;
                }
                case "L": {
                    GoodsCenter.printGoods();
                    break;
                }
                case "R": {
                    return;
                }
                default: {
                    usageInfo();
                }
            }
        }
 
    }
 
    
    public static void setting() {
        settingInfo();
        if (GoodsCenter.isFull()) {
            System.out.println("!當(dāng)前商品貨架已經(jīng)滿(mǎn)了,如果要進(jìn)行添加請(qǐng)下降部分商品");
        }
        while (true) {
            String line = scanner.nextLine();
            switch (line.toUpperCase()) {
                case "S": {
                    GoodsCenter.printGoods();
                    break;
                }
                case "A": {
                    System.out.println("請(qǐng)輸入上架商品信息(如下格式:1 餐巾紙 1.4):");
                    Goods goods = readGoods();
                    if (goods == null) {
                        System.out.println("!請(qǐng)按照格式要求輸入信息");
                        break;
                    }
                    if (GoodsCenter.isFull()) {
                        System.out.println("!當(dāng)前商品貨架已經(jīng)滿(mǎn)了,如果要進(jìn)行添加請(qǐng)下降部分商品");
                    } else if (GoodsCenter.isExist(goods)) {
                        System.out.println("!上架商品已經(jīng)存在,注意編號(hào)不能重復(fù)");
                    } else {
                        GoodsCenter.addGoods(goods);
                        GoodsCenter.printGoods();
                    }
                    break;
                }
                case "D": {
                    System.out.println("請(qǐng)輸入下架商品信息編號(hào)(如下格式:1 ):");
                    Goods goods = readGoods();
                    if (goods == null) {
                        System.out.println("請(qǐng)按照格式要求輸入信息");
                        break;
                    }
                    if (GoodsCenter.isPutaway(goods)) {
                        GoodsCenter.soldOutGoods(goods);
                        GoodsCenter.printGoods();
                    } else {
                        System.out.println("請(qǐng)選擇上架的商品編號(hào),當(dāng)前下架商品未設(shè)置");
                    }
                    break;
                }
                case "U": {
                    System.out.println("請(qǐng)輸入修改商品信息(如下格式:1 餐巾紙 1.4 )");
                    Goods goods = readGoods();
                    if (goods == null) {
                        System.out.println("請(qǐng)按照格式要求輸入信息");
                        break;
                    }
                    if (GoodsCenter.isPutaway(goods)) {
                        GoodsCenter.modifyGoods(goods);
                        GoodsCenter.printGoods();
                    } else {
                        System.out.println("請(qǐng)選擇上架的商品編號(hào),當(dāng)前修改商品未設(shè)置");
                    }
                    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類(lèi)

class GoodsCenter {
    //商品占位符
    private static String placeholder = "--";
 
    //最大商品數(shù)量
    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;
    }
 
    //商品已滿(mǎn)
    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" + "編號(hào)" + "\t" +"產(chǎn)品名稱(chēng)" + "\t" + "單價(jià)");
        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類(lèi)

class Goods{
    //商品編號(hào)
    private int id;
 
    //商品名稱(chēng)
    private  String name;
 
    //商品價(jià)格
    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類(lèi)

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("編號(hào)" + this.getId()     );
        System.out.println("打印時(shí)間" + LocalDate.now().toString());
        System.out.println("========================");
        System.out.println("編號(hào)   名稱(chēng)    數(shù)量   單價(jià)");
        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("總價(jià):%2f",this.getTotalPrice()));
        System.out.println("=========================");
    }
}

項(xiàng)目總結(jié)

  • 用常用String類(lèi),Scanner類(lèi)實(shí)現(xiàn),代碼量不多,簡(jiǎn)單易懂
  • 有弊端存在,就是用數(shù)組存放商品,容易出現(xiàn)數(shù)組越界異常,而且如果商品多的話(huà)用數(shù)組存儲(chǔ)也是極其不方便的
  • 還有就是未使用到數(shù)據(jù)庫(kù),商品信息,訂單信息的保存有很多不方便的地方,如果建立連接了數(shù)據(jù)庫(kù),這個(gè)問(wèn)題就解決了

目前能力只能實(shí)現(xiàn)到這了,希望可以再努力一下,將數(shù)據(jù)庫(kù)加入到項(xiàng)目中,讓它的易用性再提升更多。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot項(xiàng)目實(shí)現(xiàn)jar包方式打包部署

    SpringBoot項(xiàng)目實(shí)現(xiàn)jar包方式打包部署

    SpringBoot默認(rèn)的打包方式就是jar包,本文就來(lái)介紹一下SpringBoot項(xiàng)目實(shí)現(xiàn)jar包方式打包部署,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-08-08
  • mybatis集成到spring的方式詳解

    mybatis集成到spring的方式詳解

    這篇文章主要介紹了mybatis是如何集成到spring的,將mybatis集成到spring之后,就可以被spring的ioc容器托管,再也不用自己創(chuàng)建SqlSessionFactory?、打開(kāi)SqlSession等操作,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • Spring IOC和aop的原理及實(shí)例詳解

    Spring IOC和aop的原理及實(shí)例詳解

    這篇文章主要介紹了Spring IOC和aop的原理及實(shí)例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • SpringBoot整個(gè)啟動(dòng)過(guò)程的分析

    SpringBoot整個(gè)啟動(dòng)過(guò)程的分析

    今天小編就為大家分享一篇關(guān)于SpringBoot整個(gè)啟動(dòng)過(guò)程的分析,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-03-03
  • Java實(shí)現(xiàn)復(fù)制文件并命名的超簡(jiǎn)潔寫(xiě)法

    Java實(shí)現(xiàn)復(fù)制文件并命名的超簡(jiǎn)潔寫(xiě)法

    這篇文章主要介紹了Java實(shí)現(xiàn)復(fù)制文件并命名的超簡(jiǎn)潔寫(xiě)法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java基礎(chǔ)之引用相關(guān)知識(shí)總結(jié)

    Java基礎(chǔ)之引用相關(guān)知識(shí)總結(jié)

    今天聊聊Java的引用,大多數(shù)時(shí)候我們說(shuō)引用都是強(qiáng)引用,只有在對(duì)象不使用的情況下才會(huì)釋放內(nèi)存,其實(shí)Java 內(nèi)存有四種不同的引用.一起看看吧,,需要的朋友可以參考下
    2021-05-05
  • JUnit4 Hamcrest匹配器常用方法總結(jié)

    JUnit4 Hamcrest匹配器常用方法總結(jié)

    這篇文章主要介紹了JUnit4 Hamcrest匹配器常用方法總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Java后端調(diào)用微信支付和支付寶支付的詳細(xì)步驟

    Java后端調(diào)用微信支付和支付寶支付的詳細(xì)步驟

    這篇文章主要介紹了Java后端如何調(diào)用微信支付和支付寶支付,涵蓋了基本概念、配置步驟、代碼示例以及注意事項(xiàng),文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-04-04
  • Java詳細(xì)分析梳理垃圾回收機(jī)制

    Java詳細(xì)分析梳理垃圾回收機(jī)制

    垃圾回收,顧名思義,便是將已經(jīng)分配出去的,但卻不再使用的內(nèi)存回收回來(lái),以便能夠再次分配。在?Java?虛擬機(jī)的語(yǔ)境下,垃圾指的是死亡的對(duì)象所占據(jù)的堆空間
    2022-04-04
  • Spring?Boot整合log4j2日志配置的詳細(xì)教程

    Spring?Boot整合log4j2日志配置的詳細(xì)教程

    這篇文章主要介紹了SpringBoot項(xiàng)目中整合Log4j2日志框架的步驟和配置,包括常用日志框架的比較、配置參數(shù)介紹、Log4j2配置詳解以及使用步驟,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-02-02

最新評(píng)論

石狮市| 兰考县| 芜湖县| 昌邑市| 南安市| 囊谦县| 宁津县| 剑川县| 横峰县| 金阳县| 夏邑县| 旺苍县| 巴彦县| 东乡县| 商丘市| 绵竹市| 紫阳县| 伊宁县| 丹棱县| 屏山县| 正镶白旗| 万安县| 辉南县| 贵定县| 望城县| 玉环县| 盐山县| 卓资县| 乌兰察布市| 南涧| 玛多县| 涞源县| 祁阳县| 枣强县| 临沂市| 怀仁县| 太仆寺旗| 台中市| 灵石县| 陇川县| 平远县|