| Author |
Need help..Please! How to keep a running total...
|
Rose Evans
Ranch Hand
Joined: Oct 14, 2004
Posts: 195
|
|
You guys I cannot figure this out. Please, if you have time, give me some more advice. I've got to ask the user if he wants to purchase more items, and keep a running total. I just can't seem to figure out how to do this. Any suggestions would be greatly appreciated.. Here's my code so far.... public class groceryStore2 { private String name; private int groupCount; private double groupPrice; private int numberBought; //char answer; 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); } // 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;
|
) ) ) )
|
 |
Elouise Kivineva
Ranch Hand
Joined: Feb 07, 2002
Posts: 154
|
|
I would think you can calculate the total number of items purchased. You don't need to ask the customer. You also need to rethink your input checking to cut out the redundant code and simplify the procedure. You need a loop that handles a customer making multiple purchases. Inside this loop Create booleans for both input items (number and price, something like amountOK = false and priceOK = false) Create a while loop that runs as long as both booleans are false Inside the loop Give the instructions Read the input Check the input Set the booleans according to whether your input is OK Once you get throug the loop your input is in hand and OK. Now add amount (the amount from this time around) to your running total for all items purchased. Reset all variables for the next iteration of the outer loop!
|
 |
Rose Evans
Ranch Hand
Joined: Oct 14, 2004
Posts: 195
|
|
Elouise, Thank you for your advice, I'll give it a whirl and see what happens.
|
 |
 |
|
|
subject: Need help..Please! How to keep a running total...
|
|
|