Results 1 to 5 of 5
  1. #1
    chancock82 is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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. #3
    chancock82 is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Re: Beginner Java Question

    Figuring out the total and outputting it on the window that shows the totals of the products

  4. #4
    chancock82 is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Re: Beginner Java Question

    Sorry for the mess up on the code tags NEWBIE

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

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

Similar Threads

  1. Java Question [Beginner Question]
    By joker760 in forum New To Java
    Replies: 3
    Last Post: 12-13-2011, 04:01 PM
  2. Beginner Question About Packages, etc
    By Humphrey Bogart in forum New To Java
    Replies: 15
    Last Post: 03-24-2011, 04:45 PM
  3. Beginner Question About Java Coding
    By Humphrey Bogart in forum New To Java
    Replies: 3
    Last Post: 03-09-2011, 09:41 PM
  4. really quick question for beginner
    By its_crawford in forum NetBeans
    Replies: 4
    Last Post: 06-17-2010, 04:14 AM
  5. Beginner Java question
    By DanK in forum New To Java
    Replies: 3
    Last Post: 04-27-2009, 04:29 AM

Posting Permissions

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