题解 | #自动售货系统#

自动售货系统

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;
    }
}

全部评论

相关推荐

11-09 14:54
已编辑
华南农业大学 产品经理
大拿老师:这个简历,连手机号码和照片都没打码,那为什么关键要素求职职位就不写呢? 从上往下看,都没看出自己到底是产品经理的简历,还是电子硬件的简历? 这是一个大问题,当然,更大的问题是实习经历的描述是不对的 不要只是去写实习流程,陈平,怎么去开会?怎么去讨论? 面试问的是你的产品功能点,是怎么设计的?也就是要写项目的亮点,有什么功能?这个功能有什么难处?怎么去解决的? 实习流程大家都一样,没什么优势,也没有提问点,没有提问,你就不得分 另外,你要明确你投的是什么职位,如果投的是产品职位,你的项目经历写的全都是跟产品无关的,那你的简历就没用 你的面试官必然是一个资深的产品经理,他不会去问那些计算机类的编程项目 所以这种四不像的简历,在校招是大忌
点赞 评论 收藏
分享
想润的芹菜人狠话不多:把其中一个老总放中间都会得罪另一个
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务