import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static List<Product> productList = new ArrayList<>(); public static HashMap<Integer, Integer> moneyMap = new HashMap<>(); public static int balance = 0; public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); String[] strings = str.split(";"); for (String s : strings) { if (s.contains("r")) {//s为初始化指令 initialization(s.substring(2)); } else if (s.contains("p")) { //s为投币指令 putCoin(s.substring(2)); } else if (s.contains("b")) { //s为购买商品指令 buyProduct(s.substring(2)); } else if (s.contains("c")) { //s为退币指令 refunds(); } else if (s.contains("q")) { //s为查询指令 query(s.substring(2)); } } } /** * 商品类 */ public static class Product { private String name; private int price; private int num; public Product() { } public Product(String name, int price, int num) { this.name = name; this.price = price; this.num = num; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } } /** * 商品及钱币初始化 */ public static void initialization(String data) { // System.out.println(data); String[] str = data.split(" "); String[] productNums = str[0].split("-"); String[] moneyNums = str[1].split("-"); Product product = new Product("A1", 2, Integer.parseInt(productNums[0])); productList.add(product); Product product1 = new Product("A2", 3, Integer.parseInt(productNums[1])); productList.add(product1); Product product2 = new Product("A3", 4, Integer.parseInt(productNums[2])); productList.add(product2); Product product3 = new Product("A4", 5, Integer.parseInt(productNums[3])); productList.add(product3); Product product4 = new Product("A5", 8, Integer.parseInt(productNums[4])); productList.add(product4); Product product5 = new Product("A6", 6, Integer.parseInt(productNums[5])); productList.add(product5); moneyMap.put(10, Integer.parseInt(moneyNums[3])); moneyMap.put(5, Integer.parseInt(moneyNums[2])); moneyMap.put(2, Integer.parseInt(moneyNums[1])); moneyMap.put(1, Integer.parseInt(moneyNums[0])); System.out.println("S001:Initialization is successful"); } /** * 投币功能实现 */ public static void putCoin(String data) { int putCoinValue = Integer.parseInt(data); if (putCoinValue == 1 || putCoinValue == 2 || putCoinValue == 5 || putCoinValue == 10) { if (putCoinValue == 1 || putCoinValue == 2) { balance += putCoinValue; moneyMap.replace(putCoinValue, moneyMap.get(putCoinValue) + 1); System.out.println("S002:Pay success,balance=" + balance); } else { //投入币为5元或10元 if (getOneAndTwo() < putCoinValue) { System.out.println("E003:Change is not enough, pay fail"); } else { balance += putCoinValue; moneyMap.replace(putCoinValue, moneyMap.get(putCoinValue) + 1); System.out.println("S002:Pay success,balance=" + balance); } } } else { System.out.println("E002:Denomination error"); } int count = 0; for (Product product : productList) { if (product.getNum() == 0) { count++; } } if (count == 6) { //所有货品数量全部为0,卖光了 System.out.println("E005:All the goods sold out"); } } /** * 获取存钱盒里1元和2元币的金额总和 */ public static int getOneAndTwo() { int sum = 0; if (!moneyMap.isEmpty()) { sum += moneyMap.get(1); sum += 2 * moneyMap.get(2); } return sum; } /** * 购买商品功能实现 * * @param data */ public static void buyProduct(String data) { int count = 0; Product p = new Product(); for (Product product : productList) { if (!data.equals(product.getName())) { count++; } else { p = product; } } if (count == 6) { //和6个商品名字都不匹配 System.out.println("E006:Goods does not exist"); } else { if (p.getNum() == 0) { System.out.println("E007:The goods sold out"); } else { if (balance < p.getPrice()) { System.out.println("E008:Lack of balance"); } else { //购买成功 balance -= p.getPrice(); //扣除投币余额 p.setNum(p.getNum() - 1); //对应商品数量-1 System.out.println("S003:Buy success,balance=" + balance); } } } } /** * 退币功能实现 */ public static void refunds() { if (balance == 0) { System.out.println("E009:Work failure"); } else { //退币 Map<Integer, Integer> refundMap; refundMap = charge(balance); int[] a = new int[4]; if (refundMap.size() != 4) { for (int i : refundMap.keySet()) { if (i == 1) { a[0]++; } else if (i == 2) { a[1]++; } else if (i == 5) { a[2]++; } else { a[3]++; } } if (a[0] == 0) { refundMap.put(1, 0); } if (a[1] == 0) { refundMap.put(2, 0); } if (a[2] == 0) { refundMap.put(5, 0); } if (a[3] == 0) { refundMap.put(10, 0); } } for (int i : refundMap.keySet()) { System.out.println(i + " yuan coin number=" + refundMap.get(i)); } } } /** * 查询功能的实现 * * @param data */ public static void query(String data) { if ("0".equals(data)) { // 查询商品信息 for (Product product : productList) { System.out.println(product.getName() + " " + product.getPrice() + " " + product.getNum()); } } else if ("1".equals(data)) { //查询存钱盒信息 for (int i : moneyMap.keySet()) { System.out.println(i + " yuan coin number=" + moneyMap.get(i)); } } else { //查询参数有误 System.out.println("E010:Parameter error"); } } public static Map<Integer, Integer> charge(int balance1) { Map<Integer, Integer> map = new HashMap<>(); int remainAmount = balance1; Map<Integer, Integer> sortedCoins = new TreeMap<>(Comparator.reverseOrder()); sortedCoins.putAll(moneyMap); for (int coin : sortedCoins.keySet()) { int count = sortedCoins.get(coin); if (count > 0) { int numCoin = Math.min(remainAmount / coin, count); if (numCoin > 0) { map.put(coin, numCoin); remainAmount -= coin * numCoin; } } } if (remainAmount == 0) { //找零成功 for (int coin : map.keySet()) { moneyMap.put(coin, moneyMap.get(coin) - map.get( coin));//扣除存钱罐中对应币 balance = 0; //投币余额置为0 } return map; } else { //找零失败,尽最大可能弥补用户损失 for (int i = balance1 - 1; i > 0; i--) { map = charge(i); if (!map.isEmpty()) { return map; } } } return map; } }