Two Laptop Bag*
The moose likes Beginning Java and the fly likes Java problems, accumulating totals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java problems, accumulating totals" Watch "Java problems, accumulating totals" New topic
Author

Java problems, accumulating totals

Nelineli Nemu
Greenhorn

Joined: Dec 11, 2009
Posts: 3
Hi guys. I just need an example of a java program that accumulates total.
I'm using Jgrasp to compile the program.

If possible, please answer the question below so I can get an example on how to do a loop program that accumulates total.

Here's the problem:

Write a program that calculates the cost of buying a burger from the CIS 103 Super Burger store. The menus has four choices to choose from:
Menu Cost ($)
Burger 2.15
Combo 1 (Fries + Drink) 3.50
Combo 2 ( Cheese + Fries + Drink) 4.85
Combo 3 (Double Cheese Burger + Fries + Drink) 5.25

The program presents a menu that allows the user to choose the desired burger combo. When the user selects an item, the program displays the cost. When the user selects option 5, the program displays the accumulated total cost.

Sample output

//this is the menu that is suppose to appear

CIS 103 SUPER BURGER
1. Burger
2. Burger Combo1 (Fries + Drink)
3. Burger Comb2 (Cheese + Fries + Drink)
4. Burger Comb3 (Double cheese Burger + Fries + Drink)
5. Done

(The menu appears with showing "Please choose 1,2,3,4, or 5")
//user chose 2

You select combo1: $3.50

CIS 103 SUPER BURGER
1. Burger
2. Burger Combo1 (Fries + Drink)
3. Burger Comb2 (Cheese + Fries + Drink)
4. Burger Comb3 (Double cheese Burger + Fries + Drink)
5. Done

(The menu appears with showing "Please choose 1,2,3,4, or 5")
//user chose 4

You select combo3: $5.25

CIS 103 SUPER BURGER
1. Burger
2. Burger Combo1 (Fries + Drink)
3. Burger Comb2 (Cheese + Fries + Drink)
4. Burger Comb3 (Double cheese Burger + Fries + Drink)
5. Done

(The menu appears with showing "Please choose 1,2,3,4, or 5")
//user chose 3

You select combo2: $4.85

CIS 103 SUPER BURGER
1. Burger
2. Burger Combo1 (Fries + Drink)
3. Burger Comb2 (Cheese + Fries + Drink)
4. Burger Comb3 (Double cheese Burger + Fries + Drink)
5. Done

(The menu appears with showing "Please choose 1,2,3,4, or 5")
//user chose 5

The total is $13.6


Thanks in advance, I just need an example so I can better understand the principle.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
Welcome to JavaRanch.

What do you have so far? Where are you stuck making progress?


Ping & DNS - updated with new look and Ping home screen widget
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

Remember, people here will not just do your homework for you. We'll help you with your code, but people won't post it.

can you write a loop at all, of any kind? Do you know what the three loop structures are (ok, there are really four, but one is a shorthand)?

I'll give you a hint...

a 'for' loop is used when you know exactly how many times you want to loop around. The way you describe the program, you won't know this, so that one is probably out...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Nelineli Nemu
Greenhorn

Joined: Dec 11, 2009
Posts: 3
This is what I currently have
I am even doing it right?

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29287
    
140

Nelineli,
Welcome to JavaRanch! You are on the right track with the loop. One thing - you want a total to accumulate each time.

This wipes out the total and replaces it with $3.50.


This adds $3.50 to the total.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

I don't think this will compile. You can't have a variable named with a number. What you probably need is something more like

double choice1 = 2.15
double choice2 = 3.50
double choice3 = 4.85
double choice4 = 5.25

A couple of general tips, though. When dealing with money, it's generally a bad idea to use floats. use some kind of int, and use the atomic unit. When dealing with American currency, store everything as pennies, and convert when you want to display.

Also, don't write 43 lines of code at a time. Many folks here will tell you to write as little as possible (sometimes even only ONE line) and then compile/test/fix until you KNOW that works...then add some more.

Jason Schindler
Greenhorn

Joined: Dec 13, 2009
Posts: 1
Small tip...

Lines 25, 29, 33:

I think you may want


rather than
Soumil Shah
Ranch Hand

Joined: Jul 13, 2009
Posts: 54
also you are not using curly braces for if-else conditions... as there are more than one statements within them... it should be...


One more suggestion.. instead of if-else, you could also achieve this with switch-case....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java problems, accumulating totals
 
Similar Threads
Java Loops - accumulating totals
Help with code
Design Patterns
Laundrette
Help with program