题解 | #自动售货系统#

自动售货系统

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

import java.util.*;

/*
	1.难度不难,有耐心慢慢代码实现就好
	2.读题也是一种能力,有几个坑描述没提到
	3.默认的投币会加到存钱盒,以及退币减掉存钱盒里的钱币
	4.好像直接实现商品和存钱盒类会更简洁易懂
*/
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String[] ss = in.nextLine().split(";");
            for (int i = 0; i < ss.length; i++) {
                System.out.println(handle(ss[i]));
            }
            // System.out.println(handle("r 6-5-4-3-2-1 4-3-2-1"));
        }
    }

    public static String handle(String s) {
        String[] ss = s.split("\\s+");
        // 1、系统初始化 r
        if ("r".equals(ss[0])) {
            return rInit(s);
        }
        // 2、投币 p
        if ("p".equals(ss[0])) {
            return pCoin(s);
        }
        // 3、购买商品 b
        if ("b".equals(ss[0])) {
            return bBuy(s);
        }
        // 4、退币 c
        if ("c".equals(ss[0])) {
            return cRefund(s);
        }
        // 5、查询 q
        if ("q".equals(ss[0])) {
            return qQuery(s);
        }
        return "E010:Parameter error";
    }

    static HashMap<String, Integer> merchandise;
    static HashMap<String, Integer> currency;
    static HashMap<String, Integer> merchandisePrice = new HashMap<>();
    static {
        merchandisePrice.put("A1", 2);
        merchandisePrice.put("A2", 3);
        merchandisePrice.put("A3", 4);
        merchandisePrice.put("A4", 5);
        merchandisePrice.put("A5", 8);
        merchandisePrice.put("A6", 6);
    }

    public static String rInit(String s) {
        //TODO
        merchandise = new HashMap<>();
        currency = new HashMap<>();
        String[] ss1 = s.split(" ");
        String[] ss2 = ss1[1].split("-");
        String[] ss3 = ss1[2].split("-");
        merchandise.put("A1", Integer.parseInt(ss2[0]));
        merchandise.put("A2", Integer.parseInt(ss2[1]));
        merchandise.put("A3", Integer.parseInt(ss2[2]));
        merchandise.put("A4", Integer.parseInt(ss2[3]));
        merchandise.put("A5", Integer.parseInt(ss2[4]));
        merchandise.put("A6", Integer.parseInt(ss2[5]));

        currency.put("1", Integer.parseInt(ss3[0]));
        currency.put("2", Integer.parseInt(ss3[1]));
        currency.put("5", Integer.parseInt(ss3[2]));
        currency.put("10", Integer.parseInt(ss3[3]));

        return "S001:Initialization is successful";
    }

    static int balance = 0;

    public static String pCoin(String s) {
        //TODO
        String[] ss = s.split(" ");
        // 如果投入非1元、2元、5元、10元的钱币面额
        if (!"1".equals(ss[1]) && !"2".equals(ss[1]) && !"5".equals(ss[1]) &&
                !"10".equals(ss[1])) {
            return "E002:Denomination error";
        }
        // 如果存钱盒中1元和2元面额钱币总额小于本次投入的钱币面额
        if (Integer.parseInt(ss[1]) != 1 && Integer.parseInt(ss[1]) != 2 && (currency.get("1") + currency.get("2") * 2) < Integer.parseInt(ss[1])) {
            return "E003:Change is not enough, pay fail";
        }
        // 如果自动售货机中商品全部销售完毕
        int count = 0;
        for (String key : merchandise.keySet()) {
            count += merchandise.get(key);
        }
        if (count == 0) {
            return "E005:All the goods sold out";
        }
        // 如果投币成功
        balance += Integer.parseInt(ss[1]);
        currency.put(ss[1], currency.get(ss[1]) + 1);
        return "S002:Pay success,balance=" + balance;
    }

    public static String bBuy(String s) {
        //TODO
        String[] ss = s.split(" ");
        // 如果购买的商品不在商品列表中
        if (!merchandise.containsKey(ss[1])) {
            return "E006:Goods does not exist";
        }
        // 如果所购买的商品的数量为0
        if (merchandise.get(ss[1]) == 0) {
            return "E007:The goods sold out";
        }
        // 如果投币余额小于待购买商品价格
        if (balance < merchandisePrice.get(ss[1])) {
            return "E008:Lack of balance";
        }
        // 如果购买成功
        balance -= merchandisePrice.get(ss[1]);
        return "S003:Buy success,balance=" + balance;
    }

    public static String cRefund(String s) {
        //TODO
        // 如果投币余额等于0的情况下
        if (balance == 0) {
            return "E009:Work failure";
        }
        // 如果投币余额大于0的情况下,按照 退币原则 进行“找零”,输出退币信息;
        int re1 = 0, re2 = 0, re5 = 0, re10 = 0;
        if (balance >= 10) {
            if (currency.get("10") >= balance / 10) {
                re10 = balance / 10;
                balance %= 10;
            } else {
                re10 = currency.get("10");
                balance -= re10 * 10;
            }
            currency.put("10", currency.get("10") - re10);
        }
        if (balance >= 5) {
            if (currency.get("5") >= balance / 5) {
                re5 = balance / 5;
                balance %= 5;
            } else {
                re5 = currency.get("5");
                balance -= re5 * 5;
            }
            currency.put("5", currency.get("5") - re5);
        }
        if (balance >= 2) {
            if (currency.get("2") >= balance / 2) {
                re2 = balance / 2;
                balance %= 2;
            } else {
                re2 = currency.get("2");
                balance -= re2 * 2;
            }
            currency.put("2",currency.get("2") - re2);
        }
        if (balance >= 1) {
            if (currency.get("1") >= balance) {
                re1 = balance;
                balance = 0;
            } else {
                re1 = currency.get("1");
                balance -= re1;
            }
            currency.put("1",currency.get("1") - re1);
        }
        // balance = 0;
        return "1 yuan coin number=" + re1 + "\n2 yuan coin number=" + re2 +
               "\n5 yuan coin number=" + re5 + "\n10 yuan coin number=" + re10;
    }

    public static String qQuery(String s) {
        //TODO
        String[] ss = s.split(" ");
        if (ss[1] == "0") {
            String[] ssMerch = new String[6];
            int idx = 0;
            for(String key : merchandise.keySet()) {
                ssMerch[idx] = merchandise.get(key) + " " + key + " " + merchandisePrice.get(key);
                idx++;
            }
            Arrays.sort(ssMerch);
            StringBuilder sb = new StringBuilder();
            for(int i = 0; i < ssMerch.length; i++) {
                sb.append(ssMerch[i] + "\n");
            }
            return sb.toString();
        } else if (ss[1] == "1") {
            return "1 yuan coin number=" + currency.get("1") + "\n2 yuan coin number=" + currency.get("2") + "\n5 yuan coin number=" + currency.get("5") + "\n10 yuan coin number=" + currency.get("10");
        } else {
            return "E010:Parameter error";
        }
    }
}

全部评论

相关推荐

10-06 12:46
门头沟学院 Java
跨考小白:定时任务启动
点赞 评论 收藏
分享
3 收藏 评论
分享
牛客网
牛客企业服务