没啥好说的,一步步慢慢写
import java.util.*;
public class Main{
// static int[] goods = new int[6];
// static int[] box = new int[4];
static int payment = 0;
// static List<String> goodsName = List.of("A1", "A2", "A3", "A4", "A5", "A6");
static List<String> goodsName = Arrays.asList("A1", "A2", "A3", "A4", "A5",
"A6");
static Map<String, Integer> goodsPrice = new HashMap<>();
static {
goodsPrice.put("A1", 2);
goodsPrice.put("A2", 3);
goodsPrice.put("A3", 4);
goodsPrice.put("A4", 5);
goodsPrice.put("A5", 8);
goodsPrice.put("A6", 6);
}
static int[] boxName = {1, 2, 5, 10};
static Map<String, Integer> goods = new LinkedHashMap<>();
static Map<Integer, Integer> box = new LinkedHashMap<>();
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
String[] commands = input.split("\\;");
//分别处理命令
for (String command : commands) {
//初始化
if (command.startsWith("r")) System.out.println(initialize(command));
//投币
if (command.startsWith("p")) System.out.println(pay(command));
//购买商品
if (command.startsWith("b")) System.out.println(buy(command));
//退币
if (command.startsWith("c")) System.out.println(refund(command));
//查询
if (command.startsWith("q")) System.out.println(query(command));
}
}
//初始化输出
public static String initialize(String command) {
String[] inputs = command.split(" ");
String[] input1 = inputs[1].split("\\-");
String[] input2 = inputs[2].split("\\-");
for (int i = 0; i < goodsName.size(); i++) {
goods.put(goodsName.get(i), Integer.parseInt(input1[i]));
if (i < boxName.length) {
box.put(boxName[i], Integer.parseInt(input2[i]));
}
}
return "S001:Initialization is successful";
}
//投币输出
public static String pay(String command) {
int money = Integer.parseInt(command.split(" ")[1]);
if (!(money == 1 || money == 2 || money == 5 ||
money == 10)) return "E002:Denomination error";
if (((box.get(1) + 2 * box.get(2)) < money) && (money != 1 &&
money != 2)) return "E003:Change is not enough, pay fail";
if (sumGoods() == 0) return "E005:All the goods sold out";
//若成功投币
payment += money;
box.put(money, box.get(money) + 1);
return "S002:Pay success,balance=" + payment + "";
}
//购买商品输出
public static String buy(String command) {
String goodsBuyed = command.split(" ")[1];
if (!goodsName.contains(goodsBuyed)) return "E006:Goods does not exist";
if (goods.get(goodsBuyed) == 0) return "E007:The goods sold out";
if (payment < goodsPrice.get(goodsBuyed)) return "E008:Lack of balance";
//购买商品成功
payment -= goodsPrice.get(goodsBuyed);
goods.put(goodsBuyed, goods.get(goodsBuyed) - 1);
return "S003:Buy success,balance=" + payment + "";
}
//退币
public static String refund(String command) {
if (payment == 0) return "E009:Work failure";
//按照退币原则进行退币
StringBuilder sb = new StringBuilder();
sb.append("1 yuan coin number=").append(payment % 5 % 2).append("\n").
append("2 yuan coin number=").append(payment % 5 / 2).append("\n").
append("5 yuan coin number=").append(payment % 10 / 5).append("\n").
append("10 yuan coin number=").append(payment / 10);
box.put(1,box.get(1)-payment % 5 % 2);
box.put(2,box.get(2)-payment % 5 / 2);
box.put(5,box.get(5)-payment % 10 / 5);
box.put(10,box.get(10)-payment / 10);
payment = 0;
return sb.toString();
}
//查询
public static String query(String command) {
if(command.split(" ").length!=2) return "E010:Parameter error";
String option = command.split(" ")[1];
StringBuilder sb = new StringBuilder();
if (option.equals("0")) {
List<Map.Entry> list = new ArrayList<>(goods.entrySet());
list.sort(new Comparator<Map.Entry>() {
public int compare(Map.Entry e1, Map.Entry e2) {
if (e1.getValue() != e2.getValue()) {
return (int)e2.getValue() - (int)e1.getValue();
} else {
return 0;
}
}
});
for (Map.Entry e : list) {
sb.append(e.getKey() + " " + goodsPrice.get(e.getKey()) + " " + goods.get(
e.getKey()) + " \n");
}
} else if (option.equals("1")) {
sb.append("1 yuan coin number=").append(box.get(1)).append("\n").
append("2 yuan coin number=").append(box.get(2)).append("\n").
append("5 yuan coin number=").append(box.get(5)).append("\n").
append("10 yuan coin number=").append(box.get(10)).append("\n");
} else {
return "E010:Parameter error";
}
sb.delete(sb.length() - 1, sb.length());
return sb.toString();
}
//计算所有商品的剩余数量
public static int sumGoods() {
int sum = 0;
for (int item : goods.values()) {
sum += item;
}
return sum;
}
}