aspose file tools*
The moose likes Beginning Java and the fly likes Need advice - How to add items and keep a running total. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need advice - How to add items and keep a running total." Watch "Need advice - How to add items and keep a running total." New topic
Author

Need advice - How to add items and keep a running total.

Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
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.



) ) ) )
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
try using the code tags to format your code...

To keep a running total simply define a variable which you update to the total of the items in the basket whenever the content of the basket changes.


42
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

If you want to post code and amintain its formatting (which is a Good Idea for sanity of those trying to read it), you can use UBB tags.

Advice on you class:
  • Seperation of concens. This is a good Object Oriented principle, where an object only encapsulates functionality and properties directly represented by it. You have a class which does two things - maintains shopping card data and handle IO. You might want to change this to be a ShoppingCart class which holds your cart data and a GroceryStore class which has your main method and handles the IO.
  • Your set methods will always overwrite the properties you are interested in. So every time you set the price, you lose the old one. If you want to add cost as the number of items bought increases, you'll need to add to the properties, not overwrite them.
  • I don't know if this has been asked for, but there's no methods to remove items from the cart.
  • Having seperate setPrice and setNumberBought methods is perhaps a little dangerous - I know your program is fairly simple, but you are runnign the risk of setting one and forgetting to set the other. You shouldn't be allowed to do this - you might want to think of how you program to avoid that happening.
  • This:


  • is dangerous. What happens if groupCount is zero?

    JavaRanch FAQ HowToAskQuestionsOnJavaRanch
    Rose Evans
    Ranch Hand

    Joined: Oct 14, 2004
    Posts: 195
    Jeroen and Paul,

    Thanks for your advice. I'm still confused on how to do all of that though. Your advice is great, but I am only a very, very, very beginning programmer and it's hard for me to understand exactly what it is I am supposed to do. I know in my 'head' what needs to be done, but coding it sometimes is really hard for me. You two guys must really be good at java!
    Thanks so much for your advice, I'll see what I can do.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Need advice - How to add items and keep a running total.
     
    Similar Threads
    Unsolved Programming Problem. Need Help.
    Storing of Data
    Java Classes
    Missing Output While Incorporating A Class
    Need help..Please! How to keep a running total...