所以除了一些小问题之外,我在这个项目上遇到了一些麻烦,这个问题继续让我感到困惑。我需要做的是这台自动售货机测试用户输入机器的金额,看它是否足以购买用户想要的物品。如果还不够,则需要提示用户添加更多钱或按负数退出。
示例:
Please Enter money into the machine (press -1 to exit): <enter such and such amount>
You now have $1.00. Please select and item (press 0 to exit): <enter selection>
You do not have enough money. Please add more money or exit the machine.
Please Enter money into the machine (press -1 to exit): <enter such and such amount>
You now have $2.00. Please select and item (press 0 to exit): <enter selection>
我遇到的问题是,在添加更多资金打印后,我无法获得金额,而我需要运行程序的网站,当我到达addMoney扫描程序时,我的课程会抛出NoSuchElementException。我无法弄清楚为什么在这一点输入双重时它会这样做。任何帮助深表感谢。我确定有一种更简单的方法吗?
VendingMachine.java
//Runs user inputs and determines the success of transaction
public void vend(double inputMoney,int inputItem) throws NumberFormatException, InputMismatchException {
NumberFormat d = NumberFormat.getCurrencyInstance();
Scanner input = new Scanner(System.in);
boolean a = false;
boolean b = false;
boolean c = false;
double userMoney = inputMoney;
int errorNum = 0;
//double addMoney = 0;
int itemSelect = inputItem;
if (userMoney==-4){
//errorNum=-4;
//this.outputMessage(userInput, errorNum);
a = true;
b=true;
c=true;
}
while(!a){
b=false;
while(!b){
if(itemSelect==0){
errorNum = -3;
this.outputMessage(userMoney, errorNum);
b=true;
a=true;
} else if ((userMoney-stock[itemSelect-1].itemPrice)<0.00){
errorNum=-1;
this.outputMessage(userMoney, errorNum);
double newMoney=this.addMoney(userMoney);
if(newMoney==-1){
errorNum=-3;
this.outputMessage(userMoney, errorNum);
b=true;
a=true;
}
userMoney = userMoney + newMoney;
b=true;
} else if (stock[itemSelect-1].itemQuantity==0){
errorNum = -2;
this.outputMessage(userMoney, errorNum);
b=true;
a=true;
} else {
userMoney = userMoney-stock[itemSelect-1].itemPrice;
money = (money + stock[itemSelect-1].itemPrice);
this.outputMessage(userMoney, itemSelect);
stock[itemSelect-1].itemQuantity = stock[itemSelect-1].itemQuantity - 1;
b=true;
a=true;
}
}
}
}
//Prints out appropriate messages based on success of transaction
public void outputMessage(double userInput, int userInput2) {
NumberFormat d = NumberFormat.getCurrencyInstance();
boolean exit = false;
while(!exit){
if(userInput2==-1){
System.out.println("You do not have enough money. Please add more money or exit.");
System.out.println("Please enter some money into the machine (enter -1 to exit): ");
exit=true;
} else if (userInput2==-2) {
System.out.println("Sorry, we are out of this item.");
exit=true;
} else if (userInput2==-3) {
System.out.println("You did not buy anything from this vending machine. Your change is " + d.format(userInput));
exit=true;
} else {
System.out.println("You have purchased " + stock[userInput2-1].itemDesc + " for " + d.format(stock[userInput2-1].itemPrice) + ". Your change is " + d.format(userInput) + ".");
exit=true;
}
}
}
//To print the items in held in stock
public void printMenu() {
NumberFormat price = NumberFormat.getCurrencyInstance();
System.out.println("Item#" + "\t" + "Item" + "\t " + "Price" + "\t " + "Qty");
for(int i=0; i<stock.length;i++){
System.out.println((i+1) + "\t" + stock[i].itemDesc + "\t " + price.format(stock[i].itemPrice) + "\t " + stock[i].itemQuantity);
}
}
public double getMoney() {
return money;
}
public double addMoney(double userMoney){
Scanner scan = new Scanner(System.in);
double addMoney = scan.nextDouble();
double newMoney = userMoney +addMoney;
return newMoney;
}
VendingMachineDriver.java
public static void main(String args[]) throws FileNotFoundException {
Scanner input = new Scanner(System.in);
String a = new String("a");
String b = new String("b");
String x = new String("x");
int item = 0;
boolean exit = false;
VendingMachine drinks = new VendingMachine("src/drinks");
VendingMachine snacks = new VendingMachine("src/snacks");
NumberFormat cash = NumberFormat.getCurrencyInstance();
while(!exit){
boolean c = false;
boolean i = false;
System.out.println("Welcome to Jeremy's Super Vending Machines!");
System.out.println("Please select a vending machine:");
System.out.println("A-Drinks, B-Snacks, X-Exit");
String vendingSelect = input.next();
while(!c){
if(a.equalsIgnoreCase(vendingSelect)){
drinks.printMenu();
System.out.println("Please enter some money into the machine (enter -1 to exit)");
double money = input.nextDouble();
if (money==-1){
int errorNum=-3;
drinks.outputMessage(money, errorNum);
System.out.println("You did not buy anything from this vending machine. Your change is " + cash.format(0) + "." );
c = true;
} else {
while(!i){
try{
System.out.println("You now have " + cash.format(money) + " to spend. Please make a selection (enter 0 to exit): ");
item = input.nextInt();
drinks.vend(money, item);
i=true;
} catch(InputMismatchException e) {
System.out.println("Invalid Entry!");
input.next();
}
}
//drinks.vend(money, item);
}
c=true;
}
else if(b.equalsIgnoreCase(vendingSelect)){
snacks.printMenu();
System.out.println("Please enter some money into the machine (enter -1 to exit)");
double money = input.nextDouble();
if (money==-1){
System.out.println("You did not buy anything from this vending machine. Your change is " + cash.format(0) + "." );
c = true;
}
else {
while(!i){
try{
System.out.println("You now have " + cash.format(money) + " to spend. Please make a selection (enter 0 to exit): ");
item = input.nextInt();
i=true;
} catch(InputMismatchException e) {
System.out.println("Invalid Entry!");
input.next();
}
}
snacks.vend(money, item);
c=true;
}
} else if(x.equalsIgnoreCase(vendingSelect)){
double errorNum = -4;
snacks.vend(errorNum, item);
double revenue = drinks.getMoney() + snacks.getMoney();
System.out.println("The vending machines made a total of " + cash.format(revenue) + "." );
System.out.println("Thank you for your business!");
c=true;
exit=true;
}
}
}