aspose file tools*
The moose likes Beginning Java and the fly likes Beginner Java Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Beginner Java Question" Watch "Beginner Java Question" New topic
Author

Beginner Java Question

Christopher Hancock
Greenhorn

Joined: Jan 27, 2012
Posts: 3
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

Joined: Jul 21, 2011
Posts: 177
    
    6

What's blocking you from summing product1 + product2 + ... and output it?


Everything is theoretically impossible, until it is done. ~Robert A. Heinlein
Christopher Hancock
Greenhorn

Joined: Jan 27, 2012
Posts: 3
how would I write that and where should it go?
Tina Smith
Ranch Hand

Joined: Jul 21, 2011
Posts: 177
    
    6

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

Joined: Jan 27, 2012
Posts: 3
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

Joined: Jul 21, 2011
Posts: 177
    
    6

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

Joined: Oct 13, 2005
Posts: 39791
    
  28
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
 
subject: Beginner Java Question