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
• Bear Bibeault
• Ron McLeod
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Tim Cooke
• Liutauras Vilda
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• fred rosenberger
• salvin francis
Bartenders:
• Piet Souris
• Frits Walraven
• Carey Brown

# Beginner Java Question

Greenhorn
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

Ranch Hand
Posts: 208
9
What's blocking you from summing product1 + product2 + ... and output it?

Christopher Hancock
Greenhorn
Posts: 3
how would I write that and where should it go?

Tina Smith
Ranch Hand
Posts: 208
9
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
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
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.

Marshal
Posts: 69843
278
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.

 So I left, I came home, and I ate some pie. And then I read this tiny ad: Thread Boost feature https://coderanch.com/t/674455/Thread-Boost-feature
Similar Threads