题解 | #自动售货系统#

自动售货系统

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        // while (in.hasNextInt()) { // 注意 while 处理多个 case
        //     int a = in.nextInt();
        //     int b = in.nextInt();
        //     System.out.println(a + b);
        // }
        String s = in.nextLine();
        String[] ss = s.split(";");
        int len = ss.length;
        int[] prices = {2, 3, 4, 5, 8, 6};
        int[] coinTypes = {1, 2, 5, 10};
        int balance = 0;
        List<Good> goods = new ArrayList<>();
        List<Coin> coins = new ArrayList<>();
        for (int i = 0; i < len; i++) {
            String command = ss[i].trim();
            if (command.charAt(0) == 'r') {
                init(command, prices, coinTypes, goods, coins);
                System.out.println("S001:Initialization is successful");
            }
            if (command.charAt(0) == 'p') {
                int type = Integer.parseInt(command.split(" ")[1]);
                int sum12 = 0;
                for (Coin coin : coins) {
                    if (coin.value == 1 || coin.value == 2) {
                        sum12 = sum12 + coin.value * coin.count;
                    }
                }
                boolean hasGood = false;
                for (Good good : goods) {
                    if (good.count != 0) {
                        hasGood = true;
                        break;
                    }
                }
                if (!(type == 1 || type == 2 || type == 5 || type == 10)) {
                    System.out.println("E002:Denomination error");
                } else if (sum12 < type) {
                    System.out.println("E003:Change is not enough, pay fail");
                } else if (!hasGood) {
                    System.out.println("E005:All the goods sold out");
                } else {
                    for (Coin coin : coins) {
                        if (coin.value == type) {
                            coin.count++;
                            break;
                        }
                    }
                    balance = type + balance;
                    System.out.println("S002:Pay success,balance=" + balance);
                }
            }
            if (command.charAt(0) == 'b') {
                String name = command.split(" ")[1];
                boolean exit = false;
                boolean has = false;
                boolean buy = false;
                int curPrice = 0;
                for (Good good : goods) {
                    if (name.equals(good.name)) {
                        exit = true;
                        if (good.count != 0) {
                            has = true;
                        }
                        if (good.price <= balance) {
                            curPrice = good.price;
                            buy = true;
                        }
                        break;
                    }
                }
                if (!exit) {
                    System.out.println("E006:Goods does not exist");
                } else {
                    if (!has) {
                        System.out.print("E007:The goods sold out");
                    } else {
                        if (!buy) {
                            System.out.println("E008:Lack of balance");
                        } else {
                            balance = balance - curPrice;
                            System.out.println("S003:Buy success,balance=" + balance);
                        }
                    }
                }
            }
            if (command.charAt(0) == 'c') {
                String[] items = new String[4];
                if (balance == 0) {
                    System.out.println("E009:Work failure");
                } else {
                    for (int j = 3; j >= 0; j--) {
                        if (coins.get(j).count >= balance / coinTypes[j]) {
                            coins.get(j).count = coins.get(j).count - balance / coinTypes[j];
                            items[j] = coinTypes[j] + " yuan coin number=" + (balance / coinTypes[j]);
                            balance = balance - (balance / coinTypes[j]) * coinTypes[j];
                        } else {
                            items[j] = coinTypes[j] + " yuan coin number=" + coins.get(j).count;
                            balance = balance - (coins.get(j).count * coinTypes[j]);
                            coins.get(j).count = 0;
                        }
                    }
                    for (int j = 0; j < items.length; j++) {
                        System.out.println(items[j]);
                    }
                }
            }
            if (command.charAt(0) == 'q') {
                if (!command.contains(" ")) {
                    System.out.println("E010:Parameter error");
                } else {
                    try {
                        int n = Integer.parseInt(command.split(" ")[1]);
                        if (n == 0) {
                            Collections.sort(goods);
                            for (int j = 0; j < goods.size(); j++) {
                                Good good = goods.get(j);
                                System.out.println(good.name + " " + good.price + " " + good.count);
                            }
                        } else if (n == 1) {
                            for (int j = 0; j < 4; j++) {
                                Coin coin = coins.get(j);
                                System.out.println(coin.value + " yuan coin number=" + coin.count);
                            }
                        } else {
                            System.out.println("E010:Parameter error");
                        }
                    } catch (NumberFormatException e) {
                        System.out.println("E010:Parameter error");
                    }
                }
            }
        }
    }
    private static void init(String command, int[] prices, int[] coinTypes,
                             List<Good> goods, List<Coin> coins) {
        String goodsStr = command.split(" ")[1];
        String coinsStr = command.split(" ")[2];
        String[] goodsCount = goodsStr.split("-");
        String[] coinsCount = coinsStr.split("-");
        for (int i = 0; i < goodsCount.length; i++) {
            int count = Integer.parseInt(goodsCount[i]);
            Good good = new Good("A" + (i + 1), prices[i], count);
            goods.add(good);
        }
        for (int i = 0; i < coinsCount.length; i++) {
            int count = Integer.parseInt(coinsCount[i]);
            Coin coin = new Coin(coinTypes[i], count);
            coins.add(coin);
        }
    }
}

class Coin {
    int value;
    int count;
    Coin (int value, int count) {
        this.value = value;
        this.count = count;
    }
}

class Good implements Comparable<Good> {
    String name;
    int price;
    int count;
    Good (String name, int price, int count) {
        this.name = name;
        this.price = price;
        this.count = count;
    }
    public int compareTo(Good good) {
        return this.count - good.count;
    }
}

全部评论

相关推荐

头像
不愿透露姓名的神秘牛友
05-24 12:16
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务