var credit = 10;
function Item(name) {
this.name = "";
}
function Vending_item(name,price,id) {
Item.call(this,name);
this.price = price;
this.id = id;
}
Vending_item.prototype = new Item();
Vending_item.prototype.constructor = Vending_item;
var Crisps = new Vending_item("Walkers Ready Salted Crisps",2,01);
var Fizzy1 = new Vending_item("Coca-Cola",1.75,12);
var ChocolateBar = new Vending_item("Dairy Milk",1.25,23);
var ChocolateBar2 = new Vending_item("Twix",34);
var Water = new Vending_item("Buxton Spring Water",1.50,45);
var Fizzy2 = new Vending_item("Fanta Orange",56);
var JumboChocolate = new Vending_item("Double Twix",3,67);
const readline = require('readline');
const purchaseItem = readline.createInterface({input : process.stdin,output : process.stdout});
purchaseItem.question('Please input the ID of the item you would like to Purchase\n',(userInput)=>{
if(userInput.trim() == Crisps.id){
credit = credit - Crisps.price;
console.log("You now have £" + credit + " to spend");
purchaseItem.question('Would you like to refund this item?\n',(userInput)=>{
if(userInput.trim() == 'Yes'){
credit = credit + Crisps.price;
console.log("You now have £" + credit + " to spend")
purchaseItem.setPrompt('Would you like to purchase another item?');
purchaseItem.prompt();
purchaseItem.on('line',(userInput)=>{
if(userInput.trim() == 'Yes'){
purchaseItem.setPrompt("Please input another item ID\n");
purchaseItem.prompt();
}else
purchaseItem.close();
});
}
});
}
});
purchaseItem.on('close',()=>{
console.log('Thanks for using my vending machine');
});
因此,我建立了一个基本的对象类,有一个变量“ credit”,然后有了我的实际对象。我要求用户输入对象“ crisps”的ID,然后输入该对象应有机会退还该物品,如果是,他们将退还款项,如果没有,则将询问他们是否他们想购买另一种物品。
问题出在这里,当被问到是否要购买另一件商品时,我希望我的程序将其返回到最初的问题:“请输入要购买的商品的ID”,但是我不知道怎么办。
任何帮助将不胜感激。