Results 1 to 3 of 3

Thread: Help please

  1. #1
    broodwich is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default Help please

    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 Sales

  2. #2
    broodwich is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default Re: Help please

    I just can not figure out how to get it to display the subtotal after each pair of inputs.

  3. #3
    Tha Boss is offline Member
    Join Date
    Jun 2012
    Location
    ON, Canada.
    Posts
    25
    Rep Power
    0

    Default Re: Help please

    Quote Originally Posted by broodwich View Post
    I just can not figure out how to get it to display the subtotal after each pair of inputs.
    Your code just needed a bit of rearranging... by including an output screen in the loop "while (productId != 0)" you can show the user the results as they update it.
    Also by declaring the "productId" variable type as "double" not "float", you can easily overwrite the previous value if it has changed (as your current code stands, the previous value is added to the new calculation with the "+=" operator.) I replaced them with "=" to overwrite, so if the user later enters 0 for the amount of a particular product, it's total will be $0 not previous calculation + $0).

    Java Code:
    public class Sales
    {
    
        public static void main (String args[])
        {
            double product1 = 0, product2 = 0, product3 = 0,
                product4 = 0, product5 = 0;
    
            String inputString, output = "";
    
            int productId = 1;
            int totalCost = 0;
    
            // create decimal format to format floating point numbers
            // with two digits to the right of the decimal point
            NumberFormat moneyFormat =
                NumberFormat.getCurrencyInstance (Locale.US);
    
    
    
            // 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 a summary message
                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";
                
                if (productId != 0)
                {
                    // show results
                    JOptionPane.showMessageDialog (null, output,
                            "Current totals", JOptionPane.INFORMATION_MESSAGE);
                }
            }
    
            // show results
            JOptionPane.showMessageDialog (null, output,
                    "Totals", JOptionPane.INFORMATION_MESSAGE);
    
            System.exit (0);
        }
    } // end class Sales
    CHEERS!

    E.Hughes
    "I'm sometimes hard to please... because I'm only satisfied with the very best." - Fernand Point.

Posting Permissions

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