Connor Lee
2013-10-10 14:52:23 UTC
Here is the expected output:
Please enter how many items you have: 1
Enter the cost of item #1: 1
Enter the number of item #1: 1
total ==> $1.00
Do you have anymore items? (y/n): y
Enter the cost of item #2: 1
Enter the number of item #2: 1
total ==> $1.00
Do you have anymore items? (y/n): n
Thank you!
Your total food bill ==> $2.00
---------------------------------------------------------------------------------
Here is an example of what happens:
Please enter how many items you have: 1
Enter the cost of item #1: 1
Enter the number of item #1: 1
total ==> $1.00
Do you have anymore items? (y/n):
Do you have anymore items? (y/n): y
Enter the cost of item #2: 1
Enter the number of item #2: 1
total ==> $1.00
Do you have anymore items? (y/n):
Do you have anymore items? (y/n): n
Thank you!
Your total food bill ==> $2.00
----------------------------------------------------------------------------
Here is the GroceryListDriver class (provided by the teacher)
public class GroceryListDriver
{
public static void main( String args[] )
{
GroceryList foodForTheWeek = new GroceryList();
foodForTheWeek.shop();
System.out.print( "\n\n" );
System.out.printf("%35s $%-10.2f", "Your total food bill ==> ", foodForTheWeek.getTotal() );
}
}
---------------------------------------------------------------------------
Here is my GroceryList class
import java.util.Scanner;
public class GroceryList
{
private double totalCost;
public GroceryList()
{
}
public void shop()
{
Scanner inputScanner = new Scanner(System.in);
System.out.print("Please enter how many items you have: ");
int numItems = 0;
numItems = inputScanner.nextInt();
for(int i =0; i< numItems; i++)
{
System.out.print("Enter the cost of item #" + (i+1) + ": ");
double a = inputScanner.nextDouble();
System.out.print("Enter the number of item #" + (i+1) + ": ");
double b = inputScanner.nextInt();
System.out.print("\n" + "\t\t\t\t total ==> ");
System.out.printf("$%.2f", (a*b));
totalCost+=(a*b);
System.out.println();
if(numItems == (i+1))
{
int done = 0;
String anyMore = "duh";
do
{
//String test = inputScanner.nextLine(); <--- This line fixes the problem, but I feel like there is a more proper way to fix it.
System.out.print("\n" + "Do you have anymore items? (y/n): ");
anyMore = inputScanner.nextLine();
switch(anyMore)
{
case "y": case "Y":
numItems++;
done=1;
break;
case "n": case "N":
System.out.println("\n" + "Thank you!");
done=1;
break;
case "": //I added this to attempt to fix the problem
break; //assuming .nextLine() was returning nothing,
//but it doesn't do anything.
default:
break;
}
}while(done==0);
}
}
}
public double getTotal()
{
return totalCost;
}
}
---------------------------------------------------------------------
To me it seems like after hitting the return key to submit the last item's quantity, .nextLine() picks up the same keystroke and returns nothing. Adding the extra .nextLine() for a random variable seemed to patch that up, but I feel like there should be a better way to avoid this issue.