# Beginner Java Question

• 01-28-2012, 04:57 AM
chancock82
Beginner Java Question

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.

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 Sales```
• 01-28-2012, 05:23 AM
pbrockway2
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.

Quote:

I want to add in the final message window the total of the entire order.
What is the problem - figuring out the total? or adding it to the dialog?
• 01-28-2012, 05:25 AM
chancock82
Re: Beginner Java Question
Figuring out the total and outputting it on the window that shows the totals of the products
• 01-28-2012, 05:25 AM
chancock82
Re: Beginner Java Question
Sorry for the mess up on the code tags NEWBIE
• 01-28-2012, 08:21 AM
pbrockway2
Re: Beginner Java Question
You have variables that represent the totals for each of the products: so just add them together. As for outputing the result, you can add a line to the output string.