题解 | #自动售货系统#
自动售货系统
https://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) { Scanner sc = new Scanner(System.in); 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)); while(sc.hasNextLine()){ String s = sc.nextLine(); String[] arr = s.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("q")){ qStep(arr[i]); }else if(arr[i].contains("c")){ cStep(arr[i]); } } } } public static void rStep(String s){ String[] strs = s.split("\\s+"); String[] goodNum = strs[1].split("-"); for(int i=0;i<goodNum.length;i++){ int volume = Integer.parseInt(goodNum[i]); goods.get(i).volume = volume; goodTotal += volume; } String[] moneyNum = strs[2].split("-"); for(int i=0;i<moneyNum.length;i++){ moneys.get(i).count = Integer.parseInt(moneyNum[i]); } moneyTotal = 0; System.out.println("S001:Initialization is successful"); } public static void pStep(String s){ String[] msg = s.split("\\s+"); int num=Integer.parseInt(msg[1]); if(num!=1&&num!=2&&num!=5&&num!=10){ System.out.println("E002:Denomination error"); return; } 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++; }else if(num==2){ moneys.get(1).count++; }else if(num==5){ moneys.get(2).count++; }else if(num==10){ moneys.get(3).count++; } System.out.println("S002:Pay success,balance="+moneyTotal); } public static void bStep(String s){ String[] msg = s.split(" "); String goodName = msg[1]; boolean exist = false; Good good = null; for(int i=0;i<goods.size();i++){ if(goods.get(i).name.equals(goodName)){ exist = true; good = goods.get(i); break; } } if(!exist){ System.out.println("E006:Goods does not exist"); return; } if(good.volume==0){ System.out.println("E007:The goods sold out"); return; } if(good.price>moneyTotal){ System.out.println("E008:Lack of balance"); return; } moneyTotal -= good.price; System.out.println("S003:Buy success,balance="+moneyTotal); } public static void qStep(String s){ if(!s.contains(" ")){ System.out.println("E010:Parameter error"); return; } String[] msg = s.split("\\s+"); if(msg[1]!="1"&&msg[1]!="0"){ System.out.println("E010:Parameter error"); return; } if(msg[1]=="0"){ System.out.println("A1 2 "+goods.get(0).volume); System.out.println("A2 3 "+goods.get(1).volume); System.out.println("A3 4 "+goods.get(2).volume); System.out.println("A4 5 "+goods.get(3).volume); System.out.println("A5 8 "+goods.get(4).volume); System.out.println("A6 6 "+goods.get(5).volume); return; } if(msg[1]=="1"){ System.out.println("1 yuan coin number="+moneys.get(0).count); System.out.println("2 yuan coin number="+moneys.get(1).count); System.out.println("5 yuan coin number="+moneys.get(2).count); System.out.println("10 yuan coin number="+moneys.get(3).count); return; } } public static void cStep(String s){ if(moneyTotal==0){ System.out.println("E009:Work failure"); return; } int tenNum=0; int fiveNum=0; int twoNum=0; int oneNum=0; while(moneyTotal>=10&&moneys.get(3).count>0){ moneyTotal -= 10; moneys.get(3).count--; tenNum++; } while(moneyTotal>=5&&moneys.get(2).count>0){ moneyTotal -= 5; moneys.get(2).count--; fiveNum++; } while(moneyTotal>=2&&moneys.get(1).count>0){ moneyTotal -= 2; moneys.get(1).count--; twoNum++; } while(moneyTotal>=1&&moneys.get(0).count>0){ moneyTotal -= 1; moneys.get(0).count--; oneNum++; } moneyTotal=0; System.out.println("1 yuan coin number=" + oneNum); System.out.println("2 yuan coin number=" + twoNum); System.out.println("5 yuan coin number=" + fiveNum); System.out.println("10 yuan coin number=" + tenNum); } } class Good { String name; int price; int volume; 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; } }