java commodity vending machine


foreword

1. Small commodity vending machine program, example of required functions:

Please enter the product you want to buy:

1. Bread (bean paste, matcha), 2. Tea (black tea, green tea), 3. Cola, 4. Chewing gum (watermelon, cantaloupe), 5. Paper towel

1

Please enter the information of the bread you need: 1. Red bean paste, 2. Matcha

2

Please enter the quantity of the product:

1

Do you need other products: 1. Continue, 2. Settlement

1

Please enter the product you want to buy:

1. Bread (bean paste, matcha), 2. Tea (black tea, green tea), 3. Cola, 4. Chewing gum (different flavors), 5. Paper towel

。。。。

2

You purchased *********** , a total of 10 yuan, please pay 10 yuan:

5

Please continue to invest 5 yuan

100

Looking for you 90 yuan, please pay attention to put away the purchased products, thank you for your patronage!

10

Please pay attention to put away the purchased products, thank you for your patronage!


提示:以下是本篇文章正文内容,下面案例可供参考

1. Create classes: customer , goods , shopping

Three files:

 1.customer class, main entry function

import java.util.Scanner;

public class customer {
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        shopping shop =new shopping();
        while (true){
            System.out.println();
            System.out.println("*******------*******--------*******------*******------********-------*******-----*");
            System.out.println("-------------------------------欢迎使用购物系统------------------------------------~~");
            System.out.println("请输入你要购买的商品:(按以下序号选择)");
            System.out.println("1、面包(豆沙、抹茶),2、茶(红茶、绿茶),3、可乐,4、口香糖(西瓜、哈密瓜),5、纸巾----6、退出");
            System.out.println("---------------------------------------------------------------------------------");
            int select = scanner.nextInt();
            if (select == 6){
                System.out.println("欢迎下次光临!!再见");
                break;
            }
            else {
                shop.shop(select);
            }

        }



    }
}

 2.goods category (counted as the parent category of commodities, I also wrote the subcategories in it)

public class goods {
    String goodsname;
    double price;
    String test;
    public goods(){
 //构造器,初始化商品
    }
    //含参构造器,一次性构造完成
   public goods(String goodsname,double price){
        this.goodsname=goodsname;
        this.price=price;

   }
   public static class  bread extends goods{
        String test1;
        String test2;
        public bread(String goodsname,double price,String test1,String test2){
            this.goodsname=goodsname;
            this.price=price;
            this.test1=test1;
            this.test2=test2;
        }
   }
    // 1、面包(豆沙、抹茶),2、茶(红茶、绿茶),3、可乐,4、口香糖(西瓜、哈密瓜),5、纸巾
    public static class  tea extends goods{
        String test1;
        String test2;
        public tea(String goodsname,double price,String test1,String test2){
            this.goodsname=goodsname;
            this.price=price;
            this.test1=test1;
            this.test2=test2;
        }
    }
    // 1、面包(豆沙、抹茶),2、茶(红茶、绿茶),3、可乐,4、口香糖(西瓜、哈密瓜),5、纸巾
    public static class  gum extends goods{
        String test1;
        String test2;
        public gum(String goodsname,double price,String test1,String test2){
            this.goodsname=goodsname;
            this.price=price;
            this.test1=test1;
            this.test2=test2;
        }
    }


}

3.shopping class, business logic processing

import java.util.*;

public class shopping {

    // 1、面包(豆沙、抹茶),2、茶(红茶、绿茶),3、可乐,4、口香糖(西瓜、哈密瓜),5、纸巾
    goods.bread bread = new goods.bread("面包",10,"豆沙","抹茶");
    goods.tea tea = new goods.tea("茶",5,"红茶","绿茶");
    goods coke = new goods("可乐",3);
    goods.gum gum = new goods.gum("口香糖",4,"西瓜","哈密瓜");
    goods tissue = new goods("纸巾",4);
    Map good =new HashMap<>();
    Map<String, Double> goodsmap=new HashMap<String, Double>();
    String test;
    public void shop(int choose) {


            switch (choose) {

                case 1:
                    System.out.println("【*****请输入你需要的商品信息*****】:");
                    System.out.println("选择口味:" + "1: " + bread.test1 + " 2: " + bread.test2);
                    Scanner scanner = new Scanner(System.in);
                    int num = scanner.nextInt();
                    if (num == 1) {
                         test = bread.test1;
                    } else if (num == 2) {
                         test = bread.test2;
                    } else {
                        System.out.println("请选择已有口味:" + "1:" + bread.test1 + "2:" + bread.test2);
                        scanner = new Scanner(System.in);
                        num = scanner.nextInt();
                        if (num == 1) {
                             test = bread.test1;
                        } else if (num == 2) {
                             test = bread.test2;
                        }
                    }
                    int e = goodnumAdd();

                    goodsmap.put(bread.goodsname,e * bread.price);

                    good =goodsmap;
                    otherneed(goodsmap, test, e * bread.price);
                    break;

                // 1、面包(豆沙、抹茶),2、茶(红茶、绿茶),3、可乐,4、口香糖(西瓜、哈密瓜),5、纸巾
                case 2:
                    System.out.println("【*****请输入你需要的商品信息*****】:");
                    System.out.println("选择口味:" + "1:" + tea.test1 + "2:" + tea.test2);
                    scanner = new Scanner(System.in);
                    num = scanner.nextInt();
                    if (num == 1) {
                         test = tea.test1;
                    } else if (num == 2) {
                         test = tea.test2;
                    } else {
                        System.out.println("请选择已有口味:"+"1:" + tea.test1 + "2:" + tea.test2);
                        scanner = new Scanner(System.in);
                        num = scanner.nextInt();

                        if (num == 1) {
                             test = tea.test1;
                        } else {
                             test = tea.test2;
                        }
                    }
                    int a = goodnumAdd();

                    goodsmap.put(tea.goodsname,a * tea.price);
                    good =goodsmap;
                    otherneed( goodsmap,test ,a * tea.price);
                    break;

                // 1、面包(豆沙、抹茶),2、茶(红茶、绿茶),3、可乐,4、口香糖(西瓜、哈密瓜),5、纸巾
                case 3:
                    int i = goodnumAdd();

                    goodsmap.put(coke.goodsname,i * coke.price);
                    good =goodsmap;
                    otherneed(goodsmap, "原味",i * coke.price);
                    break;

                // 1、面包(豆沙、抹茶),2、茶(红茶、绿茶),3、可乐,4、口香糖(西瓜、哈密瓜),5、纸巾
                case 4:
                    System.out.println("【*****请输入你需要的商品信息*****】:");
                    System.out.println("选择口味:" + "1:" + gum.test1 + "2:" + gum.test2);
                    scanner = new Scanner(System.in);
                    num = scanner.nextInt();
                    if (num == 1) {
                         test = gum.test1;
                    } else if (num == 2) {
                         test = gum.test2;
                    } else {
                        System.out.println("请选择已有口味:" + "1:" + gum.test1 + "2:" + gum.test2);
                        scanner = new Scanner(System.in);
                        num = scanner.nextInt();
                        if (num == 1) {
                             test = gum.test1;
                        } else {
                             test = gum.test2;
                        }
                    }
                    int p = goodnumAdd();
                    goodsmap.put(gum.goodsname,p * gum.price);
                    good =goodsmap;
                    otherneed(goodsmap, test,p * gum.price);
                    break;
                // 1、面包(豆沙、抹茶),2、茶(红茶、绿茶),3、可乐,4、口香糖(西瓜、哈密瓜),5、纸巾
                case 5:
                    int k = goodnumAdd();
                    goodsmap.put(tissue.goodsname,k * tissue.price);
                    good =goodsmap;
                    otherneed(goodsmap, "原味",k * tissue.price);
                    break;
                case 6:
                    System.out.println("欢迎下次光临!!");
                    break;
                default:
                    System.out.println("没有此商品,请重新选择!!");
                    break;
            }
           // System.out.println(goodsmap);


        }

    private void otherneed(Map goodsname,String test,double goodprice) {
        Scanner scanner=new Scanner(System.in);
        System.out.println("【*******你还需要其它商品吗*******】! 1、继续,2、结算 ");
        int input = scanner.nextInt();
        if(input == 1){
            System.out.println("---------------------------------------------------------------------------------");
            System.out.println("请输入你要购买的商品:(按以下序号选择)");
            System.out.println("1、面包(豆沙、抹茶),2、茶(红茶、绿茶),3、可乐,4、口香糖(西瓜、哈密瓜),5、纸巾----6、退出");
            System.out.println("---------------------------------------------------------------------------------");
            int choose = scanner.nextInt();
            shop(choose);//又跳转到商品购物的界面
        }
        else if(input ==2){
            //success(goodsname,test,goodprice);
            System.out.println("您购买了:"+good.keySet());
            System.out.println("明细:"+good);

            double sum=0.0;
             Object[] obj=good.values().toArray();
             for (int i=0;i<obj.length;i++){
                 Double d=(Double) obj[i];
                 sum+=d;
             }
            System.out.println(sum);
            System.out.println("共计"+sum+"请支付:"+sum+"元");//系统提示语
            Scanner sc = new Scanner(System.in);
            int pay = sc.nextInt();
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            if (pay < sum) {
                double money = sum-pay;
                System.out.println("请继续投"+money+"币");
                int cpay =sc.nextInt();
                if (cpay==money){
                    System.out.println("请注意收好购买的产品,谢谢惠顾!");
                    System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>");
                }

            }
            else if(pay > sum) {
                double money = pay-sum;
                System.out.println("找你"+money+"元,请注意收好购买的产品,谢谢惠顾!");
                System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            }
            else {
                System.out.println("请注意收好购买的产品,谢谢惠顾!");
                System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            }
            good.clear();


          }
        else {
            System.out.println("******点错了!!只有两项选择*******: 1、继续,2、结算");
            Scanner sca=new Scanner(System.in);
            int put= sca.nextInt();
            if(put == 1){
                System.out.println("---------------------------------------------------------------------------------");
                System.out.println("请输入你要购买的商品:(按以下序号选择)");
                System.out.println("1、面包(豆沙、抹茶),2、茶(红茶、绿茶),3、可乐,4、口香糖(西瓜、哈密瓜),5、纸巾----6、退出");
                System.out.println("---------------------------------------------------------------------------------");
                int choose = scanner.nextInt();
                shop(choose);//又跳转到商品购物的界面
            }
            else{
                double sum=0.0;
                Object[] obj=good.values().toArray();
                for (int i=0;i<obj.length;i++){
                    Double d=(Double) obj[i];
                    sum+=d;
                }
                //success(goodsname,test,goodprice);
                System.out.println("您购买了:"+good.keySet());
                System.out.println("明细:"+good);
                System.out.println("共计"+sum+"请支付:"+sum+"元");//系统提示语
                Scanner sc = new Scanner(System.in);
                int pay = sc.nextInt();
                System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                if (pay < sum) {
                    double money = sum-pay;
                    System.out.println("请继续投"+money+"币");
                    int cpay =sc.nextInt();
                    if (cpay==money){
                        System.out.println("请注意收好购买的产品,谢谢惠顾!");
                        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>");
                    }

                }
                else if(pay > sum) {
                    double money = pay-sum;
                    System.out.println("找你"+money+"元,请注意收好购买的产品,谢谢惠顾!");
                    System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                }
                else {
                    System.out.println("请注意收好购买的产品,谢谢惠顾!");
                    System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                }
                good.clear();


            }
        }
        }

    private int goodnumAdd() {

       System.out.println("请输入商品数量:");
       Scanner scanner =new Scanner(System.in);
       int goodsnum = scanner.nextInt();
       return goodsnum;

    }


}

2. Function display

1. Shopping


2. Settlement

 

 


Summarize

  1. How to make the system automatically cycle

I wrote a while loop for the entry function, the Boolean condition is true, it is always true, and it keeps executing. When exiting, write a judgment in the loop. If the user enters 6, which is the if condition that triggers the break, it exits.

  1. How to put the selected product into a container, and get the name and price of the product to be settled from the container at the final settlement

I originally wanted to use an array to save, but later changed to a Map, which can not only save the name, but also save the price.

Put the Map name.put(String,Double) into each case condition, so that it is convenient to trigger the addition as soon as the condition is entered.

During operation, the size of the map is always 1, that is to say, it starts again when the case is out. I tried to define Map as a global one. After trying it out, the final result is that two maps are defined, map1 is responsible for adding in the case, map2 is responsible for adding map1, and map2 is used. (Idealization means that map2 is stacked with each map1 that has entered the case)

  1. How to take out the value in the map and accumulate it

Map.values() gets a collection of commodity prices, and Map.values().toArray() can convert it into an array, but it is of Object type. It involves taking Object-type array elements, performing type conversion, and searching online, in the form of Double d=(Double) obj[i]; Just let a single element be forced to convert. Accumulate using a for loop

Guess you like

Origin blog.csdn.net/SlinaW/article/details/128032596