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