题解 | #自动售货系统#

自动售货系统

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

import java.util.Scanner;
import java.util.HashMap;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    private static HashMap<String, Integer> goods = new
    HashMap<String, Integer>();//商品A1-A6的数量
    private static HashMap<String, Integer> prices = new
    HashMap<String, Integer>();//商品A1-A6的价格
    private static HashMap<Integer, Integer> coins = new
    HashMap<Integer, Integer>();//存钱盒中各面额钱币(1元、2元、5元、10元)的数量
    private static Integer change_balance = 0;//零钱余额
    private static Integer user_balance = 0;//投币余额
    private static Integer goods_count = 0;//商品数量
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String nextLine = in.nextLine();
            String[] orders = nextLine.split(";");
            for (int i = 0; i < orders.length; i++) {
                String order = orders[i].substring(0, 1);
                switch (order) {
                    case "r" :
                        init(orders[i]);
                        break;//系统初始化
                    case "p" :
                        System.out.println(coinsIn(orders[i]));
                        break;//投币
                    case "b" :
                        System.out.println(purchase(orders[i]));
                        break;//购买
                    case "c" :
                        coinsOut();
                        break;//退币
                    case "q" :
                        query(orders[i]);
                        break;//查询
                    default :
                        return;
                }
            }

        }
    }

    /**
     * 系统初始化
     * @param initialParam
     */
    public static void init(String initialParam) {
        String[] params = initialParam.split(" ");
        System.out.println(OutPut.S001.getDesc());
        String[] nums1 = params[1].split("-");
        goods.put("A1", Integer.parseInt(nums1[0]));
        goods.put("A2", Integer.parseInt(nums1[1]));
        goods.put("A3", Integer.parseInt(nums1[2]));
        goods.put("A4", Integer.parseInt(nums1[3]));
        goods.put("A5", Integer.parseInt(nums1[4]));
        goods.put("A6", Integer.parseInt(nums1[5]));
        prices.put("A1", 2);
        prices.put("A2", 3);
        prices.put("A3", 4);
        prices.put("A4", 5);
        prices.put("A5", 8);
        prices.put("A6", 6);
        String[] nums2 = params[2].split("-");
        coins.put(1, Integer.parseInt(nums2[0]));
        coins.put(2, Integer.parseInt(nums2[1]));
        coins.put(5, Integer.parseInt(nums2[2]));
        coins.put(10, Integer.parseInt(nums2[3]));
        goods_count = goods.entrySet().stream().mapToInt(e ->
                      e.getValue()).sum();//计算商品总数
        change_balance = 1 * coins.get(1) + 2 * coins.get(2);//零钱余额
    }

    /**
     * 投币
     * @param order 投币指令
     */
    public static String coinsIn(String order) {
        String amount = order.substring(order.indexOf(" ") + 1);
        if ("" != amount && amount.length() > 0) {
            int mi = Integer.parseInt(amount);

            if (mi == 5 || mi == 10) {
                if (mi > change_balance) {
                    // 零钱不足,无法找零,不能投币
                    return OutPut.E003.getDesc();
                } else if (goods_count == 0) {
                    // 商品售罄,投币失败
                    return OutPut.E005.getDesc();
                } else {
                    // 投币成功,钱币数量+1
                    coins.put(mi, coins.get(mi) + 1);
                }
            } else if (mi == 1 || mi == 2) {
                if (goods_count == 0) {
                    // 商品全部售罄,投币失败
                    return OutPut.E005.getDesc();
                } else {
                    change_balance += mi;//计入零钱余额
                    coins.put(mi, coins.get(mi) + 1);//投币成功,钱币数量+1
                }
            } else {
                // 无效的面额
                return OutPut.E002.getDesc();
            }
            user_balance += mi;//投币成功,计入用户投币余额
            return OutPut.S002.getDesc() + user_balance;
        }
        return OutPut.E002.getDesc();
    }

    /**
     * 退币
     */
    public static void coinsOut() {
        int last = user_balance;//剩余未退余额
        if (last == 0) {
            //投币余额为0
            System.out.println(OutPut.E009.getDesc());
        } else {
            int one = 0, two = 0, five = 0, ten = 0;//四种面值退的张数
            while (last > 0) {
                int temp = last / 10;//若要最少总张数,从最大面值开始退起,当前面值最多应退的张数,实际退的张数要取决于存钱盒里存有的张数
                while (coins.get(10) > 0 && temp > 0) {
                    ten++;
                    temp--;
                    last -= 10;
                    coins.put(10, coins.get(10) - 1);
                }
                temp = last / 5;
                while (coins.get(5) > 0 && temp > 0) {
                    five++;
                    temp--;
                    last -= 5;
                    coins.put(5, coins.get(5) - 1);
                }
                temp = last / 2;
                while (coins.get(2) > 0 && temp > 0) {
                    two++;
                    temp--;
                    last -= 2;
                    change_balance -= 2;
                    coins.put(2, coins.get(2) - 1);
                }
                temp = last;
                while (coins.get(1) > 0 && temp > 0) {
                    one++;
                    temp--;
                    last -= 1;
                    change_balance--;
                    coins.put(1, coins.get(1) - 1);
                }
            }
            System.out.println("1 yuan coin number=" + one);
            System.out.println("2 yuan coin number=" + two);
            System.out.println("5 yuan coin number=" + five);
            System.out.println("10 yuan coin number=" + ten);
            user_balance = 0;//找零结束,余额清零
        }
    }

    /**
     * 购买商品
     * @param order 购买指令
     */
    public static String purchase(String order) {
        String goodsName = order.substring(order.indexOf(" ") + 1);
        if (goods.containsKey(goodsName)) {
            if (goods.get(goodsName) == 0) {
                // 商品售罄
                return OutPut.E007.getDesc();
            } else if (prices.get(goodsName) > user_balance) {
                // 余额不足,购买失败
                return OutPut.E008.getDesc();
            } else {
                user_balance -= prices.get(goodsName);// 购买成功,投币余额扣减
                return OutPut.S003.getDesc() + user_balance;
            }
        } else {
            // 商品不存在
            return OutPut.E006.getDesc();
        }
    }

    /**
     * 查询
     * @param order 查询指令
     */
    public static void query(String order) {
        if (order.contains(" ") && order.length() == 3) {
            String type = order.substring(2);
            if ("0".equals(type)) {
                //查询商品信息
                goods.entrySet().forEach(e ->
                                         System.out.println(e.getKey() + " " + prices.get(e.getKey()) + " " +
                                                 e.getValue()));
            } else if ("1".equals(type)) {
                //查询存钱盒信息
                coins.entrySet().forEach(e ->
                                         System.out.println(e.getKey() + " yuan coin number=" + e.getValue()));
            } else {
                System.out.println(OutPut.E010.getDesc());
            }
        } else {
            System.out.println(OutPut.E010.getDesc());
        }
    }
}

/**
 *返回状态码 常量
 */
enum OutPut {
    S001(":Initialization is successful"),
    S002(":Pay success,balance="),
    S003(":Buy success,balance="),
    E002(":Denomination error"),
    E003(":Change is not enough, pay fail"),
    E005(":All the goods sold out"),
    E006(":Goods does not exist"),
    E007(":The goods sold out"),
    E008(":Lack of balance"),
    E009(":Work failure"),
    E010(":Parameter error");

    private String desc;
    private OutPut(String desc) {
        this.desc = desc;
    }
    public String getDesc() {
        return name() + desc;
    }

}

全部评论

相关推荐

投递腾讯等公司7个岗位
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务