Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Java problems, accumulating totals

Nelineli Nemu
Greenhorn
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:
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

//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

//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

//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

//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
Rancher
Posts: 42968
73
Welcome to JavaRanch.

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

fred rosenberger
lowercase baba
Bartender
Posts: 12234
36

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...

Nelineli Nemu
Greenhorn
Posts: 3
This is what I currently have
I am even doing it right?

Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35095
380
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.

fred rosenberger
lowercase baba
Bartender
Posts: 12234
36
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
Posts: 1
Small tip...

Lines 25, 29, 33:

I think you may want

rather than

Soumil Shah
Ranch Hand
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....