题解 | #自动售货系统#
自动售货系统
https://www.nowcoder.com/practice/cd82dc8a4727404ca5d32fcb487c50bf
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void (async function () { // Write your code here const categories = ["A1", "A2", "A3", "A4", "A5", "A6"]; let commodities; const changes = ["1", "2", "5", "10"]; let moneyBox = []; let balance = 0; const isNullCommodities = () => { for (let categroy of categories) { if (commodities.get(categroy).number !== 0) return false; } return true; }; const initial = (tokens) => { let commoditiesNumber = tokens[1].split("-").map(Number); commodities = new Map([ ["A1", { price: 2, number: commoditiesNumber[0] }], ["A2", { price: 3, number: commoditiesNumber[1] }], ["A3", { price: 4, number: commoditiesNumber[2] }], ["A4", { price: 5, number: commoditiesNumber[3] }], ["A5", { price: 8, number: commoditiesNumber[4] }], ["A6", { price: 6, number: commoditiesNumber[5] }], ]); moneyBox = tokens[2].split("-").map(Number); console.log("S001:Initialization is successful"); }; const pay = (temp) => { if (!changes.includes(temp)) console.log("E002:Denomination error"); else if ( (temp === "5" || temp === "10") && (moneyBox.length === 0 || moneyBox[0] + moneyBox[1] * 2 < parseInt(temp)) ) console.log("E003:Change is not enough, pay fail"); else if (isNullCommodities()) console.log("E005:All the goods sold out"); else { balance += parseInt(temp); switch (temp) { case "1": moneyBox[0]++; break; case "2": moneyBox[1]++; break; case "5": moneyBox[2]++; break; case "10": moneyBox[3]++; break; } console.log(`S002:Pay success,balance=${balance}`); } }; const buy = (temp) => { if (!categories.includes(temp)) console.log("E006:Goods does not exist"); else if (commodities.get(temp).number === 0) console.log("E007:The goods sold out"); else if (balance < commodities.get(temp).price) console.log("E008:Lack of balance"); else { balance -= commodities.get(temp).price; console.log(`S003:Buy success,balance=${balance}`); } }; const refuse = () => { if (balance === 0) { console.log("E009:Work failure"); } else { let dp = new Array(4).fill(0); for (let i = 3; i >= 0; i--) { let change = changes[i]; while (balance >= change && moneyBox[i] > 0) { balance -= change; moneyBox[i]--; dp[i]++; } } for (let i = 0; i < 4; i++) { console.log(changes[i].concat(" yuan coin number=", dp[i])); } } }; const query = (temp) => { if (temp === "0") { for (let i = 0; i < 6; i++) { let str = categories[i].concat( " ", commodities.get(categories[i]).price, " ", commodities.get(categories[i]).number ); console.log(str); } } else if (temp === "1") { for (let i = 0; i < 4; i++) { let str = changes[i].concat(" yuan coin number=", moneyBox[i]); console.log(str); } } else console.log("E010:Parameter error"); }; while ((line = await readline())) { let options = line.split(";"); for (let option of options) { let tokens = option.split(" "); switch (tokens[0]) { case "r": initial(tokens); break; case "p": pay(tokens[1]); break; case "b": buy(tokens[1]); break; case "c": refuse(); break; case "q": query(tokens[1]); break; default: if (tokens[0][0] === "q") { console.log("E010:Parameter error"); } break; } } } })();