Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    14
    Rep Power
    0

    Modified Fuzzy Dice


    So I'm currently taking Java using the Simply Java Programming book. Gave the info in case anyone was actually using the same book. Either way here is my problem...

    The book instructs us to "Calculate 7% of the total amount, and display the discount amount as a negative value in discountJTextField, which is uneditable. Subtract this amount from the total and update totalJTextField.

    The book tells us to begin in the after line 338.

    Any help here would be very appreciated. Here is the script that I am working with currently. Also wasn't sure how else to place my coding in here so I just copied and pasted. If there is a better way please let me know how cause this looks jumbled. Thanks in advance for any help.

    Code:
    // Exercise 7.13: FuzzyDiceOrderForm.java
    // This application calculates the total cost of a
    // purchase order for different colored fuzzy dice.
    import java.awt.*;
    import java.awt.event.*;
    import java.text.*;
    import javax.swing.*;
    
    public class FuzzyDiceOrderForm extends JFrame
    {
       // JLabel that displays header on application window
       private JLabel fuzzyDiceJLabel;
    
       // JLabel and JTextField for order number
       private JLabel orderNumberJLabel;
       private JTextField orderNumberJTextField;
    
       // JLabel and JTextField for user's name
       private JLabel nameJLabel;
       private JTextField nameJTextField;
    
       // JLabel and JTextFields for user's address
       private JLabel addressJLabel;
       private JTextField addressJTextField1;
       private JTextField addressJTextField2;
    
       // JLabel and JCheckBoxes for die types
       private JLabel typeJLabel;
       private JCheckBox whiteTypeJCheckBox;
       private JCheckBox redTypeJCheckBox;
    
       // JLabel and JTextFields for die quantities
       private JLabel quantityJLabel;
       private JTextField whiteQuantityJTextField;
       private JTextField redQuantityJTextField;
    
       // JLabels for die prices
       private JLabel priceJLabel;
       private JLabel whitePriceJLabel;
       private JLabel redPriceJLabel;
    
       // JLabel and JTextFields for die subtotals
       private JLabel totalsJLabel;
       private JTextField whiteTotalsJTextField;
       private JTextField redTotalsJTextField;
    
       // JLabel and JTextField for total before tax
       private JLabel subtotalJLabel;
       private JTextField subtotalJTextField;
       
       // JLabel and JTextField for tax
       private JLabel taxJLabel;
       private JTextField taxJTextField;
    
       // JLabel and JTextField for discount
       private JLabel discountJLabel;
       private JTextField discountJTextField;
    
       // JLabel and JTextField for final total
       private JLabel totalJLabel;
       private JTextField totalJTextField;
    
       // JButton to initiate calculate of user's bill
       private JButton calculateJButton;
       
       // no-argument constructor
       public FuzzyDiceOrderForm()
       {
          createUserInterface();
       }
       
       // create and position GUI components; register event handlers
       private void createUserInterface()
       {
          // get content pane for attaching GUI components
          Container contentPane = getContentPane();
    
          // enable explicit positioning of GUI components
          contentPane.setLayout( null );
          
          // set up fuzzyDiceJLabel
          fuzzyDiceJLabel = new JLabel();
          fuzzyDiceJLabel.setBounds( 137, 16, 235, 24 );
          fuzzyDiceJLabel.setText( "Fuzzy Dice" );
          fuzzyDiceJLabel.setFont( 
             new Font( "Default", Font.PLAIN, 22 ) );
          contentPane.add( fuzzyDiceJLabel );
    
          // set up orderNumberJLabel
          orderNumberJLabel = new JLabel();
          orderNumberJLabel.setBounds( 15, 65, 100, 16 );
          orderNumberJLabel.setText( "Order Number:" );
          contentPane.add( orderNumberJLabel );
    
          // set up orderNumberJTextField
          orderNumberJTextField = new JTextField();
          orderNumberJTextField.setBounds( 111, 65, 48, 21 );
          orderNumberJTextField.setText( "0" );
          orderNumberJTextField.setHorizontalAlignment( 
             JTextField.RIGHT );
          contentPane.add( orderNumberJTextField );
    
          // set up nameJLabel
          nameJLabel = new JLabel();
          nameJLabel.setBounds( 15, 105, 40, 16 );
          nameJLabel.setText( "Name:" );
          contentPane.add( nameJLabel );
    
          // set up nameJTextField
          nameJTextField = new JTextField();
          nameJTextField.setBounds( 111, 105, 245, 21 );
          nameJTextField.setText( "Enter name here" );
          contentPane.add( nameJTextField );
    
          // set up addressJLabel
          addressJLabel = new JLabel();
          addressJLabel.setBounds( 15, 129, 56, 16 );
          addressJLabel.setText( "Address:" );
          contentPane.add( addressJLabel );
    
          // set up addressJTextField1
          addressJTextField1 = new JTextField();
          addressJTextField1.setBounds( 111, 129, 245, 21 );
          addressJTextField1.setText( "Address Line 1" );
          contentPane.add( addressJTextField1 );
    
          // set up addressJTextField2
          addressJTextField2 = new JTextField();
          addressJTextField2.setBounds( 111, 153, 245, 21 );
          addressJTextField2.setText( "City, State, Zip" );
          contentPane.add( addressJTextField2 );
    
          // set up typeJLabel
          typeJLabel = new JLabel();
          typeJLabel.setBounds( 15, 204, 40, 16 );
          typeJLabel.setText( "Type:" );
          contentPane.add( typeJLabel );
    
          // set up whiteTypeJCheckBox
          whiteTypeJCheckBox = new JCheckBox();
          whiteTypeJCheckBox.setBounds( 10, 227, 93, 21 );
          whiteTypeJCheckBox.setText( "White/Black" );
          contentPane.add( whiteTypeJCheckBox );
          
          // set up redTypeJCheckBox
          redTypeJCheckBox = new JCheckBox();
          redTypeJCheckBox.setBounds( 10, 252, 88, 21 );
          redTypeJCheckBox.setText( "Red/Black" );
          contentPane.add( redTypeJCheckBox );
    
          // set up quantityJLabel
          quantityJLabel = new JLabel();
          quantityJLabel.setBounds( 111, 204, 72, 16 );
          quantityJLabel.setText( "Quantity:" );
          contentPane.add( quantityJLabel );
    
          // set up whiteQuantityJTextField
          whiteQuantityJTextField = new JTextField();
          whiteQuantityJTextField.setBounds( 111, 228, 64, 21 );
          whiteQuantityJTextField.setText( "0" );
          whiteQuantityJTextField.setHorizontalAlignment( 
             JTextField.RIGHT );
          contentPane.add( whiteQuantityJTextField );
    
          // set up redQuantityJTextField
          redQuantityJTextField = new JTextField();
          redQuantityJTextField.setBounds( 111, 252, 64, 21 );
          redQuantityJTextField.setText( "0" );
          redQuantityJTextField.setHorizontalAlignment(
             JTextField.RIGHT );
          contentPane.add( redQuantityJTextField );
    
          // set up priceJLabel
          priceJLabel = new JLabel();
          priceJLabel.setBounds( 196, 204, 72, 16 );
          priceJLabel.setText( "Price:" );
          contentPane.add( priceJLabel );
    
          // set up whitePriceJLabel
          whitePriceJLabel = new JLabel();
          whitePriceJLabel.setBounds( 196, 228, 80, 21 );
          whitePriceJLabel.setText( "$6.25" );
          contentPane.add( whitePriceJLabel );
    
          // set up redPriceJLabel
          redPriceJLabel = new JLabel();
          redPriceJLabel.setBounds( 196, 252, 80, 21 );
          redPriceJLabel.setText( "$5.00" );
          contentPane.add( redPriceJLabel );
    
          // set up totalsJLabel
          totalsJLabel = new JLabel();
          totalsJLabel.setBounds( 267, 204, 72, 16 );
          totalsJLabel.setText( "Totals:" );
          contentPane.add( totalsJLabel );
    
          // set up whiteTotalsJTextField
          whiteTotalsJTextField = new JTextField();
          whiteTotalsJTextField.setBounds( 267, 228, 87, 16 );
          whiteTotalsJTextField.setText( "$0.00" );
          whiteTotalsJTextField.setEditable( false );
          whiteTotalsJTextField.setHorizontalAlignment( 
             JTextField.RIGHT );
          contentPane.add( whiteTotalsJTextField );
    
          // set up redTotalsJTextField
          redTotalsJTextField = new JTextField();
          redTotalsJTextField.setBounds( 267, 252, 87, 16 );
          redTotalsJTextField.setText( "$0.00" );
          redTotalsJTextField.setEditable( false );
          redTotalsJTextField.setHorizontalAlignment( 
             JTextField.RIGHT );
          contentPane.add( redTotalsJTextField );
    
          // set up subtotalJLabel
          subtotalJLabel = new JLabel();
          subtotalJLabel.setBounds( 196, 293, 72, 16 );
          subtotalJLabel.setText( "Subtotal:" );
          contentPane.add( subtotalJLabel );
    
          // set up subtotalJTextField
          subtotalJTextField = new JTextField();
          subtotalJTextField.setBounds( 267, 293, 87, 16 );
          subtotalJTextField.setText( "$0.00" );
          subtotalJTextField.setEditable( false );
          subtotalJTextField.setHorizontalAlignment(
             JTextField.RIGHT );
          contentPane.add( subtotalJTextField );
    
          // set up taxJLabel
          taxJLabel = new JLabel();
          taxJLabel.setBounds( 196, 317, 72, 16 );
          taxJLabel.setText( "Tax:" );
          contentPane.add( taxJLabel );
    
          // set up taxJTextField
          taxJTextField = new JTextField();
          taxJTextField.setBounds( 267, 317, 87, 16 );
          taxJTextField.setText( "$0.00" );
          taxJTextField.setEditable( false );
          taxJTextField.setHorizontalAlignment(
             JTextField.RIGHT );
          contentPane.add( taxJTextField );
    
          // set up discountJLabel
          discountJLabel = new JLabel();
          discountJLabel.setBounds( 196, 341, 72, 16 );
          discountJLabel.setText( "Discount:" );
          contentPane.add( discountJLabel );
    
          // set up discountJTextField
          discountJTextField = new JTextField();
          discountJTextField.setBounds( 267, 341, 87, 16 );
          discountJTextField.setText( "$0.00" );
          discountJTextField.setEditable( false );
          discountJTextField.setHorizontalAlignment(
             JTextField.RIGHT );
          contentPane.add( discountJTextField );
    
          // set up totalJLabel
          totalJLabel = new JLabel();
          totalJLabel.setBounds( 196, 365, 72, 16 );
          totalJLabel.setText( "Total:" );
          contentPane.add( totalJLabel );
    
          // set up totalJTextField
          totalJTextField = new JTextField();
          totalJTextField.setBounds( 267, 365, 87, 16 );
          totalJTextField.setText( "$0.00" );
          totalJTextField.setEditable( false );
          totalJTextField.setHorizontalAlignment(
             JTextField.RIGHT );
          contentPane.add( totalJTextField );
          
          // set up calculateJButton and register its event handler
          calculateJButton = new JButton();
          calculateJButton.setBounds( 267, 397, 87, 23 );
          calculateJButton.setText( "Calculate" );
          contentPane.add( calculateJButton );
          calculateJButton.addActionListener( 
          
             new ActionListener() // anonymous inner class
             {
                // event handler called when user clicks calculateJButton
                public void actionPerformed( ActionEvent event )
                {
                   calculateJButtonActionPerformed( event );
                }
                
             } // end anonymous inner class
             
          ); // end call to addActionListener
          
          // set properties of application's window
          setTitle( "Fuzzy Dice Order Form" ); // set title bar string
          setSize( 380, 466 );                 // set window size
          setVisible( true );                  // display window
          
       } // end method createUserInterface
       
       // calculate the cost of the order
       private void calculateJButtonActionPerformed( ActionEvent event )
       {
          // get user inputs
          String orderNumber = orderNumberJTextField.getText();
          String name = nameJTextField.getText();
          String address1 = addressJTextField1.getText();
          String address2 = addressJTextField2.getText();
    
          // total of white/black dice ordered
          double whiteTotals = 0.0;
    
          // total of red/black dice ordered
          double redTotals = 0.0;
    
          // calculate price if white/black JCheckBox is selected
          if ( whiteTypeJCheckBox.isSelected() )
          {
             whiteTotals = Integer.parseInt( 
                whiteQuantityJTextField.getText() ) * 6.25;
          }
    
          // calculate price if red/black JCheckBox is selected
          if ( redTypeJCheckBox.isSelected() )
          {
             redTotals = Integer.parseInt(
                redQuantityJTextField.getText() ) * 5.00;
          }
           
          // display message if order number, name or address is empty
          if ( orderNumber.equals( "" ) || name.equals( "" ) || 
             address1.equals( "" ) || address2.equals( "" ) ||
             orderNumber.equals( "0" ) || 
             name.equals( "Enter name here" ) || 
             address1.equals( "Address Line 1" ) ||
             address2.equals( "City, State, Zip" ) )
          {
             // display message
             JOptionPane.showMessageDialog( null,
                "Please fill out all information fields.", 
                "Empty Fields", JOptionPane.WARNING_MESSAGE );
          }
          else if ( !whiteTypeJCheckBox.isSelected() && 
             !redTypeJCheckBox.isSelected() )
          {
             // display message if no JCheckBox is
             // selected and its quantity is 0
             JOptionPane.showMessageDialog( null,
                "Please select an item and enter a quantity.",
                "No Item selected", JOptionPane.WARNING_MESSAGE );
          }
          else // otherwise, calculate totals
          {
             // set display format
             DecimalFormat dollars = new DecimalFormat( "$0.00" );
    
             // display totals of dice ordered
             whiteTotalsJTextField.setText( dollars.format(
                whiteTotals ) );
             redTotalsJTextField.setText( dollars.format( redTotals ) );
             
             // calculate and display subtotal
             double subtotal = whiteTotals + redTotals;
             subtotalJTextField.setText( dollars.format( subtotal ) );
          
          // determine if subtotal is greater than 50
         if ( subtotal < 50 )
         {
        subtotalJTextField.setText( "subtotal greater than 50" );
    			}
    			
    	  // display information message
    	JOptionPane.showMessageDialog( null,
    	"7% discount will be applied.",
    	"Discount Offer", JOptionPane.INFORMATION_MESSAGE )
    			
    	 // calculate and display discount
    	double discount = subtotal * 0.07;
    	discountJTextField.setText( dollars.format( discount ) );    
    	     
    		  	// calculate and display tax
             double tax = subtotal * 0.05;
             taxJTextField.setText( dollars.format( tax ) );
             
             // calculate and display total
             totalJTextField.setText( 
                dollars.format( subtotal + tax ) );
             
          } // end else
    
       } // end method calculateJButtonActionPerformed
       
       // main method
       public static void main( String[] args )
       {
          FuzzyDiceOrderForm application = new FuzzyDiceOrderForm();
          application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    
       } // end method main
       
    } // end class FuzzyDiceOrderForm
    Last edited by Rahlevi; February 22nd, 2013 at 03:43 PM. Reason: Missed details
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Please edit your post and wrap the code in code tags.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    14
    Rep Power
    0
    Originally Posted by NormR
    Please edit your post and wrap the code in code tags.
    Thanks for that.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    14
    Rep Power
    0
    bump for any help
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Can you explain what your problems are?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    14
    Rep Power
    0
    Originally Posted by NormR
    Can you explain what your problems are?
    Well the problem that I'm having is I can't figure out how to do complete these steps..."Calculate 7% of the total amount, and display the discount amount as a negative value in discountJTextField, which is uneditable. Subtract this amount from the total and update totalJTextField.

    I'm not sure how to display the discount amoutn as a negative in the discountJTextField and subtract it from the total.
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    how to display the discount amoutn as a negative
    What notation do you use to display negative?
    -123.45
    (123.45)

    subtract it from the total.
    Can you explain what problem you are having subtracting one number from another?
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    14
    Rep Power
    0
    Originally Posted by NormR
    What notation do you use to display negative?
    -123.45
    (123.45)


    Can you explain what problem you are having subtracting one number from another?
    At this moment I couldn't tell you because my book is very vague on displaying negative values. I honestly don't know what statement I'm suppose to use there.

    As for subtracting, I understand how subtracting one number from another works. I just don't know the code I'm suppose to be using to do that calculation.
  16. #9
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Try adding a - in front of the number to be displayed as negative.

    I'm still confused on the problem with subtracting one number from another.
    result = another - oneNumber; // subtract oneNumber from another
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    14
    Rep Power
    0
    Originally Posted by NormR
    result = another - oneNumber; // subtract oneNumber from another
    That is what I needed right there. I didn't know the code to do any calculations. So this is a step in the right direction. Now I just need to figure out what kind of statement will display the discount amount in its JTextField.

    Thank you for at least getting me that far!!
  20. #11
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    need to figure out what kind of statement will display the discount amount in its JTextField.
    What is the problem with the code displaying numbers in the text fields now?
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    14
    Rep Power
    0
    Originally Posted by NormR
    What is the problem with the code displaying numbers in the text fields now?
    Pretty much what I have to do now is display the dollar amount that is discounted off. After I compile and run the program, if the subtotal is greater than 50 then it needs to display how much the discount is in the discountJTextField and then subtract that amount from the subtotal in order to produce to desired total amount.
  24. #13
  25. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Does the code already call methods to put numbers into all the text fields?
    You don't have to figure out how to do that. It's done.
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    14
    Rep Power
    0
    Originally Posted by NormR
    Does the code already call methods to put numbers into all the text fields?
    You don't have to figure out how to do that. It's done.
    Yea it displays the discount amount already. I've got to get it to do that on orders over 50 only. Right now it displays it regardless of order amount. It also adds the amount instead of subtracting which I'm getting ready to fix now.
  28. #15
  29. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    on orders over 50 only
    Use an if statement to test the size of the order and skip doing it if too small
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo