题解 | #自动售货系统#

自动售货系统

http://www.nowcoder.com/practice/cd82dc8a4727404ca5d32fcb487c50bf

四个坑,已通过注释表明

import java.util.*;
public class Main {
        static ArrayList<Good> goods = new ArrayList<Good>();
        static ArrayList<Money> moneys = new ArrayList<Money>();
        static int goodTotal;
        static int moneyTotal;
        public static void main(String[] args) {           
            goods.add(new Good("A1", 2));
            goods.add(new Good("A2", 3));
            goods.add(new Good("A3", 4));
            goods.add(new Good("A4", 5));
            goods.add(new Good("A5", 8));
            goods.add(new Good("A6", 6));
         
            moneys.add(new Money(1));
            moneys.add(new Money(2));
            moneys.add(new Money(5));
            moneys.add(new Money(10));
            Scanner sc = new Scanner(System.in);
            while (sc.hasNextLine()) {
                String str = sc.nextLine();
                String[] arr = str.split(";");
                for (int i=0; i< arr.length; i++) {
                    if (arr[i].contains("r")) {
                        rStep(arr[i]);
                    } else if (arr[i].contains("p")) {
                        pStep(arr[i]);
                    } else if (arr[i].contains("b")) {
                        bStep(arr[i]);
                    } else if (arr[i].contains("c")) {
                        cStep(arr[i]);
                    } else if (arr[i].contains("q")) {
                        qStep(arr[i]);
                    }
                }
            }
            
        }
    public static void rStep(String str) {
        String[] msg = str.split(" ");
        String[] goodNum = msg[1].split("-");
        for (int i = 0; i < goodNum.length; i++) {
            int count = Integer.parseInt(goodNum[i]);
            goods.get(i).count = count;
            goodTotal += count;
        }
        String[] moneyNum = msg[2].split("-");
        for (int i = 0; i < moneyNum.length; i++) {
            moneys.get(i).count = Integer.parseInt(moneyNum[i]);
        }
        moneyTotal = 0;//每次初始化,投币余额必须赋0
        System.out.println("S001:Initialization is successful");
    }
     public static void pStep(String str) {
        String[] msg = str.split(" ");
         int num = Integer.parseInt(msg[1]);
         if (num != 1 && num != 2 && num != 5 && num != 10) {
             System.out.println("E002:Denomination error");
             return;
         }
         // 注意金额为2时需进行count*2才是金额总数
         if (num > 2 && moneys.get(0).count + moneys.get(1).count *2 < num) {
             System.out.println("E003:Change is not enough, pay fail");
             return;
         }
         if (goodTotal == 0) {
             System.out.println("E005:All the goods sold out");
             return;
         }
         moneyTotal += num;
         // 注意,投入的金额需添加到对应的钱币盒内
         if (num ==1) {
             moneys.get(0).count+= 1;
         } else if (num ==2) {
             moneys.get(1).count+= 1;
         } else if (num ==5) {
             moneys.get(2).count+= 1;
         } else if (num ==10) {
             moneys.get(3).count+= 1;
         }
         System.out.println("S002:Pay success,balance=" + moneyTotal);
    }
     public static void bStep(String str) {
        String[] msg = str.split(" ");
        String name = msg[1];
        boolean existed = false; 
        Good good = null;
        for (int i =0; i< goods.size(); i++) {
            if (goods.get(i).name.equals(name)) {
                existed = true;
                good = goods.get(i);
                break;
            }
        }
         if(!existed) {
             System.out.println("E006:Goods does not exist");
             return;
         }
          if (goodTotal == 0) {
             System.out.println("E007:The goods sold out");
             return;
         }
         if (moneyTotal < good.price) {
             System.out.println("E008:Lack of balance");
             return;
         }
         moneyTotal -= good.price;
        System.out.println("S003:Buy success,balance=" + moneyTotal); 
    }
     public static void cStep(String str) {
        if (moneyTotal == 0) {
             System.out.println("E009:Work failure");
             return;
         }
         int tenNum = 0;
         int fiveNum = 0;
         int secNum = 0;
         int oneNum =0;
         while (moneyTotal >= 10 && moneys.get(3).count > 0) {
             moneyTotal -= 10;
             moneys.get(3).count -= 1;
             tenNum++;
         }
         while (moneyTotal >= 5 && moneys.get(2).count > 0) {
             moneyTotal -= 5;
             moneys.get(2).count -= 1;
             fiveNum++;
         }
          while (moneyTotal >= 2 && moneys.get(1).count > 0) {
             moneyTotal -= 2;
             moneys.get(1).count -= 1;
             secNum++;
         }
          while (moneyTotal >= 1 && moneys.get(0).count > 0) {
             moneyTotal -= 1;
             moneys.get(0).count -= 1;
             oneNum++;
         }
         moneyTotal=0;
         System.out.println("1 yuan coin number=" + oneNum); 
         System.out.println("2 yuan coin number=" + secNum); 
         System.out.println("5 yuan coin number=" + fiveNum); 
         System.out.println("10 yuan coin number=" + tenNum); 
    }
     public static void qStep(String str) {
     //p0、p1需要判错
         if (!str.contains(" ")) {
             System.out.println("E010:Parameter error");
             return;
         }
        String[] arr = str.split(" ");
        int num = Integer.parseInt(arr[1]);
         if (num < 0 || num > 1) {
             System.out.println("E010:Parameter error");
             return;
         }
         if (num == 0) {
             Good good = null;
             for (int i = 0; i <goods.size(); i++) {
                 good = goods.get(i);
                 System.out.println(good.name + " " + good.price + " " + good.count);
             }
         } else {
             Money money = null;
             for (int i = 0; i< moneys.size(); i++) {
                 money = moneys.get(i);
                 System.out.println(money.id + " yuan coin number=" + money.count);
             }
         }
    }
}

class Good {
    String name;
    int price;
    int count;
    
    public Good(String name, int price) {
        this.name = name;
        this.price = price;
    }
}

class Money {
    int id;
    int count;
    public Money(int id) {
        this.id = id;
    }
}
全部评论
第99行,我觉得应该是good.count == 0而不是goodTotal == 0,应该看单品数量为不为0,而不是总数
1 回复 分享
发布于 2022-06-08 16:54
很强!
点赞 回复 分享
发布于 2022-08-13 21:13
题目要求查询商品要按数量从大到小排序,这里好像没有排序吧,可以加一下
点赞 回复 分享
发布于 2022-08-13 21:43
购买商品后没有扣减商品数量,导致只要还有钱就可以不断购买该商品
点赞 回复 分享
发布于 2022-12-05 01:05 广东

相关推荐

挣K存W养DOG:他真的很中意你,为什么不回他
点赞 评论 收藏
分享
11-01 08:48
门头沟学院 C++
伤心的候选人在吵架:佬你不要的,能不能拿户口本证明过户给我。。球球了
点赞 评论 收藏
分享
13 1 评论
分享
牛客网
牛客企业服务