1. Member
Join Date
Jan 2012
Posts
3
Rep Power
0

## 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.

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 Sales```
Last edited by pbrockway2; 01-28-2012 at 04:19 AM. Reason: code tags added

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,545
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.

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?

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

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

5. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,545
Rep Power
11

## 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•