I'm using a loop to get user input and compare it; Idk why but on the first iteration of the while it works fine and displays only once. Second run thru I get two prompts, hoping someone can point out where I'm screwing up.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Creates instance of Store and populates it with cakes and cookies
Store bakery = fillInventory();
// Creates instance of Customer and asks user to provide input
Customer ourCustomer = new Customer("TheCustomer", 5.00);
Cake theCake = null;
Cookie theCookie = null;
boolean validChoice = false;
boolean validQuantity = false;
String userChoice = null;
String cakeOrCookie = "";
int userQuantity = 0;
System.out.print("Welcome to the Bakery!\n\n");
while (ourCustomer.getCustomerMoney() > 0.25 ) {
System.out.print("Would you like a cake or cookie?(-1 to quit): ");
cakeOrCookie = input.nextLine();
if (cakeOrCookie.equalsIgnoreCase("cake")) {
System.out.print("Here is our cake selection:\n");
bakery.cakeShop();
validChoice = false;
System.out.print("Please select one: ");
userChoice = input.nextLine();
validChoice = bakery.validCake(userChoice);
while (validChoice == false) {
System.out.print("I didn't understand your selection, please re-renter: ");
userChoice = input.nextLine();
validChoice = bakery.validCake(userChoice);
}
validQuantity = false;
System.out.print("How many would you like?: ");
userQuantity = input.nextInt();
validQuantity = bakery.checkCake(userChoice, userQuantity);
while (validQuantity == false) {
System.out.print("I'm sorry we can't allow that many, please re-enter: ");
userQuantity = input.nextInt();
validQuantity = bakery.checkCake(userChoice, userQuantity);
}
if (validChoice == true && validQuantity == true) {
for (Cake index : bakery.cakeList) {
if (userChoice.equalsIgnoreCase(index.getCakeName())) {
theCake = index;
}
}
}
bakery.buy(ourCustomer, theCake, userQuantity);
bakery.printReceipt();
} else if (cakeOrCookie.equalsIgnoreCase("cookie")) {
System.out.print("Here is our cookie selection:\n");
bakery.cookieShop();
validChoice = false;
System.out.print("Please select one: ");
userChoice = input.nextLine();
validChoice = bakery.validCookie(userChoice);
while (validChoice == false) {
System.out.print("I didn't understand your selection, please re-renter: ");
userChoice = input.nextLine();
validChoice = bakery.validCookie(userChoice);
}
validQuantity = false;
System.out.print("How many would you like?: ");
userQuantity = input.nextInt();
validQuantity = bakery.checkCookie(userChoice, userQuantity);
while (validQuantity == false) {
System.out.print("I'm sorry we can't allow that many, please re-enter: ");
userQuantity = input.nextInt();
validQuantity = bakery.checkCookie(userChoice, userQuantity);
}
if (validChoice == true && validQuantity == true) {
for (Cookie index : bakery.cookieList) {
if (userChoice.equalsIgnoreCase(index.getCookieName())) {
theCookie = index;
}
}
}
bakery.buy(ourCustomer, theCookie, userQuantity);
bakery.printReceipt();
} else if ( cakeOrCookie.equalsIgnoreCase("-1") ) {
break;
}
}
// once the customer is broke and can't afford anything thank them and print final receipt
System.out.print("Thanks for shopping! Come back when you have more money!\n");
System.out.print("Total receipt:\n");
}
public static Store fillInventory() {
Store bakery = new Store();
bakery.createCake("Cake 1", "Flavor 1", 0.50, 15);
bakery.createCake("Cake 2", "Flavor 2", 0.50, 15);
bakery.createCake("Cake 3", "Flavor 3", 0.50, 15);
bakery.createCookie("Cookie 1", "Flavor 1", 0.25, 15);
bakery.createCookie("Cookie 2", "Flavor 2", 0.25, 15);
bakery.createCookie("Cookie 3", "Flavor 3", 0.25, 15);
return bakery;
}
}
The code isn't the cleanest but I'm teaching myself so give me a break lol