Results 1 to 5 of 5
Thread: Beginner Java Question
- 01-28-2012, 03:57 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
Beginner Java Question
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.
Java Code:// Sales.java // Program calculates sales, based on an input of product // number and quantity sold import java.awt.*; import java.text.NumberFormat; import java.util.Locale; import javax.swing.*; public class Sales { public static void main( String args[] ) { float product1 = 0, product2 = 0, product3 = 0; float product4 = 0, product5 = 0; String inputString; int productId = 1; int totalCost = 0; // ask user for product number until flag value entered while (productId != 0) { // determine the product chosen inputString = JOptionPane.showInputDialog( "Enter product number (1-5) 0 to Quit:" ); productId = Integer.parseInt( inputString ); if ( productId >= 1 && productId <= 5 ) { // determine the number sold of the item inputString = JOptionPane.showInputDialog( "Enter quantity:" ); int quantity = Integer.parseInt( inputString ); // Calculate the total for the item by the // price times the quantity sold switch ( productId ) { case 1: product1 += quantity * 2.98; break; case 2: product2 += quantity * 4.50; break; case 3: product3 += quantity * 9.98; break; case 4: product4 += quantity * 4.49; break; case 5: product5 += quantity * 6.87; break; } } else if ( productId != 0 ) { JOptionPane.showMessageDialog( null, "Product number must be between 1 and 5 or -1 to Quit", "Input Error", JOptionPane.INFORMATION_MESSAGE ); } } // create decimal format to format floating point numbers // with two digits to the right of the decimal point NumberFormat moneyFormat = NumberFormat.getCurrencyInstance( Locale.US ); // create a summary message String output = "Product 1: " + moneyFormat.format( product1 ); output += "\nProduct 2: "+ moneyFormat.format( product2 ); output += "\nProduct 3: " + moneyFormat.format( product3 ); output += "\nProduct 4: " + moneyFormat.format( product4 ); output += "\nProduct 5: " + moneyFormat.format( product5 ) + "\n"; JTextArea outputArea = new JTextArea( 6, 20 ); outputArea.setText( output ); // show results JOptionPane.showMessageDialog( null, outputArea, "Totals", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } } // end class SalesLast edited by pbrockway2; 01-28-2012 at 04:19 AM. Reason: code tags added
- 01-28-2012, 04:23 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
Re: Beginner Java Question
Hi chancock82, welcome to the forums!
When you post code, use the "code" tags. You put [code] at the start of the code and [/code] at the end. That way the code will be more readable when it appears here.
What is the problem - figuring out the total? or adding it to the dialog?I want to add in the final message window the total of the entire order.
- 01-28-2012, 04:25 AM #3
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
Re: Beginner Java Question
Figuring out the total and outputting it on the window that shows the totals of the products
- 01-28-2012, 04:25 AM #4
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
Re: Beginner Java Question
Sorry for the mess up on the code tags NEWBIE
- 01-28-2012, 07:21 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
Similar Threads
-
Java Question [Beginner Question]
By joker760 in forum New To JavaReplies: 3Last Post: 12-13-2011, 04:01 PM -
Beginner Question About Packages, etc
By Humphrey Bogart in forum New To JavaReplies: 15Last Post: 03-24-2011, 04:45 PM -
Beginner Question About Java Coding
By Humphrey Bogart in forum New To JavaReplies: 3Last Post: 03-09-2011, 09:41 PM -
really quick question for beginner
By its_crawford in forum NetBeansReplies: 4Last Post: 06-17-2010, 04:14 AM -
Beginner Java question
By DanK in forum New To JavaReplies: 3Last Post: 04-27-2009, 04:29 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks