Reputation: 13
So I am writing a basic fast food ordering program and I am having trouble with either one of my loops or an error in returning one of my variables (so I think). My code runs but when I get to the end and i do not want to enter in another order, it asks me again and then displays the entire food menu, and after that output comes up it allows me to end the program. I will post half of code below where I believe the issue lies. I believe it has to also do with the userChoice variable.
package lab08;
import java.util.Scanner;
import java.io.*;
import java.text.DecimalFormat;
public class Lab08 {
public static void main(String[] args) throws IOException {
String mealName[];
double mealPrice[];
mealName = new String[3];
mealPrice = new double[3];
LoadArraysFromFile(mealName, mealPrice);
PerformTask(mealName, mealPrice);
}
//Load Arrays From File
public static void LoadArraysFromFile(String mealName[], double
mealPrice[]) throws IOException {
File mealInfoFile;
Scanner mealFileSC;
mealInfoFile = new File("mealInfoFile.txt");
mealFileSC = new Scanner(mealInfoFile);
int cnt = 0;
while (mealFileSC.hasNext() && cnt < 3) {
mealName[cnt] = mealFileSC.nextLine();
mealPrice[cnt] = mealFileSC.nextDouble();
mealFileSC.nextLine();
cnt++;
}
mealFileSC.close();
}
//Process Each Customer
public static void PerformTask(String mealName[], double mealPrice[])
throws IOException {
char userChoice;
userChoice = 'Z';
while (userChoice != 'N') {
userChoice = GetUserChoice();
ProcessEachCustomer(mealName, mealPrice);
}
}
//Get User Choice
public static char GetUserChoice() {
char userChoice;
Scanner kbd = new Scanner(System.in);
System.out.print("Would you like to Order? (Y/N) ");
userChoice = kbd.nextLine().toUpperCase().charAt(0);
while (userChoice != 'Y' && userChoice != 'N') {
System.out.print("Would you like to Order? (Y/N)");
userChoice = kbd.nextLine().toUpperCase().charAt(0);
}
return userChoice;
}
//Process Each Customer
public static void ProcessEachCustomer(String mealName[],
double mealPrice[])throws IOException {
int mealNumber;
double[] mealTotals = new double[3];
int[] quantities = new int[3];
double[] totals;
char userChoice;
userChoice = 'Z';
while (userChoice != 'N') {
ProcessEachMeal(quantities, mealName, mealPrice);
totals = UpdateMealTotals(mealPrice, mealTotals, quantities);
DisplayOrderSummary(mealPrice, mealTotals, quantities, totals,
mealName);
LogTransaction( mealPrice, mealTotals, quantities, totals,
mealName);
userChoice = GetUserChoice();
}
}
//Process Each Meal
public static void ProcessEachMeal(int[] quantities, String
mealName[], double mealPrice[]) {
int mealNumber;
//loop
Scanner kbd = new Scanner(System.in);
DisplayMenu(mealName, mealPrice);
mealNumber = GetMealNumber();
UpdateQuantities(quantities, mealNumber);
}
Upvotes: 1
Views: 60
Reputation: 4187
You would have to call GetUserChoice()
once before starting while-loop
and then after processing order.
public static void PerformTask(String mealName[], double mealPrice[]) throws IOException {
char userChoice;
// ask for first time
userChoice = GetUserChoice();
while (userChoice != 'N') {
ProcessEachCustomer(mealName, mealPrice);
// ask again so if user is done (userChoice='N') while-loop will terminate
userChoice = GetUserChoice();
}
}
UPDATED: All the methods aren't present so I can't run the complete code but here is what you can do to avoid 2 times printing of question.
Below will ensure that you only ask for user choice when performing task and not when processing each customer.
// Process Each Customer
public static void PerformTask(String mealName[], double mealPrice[]) throws IOException {
char userChoice;
userChoice = GetUserChoice();
while (userChoice != 'N') {
ProcessEachCustomer(mealName, mealPrice);
userChoice = GetUserChoice();
}
}
// Get User Choice
public static char GetUserChoice() {
char userChoice;
Scanner kbd = new Scanner(System.in);
do {
System.out.print("Would you like to Order? (Y/N) ");
userChoice = kbd.nextLine().toUpperCase().charAt(0);
} while (userChoice != 'Y' && userChoice != 'N');
return userChoice;
}
// Process Each Customer
public static void ProcessEachCustomer(String mealName[], double mealPrice[]) throws IOException {
int mealNumber;
double[] mealTotals = new double[3];
int[] quantities = new int[3];
double[] totals;
ProcessEachMeal(quantities, mealName, mealPrice);
totals = UpdateMealTotals(mealPrice, mealTotals, quantities);
DisplayOrderSummary(mealPrice, mealTotals, quantities, totals, mealName);
LogTransaction(mealPrice, mealTotals, quantities, totals, mealName);
}
Upvotes: 2