This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Beginner Java Question

 
Christopher Hancock
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Help!

Write a Java application that prompts the user for pairs of inputs of a product number (1-5), and then an integer quantity of units sold. You must use a switch statement and a sentinel-controlled loop (i.e. a loop that stops execution when an out of range value, such as -1, is input). All 15 items below are for a single purchase. There are five sets of inputs as follows:

Product 1 1 unit (cost is $2.98 per unit)
Product 2 2 units (cost is $4.50 per unit)
Product 3 3 units (cost is $9.98 per unit)
Product 4 4 units (cost is $4.49 per unit)
Product 5 5 units (cost is $6.87 per unit)

Your application must calculate and display the total retail value of all products sold, after all 5 pairs of inputs are completed. You must also display the total after each new pair of input values is entered.

Here is what I have so far...It works ,however, I want to add in the final message window the total of the entire order.



Thanks Collin





 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's blocking you from summing product1 + product2 + ... and output it?
 
Christopher Hancock
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how would I write that and where should it go?
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where it should go:
Your application must calculate and display the total retail value of all products sold, after all 5 pairs of inputs are completed. You must also display the total after each new pair of input values is entered.


Identify where each of these points are in your code, then calculate the sum of product $$ sold and display it to the user in each of these places.
 
Christopher Hancock
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that it already does this but I would like it to add all the totals together and output it to show the total cost of all the products combined
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Total retail value is == total cost of all the products combined, is it not?
Please ShowSomeEffort (<= link) and take what you just said and put it into code. It sounds like you have a good idea of what you want to do.

If you're still having problems after you have code, post back with the problems you're having, and we'll help work through them.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47274
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Never never use floating-point arithmetic for money. Using float instead of double arithmetic simply makes the imprecison worse.
Never put so much code in the main method. Ideal length: one statement.
You have hard-coded the prices dissociated from the products. You ought to have a Product class with price and title as a field. You can put product objects into an array and find them from the input (or more likely input - 1, so you can access the index 0).
JOptionPane is a very old-fashioned way to get input. Have you not come across the Scanner class?
I think String.format is a better way to format output that NumberFormat and Currency.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic