This is a piece of work i need to complete for my Object Orientated programming
module at university.

The task,

(User Input) Order Number, Name, Address line 1 and Address line 2 and select either Computer and/or printer check box. N.B. These FIVE fields must be completed by User otherwise suitable error messages are displayed as part of the validation),

(User Input) 1 * computer @ value £350 each, sub-total for computer = £350,

(User Input) 2 * printer @ value is £125 each, sub-total for printer = £250,

(User Input) press ‘Calculate’ Button depressed on dialog screen)

(Screen Display) sub-total for computer and printer = £600,
VAT (17.5% on £600) = £105,
Total = £705.

So far i have set up the environment with all of the Jlables, jtextfields, Jcheckboxs and calculate button.

But i need the methods that will calculate subtotals of a set value and then add the vat!

What i have done so far!

// Exercise 3.11: computer.java
// GUI that enables users to calculate vat.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class computer extends JFrame
{
    //JLabel for business name
    private JLabel businessNameJLabel;
    
    // JLabel and JTextField for order number
   private JLabel orderJLabel;
   private JTextField orderJTextField;

   // JLabel and JTextField for first name
   private JLabel firstNameJLabel;
   private JTextField firstNameJTextField;

   // JLabel and JTextField for last name
   private JLabel lastNameJLabel;
   private JTextField lastNameJTextField;

   // JLabel and JTextField for address
   private JLabel addressJLabel;
   private JTextField addressJTextField;

   // JLabel and JTextField for city
   private JLabel cityJLabel;
   private JTextField cityJTextField;
      
   // JCheckBox and JLabel for computer
   private JCheckBox computerJCheckBox;
   private JLabel computerPriceJLabel;
   
   // JCheckBox and JLabel for printer
   private JCheckBox printerJCheckBox;
   private JLabel printerPriceJLabel;
   
   //JTextField for computerquantity
   private JTextField computerquantityJTextField;
   
   //JTextField for printerquantity
   private JTextField printerquantityJTextField;
   
   
   
   //JTextField for computertotal
   private JTextField computertotalJTextField;
   
   //JTextField for printertotal
   private JTextField printertotalJTextField;
   
   // JLabel and JTextField for subtotal
   private JLabel subtotalJLabel;
   private JTextField subtotalJTextField;
   
   // JLabel and JTextField for vat
   private JLabel vatJLabel;
   private JTextField vatJTextField;
   
   // JLabel and JTextField for total
   private JLabel totalJLabel;
   private JTextField totalJTextField;
   
   // JButton to initiate calculation
   private JButton calculateJButton;
   
   
   // no-argument constructor
   public computer()
   {
      createUserInterface();
   }

   // create and position GUI components
   public void createUserInterface()
   {
      // get content pane and set its layout
      Container container = getContentPane();
      container.setLayout( null );
      
      // set up businessNameJLabel
      businessNameJLabel = new JLabel();
      businessNameJLabel.setText( "Cool Computer Sales" );
      businessNameJLabel.setLocation( 35, 0 );
      businessNameJLabel.setSize( 550, 88 );
      businessNameJLabel.setFont( new Font( "SansSerif", Font.PLAIN, 36 ) );
      businessNameJLabel.setHorizontalAlignment( JLabel.CENTER );
      container.add( businessNameJLabel );
      
       // set up orderJLabel
      orderJLabel = new JLabel();
      orderJLabel.setBounds( 16, 80, 134, 21 );
      orderJLabel.setText( "Order No:" );
      container.add( orderJLabel );

      // set up orderJTextField
      orderJTextField = new JTextField();
      orderJTextField.setBounds( 88, 80, 40, 21 );
      orderJTextField.setText( "" );
      container.add( orderJTextField );

      // set up firstNameJLabel
      firstNameJLabel = new JLabel();
      firstNameJLabel.setBounds( 16, 110, 130, 21 );
      firstNameJLabel.setText( "First name:" );
      container.add( firstNameJLabel );

      // set up firstNameJTextField
      firstNameJTextField = new JTextField();
      firstNameJTextField.setBounds( 88, 110, 220, 21 );
      firstNameJTextField.setText( "" );
      container.add( firstNameJTextField );

      // set up lastNameJLabel
      lastNameJLabel = new JLabel();
      lastNameJLabel.setBounds( 16, 140, 130, 21 );
      lastNameJLabel.setText( "Last name:" );
      container.add( lastNameJLabel );

      // set up lastNameJTextField
      lastNameJTextField = new JTextField();
      lastNameJTextField.setBounds( 88, 140, 220, 21 );
      lastNameJTextField.setText( "" );
      container.add( lastNameJTextField );

      // set up addressJLabel
      addressJLabel = new JLabel();
      addressJLabel.setBounds( 16, 170, 64, 21 );
      addressJLabel.setText( "Address:" );
      container.add( addressJLabel );

      // set up addressJTextField
      addressJTextField = new JTextField();
      addressJTextField.setBounds( 88, 170, 220, 21 );
      addressJTextField.setText( "" );
      container.add( addressJTextField );
      
      // set up cityJLabel
      cityJLabel = new JLabel();
      cityJLabel.setBounds( 16, 200, 64, 21 );
      cityJLabel.setText( "City:" );
      container.add( cityJLabel );

      // set up cityJTextField
      cityJTextField = new JTextField();
      cityJTextField.setBounds( 88, 200, 220, 21 );
      cityJTextField.setText( "" );
      container.add( cityJTextField );
      
      // set up computerJCheckBox
      computerJCheckBox = new JCheckBox();
      computerJCheckBox.setBounds( 16, 260, 122, 24 );
      computerJCheckBox.setText( "Computer" );
      container.add( computerJCheckBox );
      
      // set up computerPriceJLabel
      computerPriceJLabel = new JLabel();
      computerPriceJLabel.setBounds( 250, 260, 50, 24 );
      computerPriceJLabel.setText( "£350.00" );
      computerPriceJLabel.setHorizontalAlignment( JLabel.RIGHT );
      container.add( computerPriceJLabel );
      
       // set up printerJCheckBox
      printerJCheckBox = new JCheckBox();
      printerJCheckBox.setBounds( 16, 300, 122, 24 );
      printerJCheckBox.setText( "Printer" );
      container.add( printerJCheckBox );
      
      // set up printerPriceJLabel
      printerPriceJLabel = new JLabel();
      printerPriceJLabel.setBounds( 250, 300, 50, 24 );
      printerPriceJLabel.setText( "£125.00" );
      printerPriceJLabel.setHorizontalAlignment( JLabel.RIGHT );
      container.add( printerPriceJLabel );
      
      // set up computerquantityJTextField
      computerquantityJTextField = new JTextField();
      computerquantityJTextField.setBounds( 150, 260, 56, 21 );
      computerquantityJTextField.setHorizontalAlignment( JTextField.CENTER );
      container.add( computerquantityJTextField );
      
       // set up printerquantityJTextField
      printerquantityJTextField = new JTextField();
      printerquantityJTextField.setBounds( 150, 300, 56, 21 );
      printerquantityJTextField.setHorizontalAlignment( JTextField.CENTER );
      container.add( printerquantityJTextField );
   
      // set up computertotalJTextField
      computertotalJTextField = new JTextField();
      computertotalJTextField.setBounds( 350, 260, 56, 21 );
      computertotalJTextField.setText( "£0.00" );
      computertotalJTextField.setEditable( false );
      computertotalJTextField.setHorizontalAlignment( JTextField.CENTER );
      container.add( computertotalJTextField );
      
       // set up printertotalJTextField
      printertotalJTextField = new JTextField();
      printertotalJTextField.setBounds( 350, 300, 56, 21 );
      printertotalJTextField.setText( "£0.00" );
      printertotalJTextField.setEditable( false );
      printertotalJTextField.setHorizontalAlignment( JTextField.CENTER );
      container.add( printertotalJTextField );
      
       // set up subtotalJLabel
      subtotalJLabel = new JLabel();
      subtotalJLabel.setBounds( 250, 340, 134, 21 );
      subtotalJLabel.setText( "Subtotal:" );
      container.add( subtotalJLabel );

      // set up subtotalJTextField
      subtotalJTextField = new JTextField();
      subtotalJTextField.setBounds( 350, 340, 56, 21 );
      subtotalJTextField.setText( "£0.00" );
      subtotalJTextField.setEditable( false );
      subtotalJTextField.setHorizontalAlignment( JTextField.CENTER );
      container.add( subtotalJTextField );
      
      // set up vatJLabel
      vatJLabel = new JLabel();
      vatJLabel.setBounds( 250, 380, 134, 21 );
      vatJLabel.setText( "VAT 17.5%:" );
      container.add( vatJLabel );

      // set up vatJTextField
      vatJTextField = new JTextField();
      vatJTextField.setBounds( 350, 380, 60, 21 );
      vatJTextField.setText( "£0.00" );
      vatJTextField.setEditable( false );
      vatJTextField.setHorizontalAlignment( JTextField.CENTER );
      container.add( vatJTextField );
      
      // set up totalJLabel
      totalJLabel = new JLabel();
      totalJLabel.setBounds( 250, 420, 134, 21 );
      totalJLabel.setText( "Total:" );
      container.add( totalJLabel );

      // set up totalJTextField
      totalJTextField = new JTextField();
      totalJTextField.setBounds( 350, 420, 60, 21 );
      totalJTextField.setText( "£0.00" );
      totalJTextField.setEditable( false );
      totalJTextField.setHorizontalAlignment( JTextField.CENTER );
      container.add( totalJTextField );
      
      // set up calculateJButton
      calculateJButton = new JButton();
      calculateJButton.setBounds( 325, 460, 90, 24 );
      calculateJButton.setText( "Calculate" );
      container.add( calculateJButton );
  
      // set properties of application’s window
      setTitle( "Cool Computer Sales" ); // set title bar text
      setSize( 600, 600 );        // set window size
      setVisible( true );         // display window

   } // end method createUserInterface

 

   // main method
   public static void main( String[] args )
   {
      computer application = new computer();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

   } // end method main

} // end class computer

Code tags added. -Narue

Recommended Answers

All 5 Replies

I would do something like this:

public void actionPerformed(ActionEvent ae)
{
  if (ae.getSource() == theCalculateButton)
  {
     int comp = Integer.parseInt(computerQuanityJTextField.getText());
     int print = Integer.parseInt(printerQuanityJTextField.getText());
     double tax = .175;
     totalJTextField.setText("" +getCost(comp,print,tax));
  }
}


public double getCost(int compQuanity, int printQuanity, double tax)
{
  double totalCost = compQuanity * 350;
  totalCost += printQuanity * 125;
  totalCost += totalCost * tax;
  
  return totalCost;
}

Although, I would use some constants for the Computer cost, the printer cost, and the tax.

By the way, very nice layout of your program.

Thanks for the reply. unfortunatly the code submited wouldnt compile. But after a few late nights and abit of lecturer interaction, i managed to finish the program, which calculated the vat on the products and enforced user validation on all JTextFields and the Computer JCheckBox.

Here's the finsihed code for any one who's interested.

Cheers.

// Exercivat = subtotal * 0.175;ext ( total );ables users to calculate vat.
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;

public class orderForm extends JFrame
{
    //JLabel for business name
    private JLabel businessNameJLabel;
    
    //JPicture
    private JLabel computerJLabel; // JLabel that displays an image
    
    //JPicture
    private JLabel computer2JLabel; // JLabel that displays an image
    
    // JLabel and JTextField for order number
   private JLabel orderJLabel;
   private JTextField orderJTextField;

   // JLabel and JTextField for name
   private JLabel nameJLabel;
   private JTextField nameJTextField;

   // JLabel and JTextField for addressline1
   private JLabel addressLine1JLabel;
   private JTextField addressLine1JTextField;

   // JLabel and JTextField for addressLine2
   private JLabel addressLine2JLabel;
   private JTextField addressLine2JTextField;
   
   //JLabel for product name
   private JLabel productNameJLabel;
    
   //JLabel for productQuantity
   private JLabel productQuantityJLabel;
   
    //JLabel for productPrice
   private JLabel productPriceJLabel;
   
    //JLabel for orderTotals
   private JLabel orderTotalsJLabel;
    
   // JCheckBox and JLabel for computer
   private JCheckBox computerJCheckBox;
   private JLabel computerPriceJLabel;
   
   // JCheckBox and JLabel for printer
   private JCheckBox printerJCheckBox;
   private JLabel printerPriceJLabel;
   
   //JTextField for computerquantity
   private JTextField computerquantityJTextField;
   
   //JTextField for printerquantity
   private JTextField printerquantityJTextField;
   
   //JTextField for computerprice
   private JTextField computerpriceJTextField;
   
   //JTextField for printerprice
   private JTextField printerpriceJTextField;
   
   //JTextField for computerTotals
   private JTextField computerTotalsJTextField;
   
   //JTextField for printerTotals
   private JTextField printerTotalsJTextField;
   
   // JLabel and JTextField for subtotal
   private JLabel subtotalJLabel;
   private JTextField subtotalJTextField;
   
   // JLabel and JTextField for vat
   private JLabel vatJLabel;
   private JTextField vatJTextField;
   
   // JLabel and JTextField for total
   private JLabel totalJLabel;
   private JTextField totalJTextField;
   
   // JButton to initiate calculation
   private JButton calculateJButton;
   
   
   // no-argument constructor
   public orderForm()
   {
      createUserInterface();
   }

   // create and position GUI components
   public void createUserInterface()
   {
      // get content pane and set its layout
      Container container = getContentPane();
      container.setLayout( null );
      
      // set up businessNameJLabel
      businessNameJLabel = new JLabel();
      businessNameJLabel.setText( "Cool Computer Sales" );
      businessNameJLabel.setLocation( 35, 20 );
      businessNameJLabel.setSize( 550, 88 );
      businessNameJLabel.setFont( new Font( "SansSerif", Font.PLAIN, 36 ) );
      businessNameJLabel.setHorizontalAlignment( JLabel.CENTER );
      container.add( businessNameJLabel );
      
      // set up pictureJLabel
      computerJLabel = new JLabel();
      computerJLabel.setIcon( new ImageIcon( "computer.jpg" ) );
      computerJLabel.setBounds( 10, 30, 100, 100 );
      computerJLabel.setHorizontalAlignment( JLabel.CENTER );
      container.add( computerJLabel );

       // set up pictureJLabel
      computer2JLabel = new JLabel();
      computer2JLabel.setIcon( new ImageIcon( "computer.jpg" ) );
      computer2JLabel.setBounds( 510, 30, 100, 100 );
      computer2JLabel.setHorizontalAlignment( JLabel.CENTER );
      container.add( computer2JLabel );
      
       // set up orderJLabel
      orderJLabel = new JLabel();
      orderJLabel.setBounds( 26, 170, 134, 21 );
      orderJLabel.setText( "Order No:" );
      container.add( orderJLabel );

      // set up orderJTextField
      orderJTextField = new JTextField();
      orderJTextField.setBounds( 120, 170, 40, 21 );
      orderJTextField.setText( "" );
      container.add( orderJTextField );

      // set up nameJLabel
      nameJLabel = new JLabel();
      nameJLabel.setBounds( 26, 200, 130, 21 );
      nameJLabel.setText( "Name:" );
      container.add( nameJLabel );

      // set up nameJTextField
      nameJTextField = new JTextField();
      nameJTextField.setBounds( 120, 200, 220, 21 );
      nameJTextField.setText( "" );
      container.add( nameJTextField );

      // set up addressLine1JLabel
      addressLine1JLabel = new JLabel();
      addressLine1JLabel.setBounds( 26, 230, 120, 21 );
      addressLine1JLabel.setText( "Address Line 1:" );
      container.add( addressLine1JLabel );

      // set up addressLine1JTextField
      addressLine1JTextField = new JTextField();
      addressLine1JTextField.setBounds( 120, 230, 220, 21 );
      addressLine1JTextField.setText( "" );
      container.add( addressLine1JTextField );
      
      // set up addressLine2JLabel
      addressLine2JLabel = new JLabel();
      addressLine2JLabel.setBounds( 26, 260, 120, 21 );
      addressLine2JLabel.setText( "Address Line 2:" );
      container.add( addressLine2JLabel );

      // set up addressLine2JTextField
      addressLine2JTextField = new JTextField();
      addressLine2JTextField.setBounds( 120, 260, 220, 21 );
      addressLine2JTextField.setText( "" );
      container.add( addressLine2JTextField );
      
       // set up productNameJLabel
      productNameJLabel = new JLabel();
      productNameJLabel.setBounds( 26, 315, 64, 21 );
      productNameJLabel.setText( "Product" );
      container.add( productNameJLabel );
      
      // set up productQuantityJLabel
      productQuantityJLabel = new JLabel();
      productQuantityJLabel.setBounds( 160, 315, 64, 21 );
      productQuantityJLabel.setText( "Quantity" );
      container.add( productQuantityJLabel );
      
       // set up productPriceJLabel
      productPriceJLabel = new JLabel();
      productPriceJLabel.setBounds( 260, 315, 64, 21 );
      productPriceJLabel.setText( "Price" );
      container.add( productPriceJLabel );
      
       // set up orderTotalsJLabel
      orderTotalsJLabel = new JLabel();
      orderTotalsJLabel.setBounds( 360, 315, 64, 21 );
      orderTotalsJLabel.setText( "Totals" );
      container.add( orderTotalsJLabel );
      
      // set up computerJCheckBox
      computerJCheckBox = new JCheckBox();
      computerJCheckBox.setBounds( 26, 350, 122, 24 );
      computerJCheckBox.setText( "Computer" );
      container.add( computerJCheckBox );
      
       // set up printerJCheckBox
      printerJCheckBox = new JCheckBox();
      printerJCheckBox.setBounds( 26, 390, 122, 24 );
      printerJCheckBox.setText( "Printer" );
      container.add( printerJCheckBox );
      
      // set up computerquantityJTextField
      computerquantityJTextField = new JTextField();
      computerquantityJTextField.setBounds( 160, 350, 56, 21 );
      computerquantityJTextField.setText( "0" );
      computerquantityJTextField.setHorizontalAlignment( JTextField.CENTER );
      container.add( computerquantityJTextField );
      
       // set up printerquantityJTextField
      printerquantityJTextField = new JTextField();
      printerquantityJTextField.setBounds( 160, 390, 56, 21 );
      printerquantityJTextField.setText( "0" );
      printerquantityJTextField.setHorizontalAlignment( JTextField.CENTER );
      container.add( printerquantityJTextField );
      
      
      // set up computerpriceJTextField
      computerpriceJTextField = new JTextField();
      computerpriceJTextField.setBounds( 260, 350, 56, 21 );
      computerpriceJTextField.setText( "$350.00" );
      computerpriceJTextField.setEditable( false );
      computerpriceJTextField.setHorizontalAlignment( JTextField.CENTER );
      container.add( computerpriceJTextField );
      
      // set up printerpriceJTextField
      printerpriceJTextField = new JTextField();
      printerpriceJTextField.setBounds( 260, 390, 56, 21 );
      printerpriceJTextField.setText( "$125.00" );
      printerpriceJTextField.setEditable( false );
      printerpriceJTextField.setHorizontalAlignment( JTextField.CENTER );
      container.add( printerpriceJTextField );
   
      // set up computerTotalsJTextField
      computerTotalsJTextField = new JTextField();
      computerTotalsJTextField.setBounds( 360, 350, 56, 21 );
      computerTotalsJTextField.setText( "$0.00" );
      computerTotalsJTextField.setEditable( false );
      computerTotalsJTextField.setHorizontalAlignment( JTextField.CENTER );
      container.add( computerTotalsJTextField );
      
       // set up printerTotalsJTextField
      printerTotalsJTextField = new JTextField();
      printerTotalsJTextField.setBounds( 360, 390, 56, 21 );
      printerTotalsJTextField.setText( "$0.00" );
      printerTotalsJTextField.setEditable( false );
      printerTotalsJTextField.setHorizontalAlignment( JTextField.CENTER );
      container.add( printerTotalsJTextField );
      
       // set up subtotalJLabel
      subtotalJLabel = new JLabel();
      subtotalJLabel.setBounds( 260, 430, 134, 21 );
      subtotalJLabel.setText( "Subtotal:" );
      container.add( subtotalJLabel );

      // set up subtotalJTextField
      subtotalJTextField = new JTextField();
      subtotalJTextField.setBounds( 360, 430, 56, 21 );
      subtotalJTextField.setText( "$0.00" );
      subtotalJTextField.setEditable( false );
      subtotalJTextField.setHorizontalAlignment( JTextField.CENTER );
      container.add( subtotalJTextField );
      
      // set up vatJLabel
      vatJLabel = new JLabel();
      vatJLabel.setBounds( 260, 470, 134, 21 );
      vatJLabel.setText( "VAT 17.5%:" );
      container.add( vatJLabel );

      // set up vatJTextField
      vatJTextField = new JTextField();
      vatJTextField.setBounds( 360, 470, 60, 21 );
      vatJTextField.setText( "$0.00" );
      vatJTextField.setEditable( false );
      vatJTextField.setHorizontalAlignment( JTextField.CENTER );
      container.add( vatJTextField );
      
      // set up totalJLabel
      totalJLabel = new JLabel();
      totalJLabel.setBounds( 260, 510, 134, 21 );
      totalJLabel.setText( "Total:" );
      container.add( totalJLabel );

      // set up totalJTextField
      totalJTextField = new JTextField();
      totalJTextField.setBounds( 360, 510, 60, 21 );
      totalJTextField.setText( "$0.00" );
      totalJTextField.setEditable( false );
      totalJTextField.setHorizontalAlignment( JTextField.CENTER );
      container.add( totalJTextField );
      
      // set up calculateJButton
      calculateJButton = new JButton();
      calculateJButton.setBounds( 335, 550, 90, 24 );
      calculateJButton.setText( "Calculate" );
      container.add( calculateJButton );
      
      calculateJButton.addActionListener(
      
        new ActionListener()
        {
        public void actionPerformed (ActionEvent event)
            {
                calculateJButtonActionPerformed (event);
            }
       }
    );     
        
      // set properties of application’s window
      setTitle( "Cool Computer Sales" ); // set title bar text
      setSize( 650, 650 );        // set window size
      setVisible( true );         // display window

   } // end method createUserInterface

// calculate total of order

 private void calculateJButtonActionPerformed ( ActionEvent event )
 {
  
  // declare 
  String orderNumber = orderJTextField.getText();
  String name = nameJTextField.getText();
  String addressLine1 = addressLine1JTextField.getText();
  String addressLine2 = addressLine2JTextField.getText();
  
  double computerTotals = 0.0;
  double printerTotals = 0.0;
  double subtotal = 0.0;
  double vat = 0.0;
  double total =0.0;
  

     // display error message if no name entered or 
      // no JCheckBox is selected
      if ( ( orderNumber.equals( "" ) ) ||
         ( ( name.equals( "" ) ) ||
         ( ( addressLine1.equals( "" ) ) ||
         ( ( addressLine2.equals( "" ) ) ||
         ( !computerJCheckBox.isSelected() ) ) ) ) )
      {
         // display error message
         JOptionPane.showMessageDialog( null,
            "Please enter a Order No, Name, Address details and check at least one item.",
            "Missing Information", JOptionPane.ERROR_MESSAGE );
      }
      else // otherwise, do calculations
      {

        DecimalFormat dollars = new DecimalFormat ("$0.00");
        computerTotals = Integer.parseInt(
        computerquantityJTextField.getText() ) * 350.00;
        printerTotals = Integer.parseInt(
        printerquantityJTextField.getText() ) * 125.00;
         
        computerTotals = computerTotals;  
        computerTotalsJTextField.setText ( dollars.format(computerTotals));
        
        printerTotals = printerTotals;  
        printerTotalsJTextField.setText ( dollars.format(printerTotals));
   
        subtotal = computerTotals + printerTotals; 
        subtotalJTextField.setText ( dollars.format(subtotal ));
         
        vat = subtotal * 0.175;
        vatJTextField.setText ( dollars.format( vat ));
  
        total = subtotal + vat;
        totalJTextField.setText ( dollars.format( total ));
         
         
     }
         
}

   // main method
   public static void main( String[] args )
   {
      orderForm application = new orderForm();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

   } // end method main

} // end class orderForm

:cheesy: :cheesy: :cheesy:

Code tags added. -Narue

Please use code tags when posting any length of code. However, it becomes more important when you post a lot of code. I've added tags to both of your posts, but for further posts it's up to you.

please help
could any one help me
how to design the UML for this program.

// Exercivat = subtotal * 0.175;ext ( total );ables users to calculate vat.
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;


public class orderForm extends JFrame
{
//JLabel for business name
private JLabel businessNameJLabel;


//JPicture
private JLabel computerJLabel; // JLabel that displays an image


//JPicture
private JLabel computer2JLabel; // JLabel that displays an image


// JLabel and JTextField for order number
private JLabel orderJLabel;
private JTextField orderJTextField;


// JLabel and JTextField for name
private JLabel nameJLabel;
private JTextField nameJTextField;


// JLabel and JTextField for addressline1
private JLabel addressLine1JLabel;
private JTextField addressLine1JTextField;


// JLabel and JTextField for addressLine2
private JLabel addressLine2JLabel;
private JTextField addressLine2JTextField;


//JLabel for product name
private JLabel productNameJLabel;


//JLabel for productQuantity
private JLabel productQuantityJLabel;


//JLabel for productPrice
private JLabel productPriceJLabel;


//JLabel for orderTotals
private JLabel orderTotalsJLabel;


// JCheckBox and JLabel for computer
private JCheckBox computerJCheckBox;
private JLabel computerPriceJLabel;


// JCheckBox and JLabel for printer
private JCheckBox printerJCheckBox;
private JLabel printerPriceJLabel;


//JTextField for computerquantity
private JTextField computerquantityJTextField;


//JTextField for printerquantity
private JTextField printerquantityJTextField;


//JTextField for computerprice
private JTextField computerpriceJTextField;


//JTextField for printerprice
private JTextField printerpriceJTextField;


//JTextField for computerTotals
private JTextField computerTotalsJTextField;


//JTextField for printerTotals
private JTextField printerTotalsJTextField;


// JLabel and JTextField for subtotal
private JLabel subtotalJLabel;
private JTextField subtotalJTextField;


// JLabel and JTextField for vat
private JLabel vatJLabel;
private JTextField vatJTextField;


// JLabel and JTextField for total
private JLabel totalJLabel;
private JTextField totalJTextField;


// JButton to initiate calculation
private JButton calculateJButton;



// no-argument constructor
public orderForm()
{
createUserInterface();
}


// create and position GUI components
public void createUserInterface()
{
// get content pane and set its layout
Container container = getContentPane();
container.setLayout( null );


// set up businessNameJLabel
businessNameJLabel = new JLabel();
businessNameJLabel.setText( "Cool Computer Sales" );
businessNameJLabel.setLocation( 35, 20 );
businessNameJLabel.setSize( 550, 88 );
businessNameJLabel.setFont( new Font( "SansSerif", Font.PLAIN, 36 ) );
businessNameJLabel.setHorizontalAlignment( JLabel.CENTER );
container.add( businessNameJLabel );


// set up pictureJLabel
computerJLabel = new JLabel();
computerJLabel.setIcon( new ImageIcon( "computer.jpg" ) );
computerJLabel.setBounds( 10, 30, 100, 100 );
computerJLabel.setHorizontalAlignment( JLabel.CENTER );
container.add( computerJLabel );


// set up pictureJLabel
computer2JLabel = new JLabel();
computer2JLabel.setIcon( new ImageIcon( "computer.jpg" ) );
computer2JLabel.setBounds( 510, 30, 100, 100 );
computer2JLabel.setHorizontalAlignment( JLabel.CENTER );
container.add( computer2JLabel );


// set up orderJLabel
orderJLabel = new JLabel();
orderJLabel.setBounds( 26, 170, 134, 21 );
orderJLabel.setText( "Order No:" );
container.add( orderJLabel );


// set up orderJTextField
orderJTextField = new JTextField();
orderJTextField.setBounds( 120, 170, 40, 21 );
orderJTextField.setText( "" );
container.add( orderJTextField );


// set up nameJLabel
nameJLabel = new JLabel();
nameJLabel.setBounds( 26, 200, 130, 21 );
nameJLabel.setText( "Name:" );
container.add( nameJLabel );


// set up nameJTextField
nameJTextField = new JTextField();
nameJTextField.setBounds( 120, 200, 220, 21 );
nameJTextField.setText( "" );
container.add( nameJTextField );


// set up addressLine1JLabel
addressLine1JLabel = new JLabel();
addressLine1JLabel.setBounds( 26, 230, 120, 21 );
addressLine1JLabel.setText( "Address Line 1:" );
container.add( addressLine1JLabel );


// set up addressLine1JTextField
addressLine1JTextField = new JTextField();
addressLine1JTextField.setBounds( 120, 230, 220, 21 );
addressLine1JTextField.setText( "" );
container.add( addressLine1JTextField );


// set up addressLine2JLabel
addressLine2JLabel = new JLabel();
addressLine2JLabel.setBounds( 26, 260, 120, 21 );
addressLine2JLabel.setText( "Address Line 2:" );
container.add( addressLine2JLabel );


// set up addressLine2JTextField
addressLine2JTextField = new JTextField();
addressLine2JTextField.setBounds( 120, 260, 220, 21 );
addressLine2JTextField.setText( "" );
container.add( addressLine2JTextField );


// set up productNameJLabel
productNameJLabel = new JLabel();
productNameJLabel.setBounds( 26, 315, 64, 21 );
productNameJLabel.setText( "Product" );
container.add( productNameJLabel );


// set up productQuantityJLabel
productQuantityJLabel = new JLabel();
productQuantityJLabel.setBounds( 160, 315, 64, 21 );
productQuantityJLabel.setText( "Quantity" );
container.add( productQuantityJLabel );


// set up productPriceJLabel
productPriceJLabel = new JLabel();
productPriceJLabel.setBounds( 260, 315, 64, 21 );
productPriceJLabel.setText( "Price" );
container.add( productPriceJLabel );


// set up orderTotalsJLabel
orderTotalsJLabel = new JLabel();
orderTotalsJLabel.setBounds( 360, 315, 64, 21 );
orderTotalsJLabel.setText( "Totals" );
container.add( orderTotalsJLabel );


// set up computerJCheckBox
computerJCheckBox = new JCheckBox();
computerJCheckBox.setBounds( 26, 350, 122, 24 );
computerJCheckBox.setText( "Computer" );
container.add( computerJCheckBox );


// set up printerJCheckBox
printerJCheckBox = new JCheckBox();
printerJCheckBox.setBounds( 26, 390, 122, 24 );
printerJCheckBox.setText( "Printer" );
container.add( printerJCheckBox );


// set up computerquantityJTextField
computerquantityJTextField = new JTextField();
computerquantityJTextField.setBounds( 160, 350, 56, 21 );
computerquantityJTextField.setText( "0" );
computerquantityJTextField.setHorizontalAlignment( JTextField.CENTER );
container.add( computerquantityJTextField );


// set up printerquantityJTextField
printerquantityJTextField = new JTextField();
printerquantityJTextField.setBounds( 160, 390, 56, 21 );
printerquantityJTextField.setText( "0" );
printerquantityJTextField.setHorizontalAlignment( JTextField.CENTER );
container.add( printerquantityJTextField );



// set up computerpriceJTextField
computerpriceJTextField = new JTextField();
computerpriceJTextField.setBounds( 260, 350, 56, 21 );
computerpriceJTextField.setText( "$350.00" );
computerpriceJTextField.setEditable( false );
computerpriceJTextField.setHorizontalAlignment( JTextField.CENTER );
container.add( computerpriceJTextField );


// set up printerpriceJTextField
printerpriceJTextField = new JTextField();
printerpriceJTextField.setBounds( 260, 390, 56, 21 );
printerpriceJTextField.setText( "$125.00" );
printerpriceJTextField.setEditable( false );
printerpriceJTextField.setHorizontalAlignment( JTextField.CENTER );
container.add( printerpriceJTextField );


// set up computerTotalsJTextField
computerTotalsJTextField = new JTextField();
computerTotalsJTextField.setBounds( 360, 350, 56, 21 );
computerTotalsJTextField.setText( "$0.00" );
computerTotalsJTextField.setEditable( false );
computerTotalsJTextField.setHorizontalAlignment( JTextField.CENTER );
container.add( computerTotalsJTextField );


// set up printerTotalsJTextField
printerTotalsJTextField = new JTextField();
printerTotalsJTextField.setBounds( 360, 390, 56, 21 );
printerTotalsJTextField.setText( "$0.00" );
printerTotalsJTextField.setEditable( false );
printerTotalsJTextField.setHorizontalAlignment( JTextField.CENTER );
container.add( printerTotalsJTextField );


// set up subtotalJLabel
subtotalJLabel = new JLabel();
subtotalJLabel.setBounds( 260, 430, 134, 21 );
subtotalJLabel.setText( "Subtotal:" );
container.add( subtotalJLabel );


// set up subtotalJTextField
subtotalJTextField = new JTextField();
subtotalJTextField.setBounds( 360, 430, 56, 21 );
subtotalJTextField.setText( "$0.00" );
subtotalJTextField.setEditable( false );
subtotalJTextField.setHorizontalAlignment( JTextField.CENTER );
container.add( subtotalJTextField );


// set up vatJLabel
vatJLabel = new JLabel();
vatJLabel.setBounds( 260, 470, 134, 21 );
vatJLabel.setText( "VAT 17.5%:" );
container.add( vatJLabel );


// set up vatJTextField
vatJTextField = new JTextField();
vatJTextField.setBounds( 360, 470, 60, 21 );
vatJTextField.setText( "$0.00" );
vatJTextField.setEditable( false );
vatJTextField.setHorizontalAlignment( JTextField.CENTER );
container.add( vatJTextField );


// set up totalJLabel
totalJLabel = new JLabel();
totalJLabel.setBounds( 260, 510, 134, 21 );
totalJLabel.setText( "Total:" );
container.add( totalJLabel );


// set up totalJTextField
totalJTextField = new JTextField();
totalJTextField.setBounds( 360, 510, 60, 21 );
totalJTextField.setText( "$0.00" );
totalJTextField.setEditable( false );
totalJTextField.setHorizontalAlignment( JTextField.CENTER );
container.add( totalJTextField );


// set up calculateJButton
calculateJButton = new JButton();
calculateJButton.setBounds( 335, 550, 90, 24 );
calculateJButton.setText( "Calculate" );
container.add( calculateJButton );


calculateJButton.addActionListener(


new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
calculateJButtonActionPerformed (event);
}
}
);


// set properties of application’s window
setTitle( "Cool Computer Sales" ); // set title bar text
setSize( 650, 650 );        // set window size
setVisible( true );         // display window


} // end method createUserInterface


// calculate total of order


private void calculateJButtonActionPerformed ( ActionEvent event )
{


// declare
String orderNumber = orderJTextField.getText();
String name = nameJTextField.getText();
String addressLine1 = addressLine1JTextField.getText();
String addressLine2 = addressLine2JTextField.getText();


double computerTotals = 0.0;
double printerTotals = 0.0;
double subtotal = 0.0;
double vat = 0.0;
double total =0.0;



// display error message if no name entered or
// no JCheckBox is selected
if ( ( orderNumber.equals( "" ) ) ||
( ( name.equals( "" ) ) ||
( ( addressLine1.equals( "" ) ) ||
( ( addressLine2.equals( "" ) ) ||
( !computerJCheckBox.isSelected() ) ) ) ) )
{
// display error message
JOptionPane.showMessageDialog( null,
"Please enter a Order No, Name, Address details and check at least one item.",
"Missing Information", JOptionPane.ERROR_MESSAGE );
}
else // otherwise, do calculations
{


DecimalFormat dollars = new DecimalFormat ("$0.00");
computerTotals = Integer.parseInt(
computerquantityJTextField.getText() ) * 350.00;
printerTotals = Integer.parseInt(
printerquantityJTextField.getText() ) * 125.00;


computerTotals = computerTotals;
computerTotalsJTextField.setText ( dollars.format(computerTotals));


printerTotals = printerTotals;
printerTotalsJTextField.setText ( dollars.format(printerTotals));


subtotal = computerTotals + printerTotals;
subtotalJTextField.setText ( dollars.format(subtotal ));


vat = subtotal * 0.175;
vatJTextField.setText ( dollars.format( vat ));


total = subtotal + vat;
totalJTextField.setText ( dollars.format( total ));



}


}


// main method
public static void main( String[] args )
{
orderForm application = new orderForm();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );


} // end method main


} // end class orderForm
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.