Hi guys. I'm at it again. I need a little advice from you if you can help.
I have written a program, but I need to be able to ask a user if he/she wants to add more items to their shopping cart, and I need to keep a running total until they do not want anymore items. Would you guys know how to do this? Here is my code so far...
____________________________________________________________
public class groceryStore2
{
private
String name;
private int groupCount;
private double groupPrice;
private int numberBought;
public void setName(String newName)
{
name = newName;
}
//Sets price to count pieces for $costForCount.
//For example, 2 for $1.99.
public void setPrice(int count, double costForCount)
{
if ((count <= 0) || (costForCount <=0))
{
System.out.println("Error: Bad parameter in setPrice.");
System.exit(0);
}
else
{
groupCount = count;
groupPrice = costForCount;
}
}
public void setNumberBought(int number)
{
if (number <= 0)
{
System.out.println("Error: Bad parameter in setNumberBought.");
System.exit(0);
}
else
numberBought = number;
}
//Gets price and number being purchased from keyboard.
public void readInput()
{
System.out.println("Enter name of item you are purchasing: ");
name = SavitchIn.readLine();
System.out.println("Enter price of item on two lines. ");
System.out.println("For example, 3 for $2.99 is entered as");
System.out.println("3");
System.out.println("2.99");
System.out.println("Enter price of item on two lines, now:");
groupCount = SavitchIn.readLineInt();
groupPrice = SavitchIn.readLineDouble();
while ((groupCount <= 0) || (groupPrice <=0))
{ //Try again
System.out.println("Both numbers must be positive. Try again.");
System.out.println("Enter price of item on two lines.");
System.out.println("For example, 3 for $2.99 is entered as");
System.out.println("3");
System.out.println("2.99");
System.out.println("Enter price of item on two lines, now");
groupCount = SavitchIn.readLineInt();
groupPrice = SavitchIn.readLineDouble();
} // ends while loop
System.out.println("Enter number of items purchased:");
numberBought = SavitchIn.readLineInt();
while (numberBought <= 0)
{ //Try again
System.out.println("Number must be positive. Try again.");
System.out.println("Enter number of items purchased:");
numberBought = SavitchIn.readLineInt();
}
setPrice(groupCount,groupPrice);
setNumberBought(numberBought);
getUnitCost();
double TotalCost;
TotalCost=getTotalCost();
writeOutput();
System.out.println("Price for "+name+" is "+ TotalCost);
/* char answer;
answer = SavitchIn.readLineNonwhiteChar();
if (answer == 'y')
System.out.println("Do you want to buy another item? ");
System.out.println("Press Enter key to end program.");
String junk;
junk = SavitchIn.readLine();
*/
} // ends readInput method
//Outputs price and number being purchased to screen.
public void writeOutput()
{
System.out.println(numberBought + " " + name);
System.out.println("at " + groupCount + " for $" + groupPrice);
}
public String getName()
{
return name;
}
public double getTotalCost()
{
return ((groupPrice/groupCount)*numberBought);
}
public double getUnitCost()
{
return (groupPrice/groupCount);
}
public int getNumberBought()
{
return numberBought;
}
}
_______________________________________________________________________
My code didn't paste too well on here, but hopefully, you can read it. I'd appreciate any information you could give me.
