• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Java problems, accumulating totals

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

What do you have so far? Where are you stuck making progress?
 
lowercase baba
Posts: 12893
63
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Nelineli Nemu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I currently have
I am even doing it right?

 
author & internet detective
Posts: 40200
816
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12893
63
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Small tip...

Lines 25, 29, 33:

I think you may want


rather than
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Life just hasn't been the same since the volcano erupted and now the air is full of tiny ads.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic