Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Scenario
- You are a Junior Developer employed by Turtle Shell Software Solutions who has recently been approached by a local printer firm to, to help them with booking and pricing.
- The local printer company have asked for a calculator to work out the costs per order and size of paper the client might need. They would like you to create a Java Application for booking staff to use.
- The table below details the different costs that need to be considered when creating the application.
- Initial one off Fee A1 A2 A3 A4 How many copies Req.
- Week days £0.40 £1.00 0.65 0.45 0.25
- Weekends £1.00 £1.20 0.75 0.55 0.35
- Urgent Prints £1.80 £2.00 0.95 0.65 0.45
- Requirements of the application
- The application should be able to:
- 1. Allow the user to select the print required and create a subtotal.
- 2. Allow the user to set the amount of copies required.
- 3. Allow the user to set the date of the print and adjust the subtotal accordingly.
- 4. Use a tick box to identify that the date of the booking is an Urgent Print and adjust the cost accordingly.
- 5. Allow the user to input additional detail about the order including name and number then save it into a text file.
- 6. Allow the admin user to view previous bookings that have been saved.
- The quality of the application design and code will also be a factor of the grades
- Allow the user to set number of copies required.
- Calculate the subtotal.
- A select a predefined week day
- Allow the user to set the particular date of the booking and adjust the subtotal accordingly according to paper size
- Use a tick box to identify that the date of the booking is Urgent adjust it can adjust the cost accordingly.
- Allow the client to save the correct transaction cost relating what size paper order, how many sheets were requested and on what part of the week
- Allow the user to input additional detail about the customer including name and number then save it into a text file.
- Allow the admin to view previous bookings that have been saved.
- ----------------------------------------------------------------------------------------------------------------------------------------
- package college.calculator;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JComboBox;
- import javax.swing.JFileChooser;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JCheckBox;
- import javax.swing.JTextField;
- import javax.swing.JPasswordField;
- import javax.swing.JRadioButton;
- import javax.swing.JButton;
- import java.awt.event.ActionListener;
- import java.io.BufferedReader;
- import java.io.FileInputStream;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.nio.charset.StandardCharsets;
- import java.awt.event.ActionEvent;
- import java.awt.Font;
- import javax.swing.DefaultComboBoxModel;
- import javax.swing.SwingConstants;
- public class Gui extends JFrame {
- private JPanel contentPane;
- private JTextField txtLogin;
- private JPasswordField passwordField;
- private JTextField textFieldQntyA1;
- private JTextField textField_name;
- private JTextField textField_number;
- private JTextField textField_subtotal;
- private JTextField textField_initialfee;
- private double sumSubtotal = 0;
- private double initialFee = 0;
- private int q1 = 0, q2 = 0, q3 = 0, q4 = 0; //quantity = 0;
- private JTextField textFieldQntyA2;
- private JTextField textFieldQntyA3;
- private JTextField textFieldQntyA4;
- private JTextField textField_total;
- /**
- * Launch the application.
- */
- /**
- * Create the frame.
- */
- public Gui() {
- setTitle("Calculator");
- JLabel lblPriceA1 = new JLabel("");
- lblPriceA1.setBounds(168, 107, 46, 14);
- JLabel lblPriceA2 = new JLabel("");
- lblPriceA2.setBounds(168, 137, 46, 14);
- JLabel lblPriceA3 = new JLabel("");
- lblPriceA3.setBounds(168, 166, 46, 14);
- JLabel lblPriceA4 = new JLabel("");
- lblPriceA4.setBounds(168, 192, 46, 14);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 399, 484);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- JComboBox cmboSelectDate = new JComboBox(); // create ComboBox
- cmboSelectDate.setEnabled(false);
- cmboSelectDate.setModel(new DefaultComboBoxModel(new String[] {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}));
- cmboSelectDate.setBounds(49, 56, 78, 21);
- contentPane.add(cmboSelectDate);
- JLabel lblSelectPrintDate = new JLabel("Select Print Date OR ");
- lblSelectPrintDate.setBounds(137, 59, 126, 14);
- contentPane.add(lblSelectPrintDate);
- JCheckBox chckbxUrgentPrints = new JCheckBox("Urgent Print");
- chckbxUrgentPrints.setEnabled(false);
- chckbxUrgentPrints.setBounds(256, 55, 97, 23);
- contentPane.add(chckbxUrgentPrints);
- JLabel lblUsername = new JLabel("Username");
- lblUsername.setBounds(10, 15, 78, 14);
- contentPane.add(lblUsername);
- JLabel lblPassword = new JLabel("Password");
- lblPassword.setBounds(161, 15, 68, 14);
- contentPane.add(lblPassword);
- txtLogin = new JTextField();
- txtLogin.setBounds(71, 12, 80, 20);
- contentPane.add(txtLogin);
- txtLogin.setColumns(10);
- passwordField = new JPasswordField();
- passwordField.setToolTipText("");
- passwordField.setBounds(221, 12, 80, 20);
- contentPane.add(passwordField);
- JRadioButton rdbtnA1 = new JRadioButton("A1");
- rdbtnA1.setEnabled(false);
- rdbtnA1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if (rdbtnA1.isSelected()) { //setEnabled true if rdbtnA1 selected
- textFieldQntyA1.setEnabled(true);
- q1=Integer.parseInt(textFieldQntyA1.getText()); // get quantity from textFieldQntyA1
- }else if (!rdbtnA1.isSelected()){ //if rdbtnA1 not selected - textFieldQntyA1 hidden and set textFieldQntyA1 "0"
- textFieldQntyA1.setEnabled(false);
- textFieldQntyA1.setText("0");
- }
- }
- });
- rdbtnA1.setBounds(49, 107, 86, 23);
- contentPane.add(rdbtnA1);
- JRadioButton rdbtnA2 = new JRadioButton("A2");
- rdbtnA2.setEnabled(false);
- rdbtnA2.addActionListener(new ActionListener() { //see rdbtnA1.addActionListener(new ActionListener())
- public void actionPerformed(ActionEvent e) {
- if (rdbtnA2.isSelected()) {
- textFieldQntyA2.setEnabled(true);
- q2=Integer.parseInt(textFieldQntyA2.getText());
- }else if (!rdbtnA2.isSelected()){
- textFieldQntyA2.setEnabled(false);
- textFieldQntyA2.setText("0");
- }
- }
- });
- rdbtnA2.setBounds(49, 133, 86, 23);
- contentPane.add(rdbtnA2);
- JRadioButton rdbtnA3 = new JRadioButton("A3");
- rdbtnA3.setEnabled(false);
- rdbtnA3.addActionListener(new ActionListener() { //see rdbtnA1.addActionListener(new ActionListener())
- public void actionPerformed(ActionEvent e) {
- if (rdbtnA3.isSelected()) {
- textFieldQntyA3.setEnabled(true);
- q3=Integer.parseInt(textFieldQntyA3.getText());
- }else if (!rdbtnA3.isSelected()){
- textFieldQntyA3.setEnabled(false);
- textFieldQntyA3.setText("0");
- }
- }
- });
- rdbtnA3.setBounds(49, 162, 86, 23);
- contentPane.add(rdbtnA3);
- JRadioButton rdbtnA4 = new JRadioButton("A4");
- rdbtnA4.setEnabled(false);
- rdbtnA4.addActionListener(new ActionListener() {//see rdbtnA1.addActionListener(new ActionListener())
- public void actionPerformed(ActionEvent e) {
- if (rdbtnA4.isSelected()) {
- textFieldQntyA4.setEnabled(true);
- q4=Integer.parseInt(textFieldQntyA4.getText());
- }
- else if (!rdbtnA4.isSelected()){
- textFieldQntyA4.setEnabled(false);
- textFieldQntyA4.setText("0");
- }
- }
- });
- rdbtnA4.setBounds(49, 188, 86, 23);
- contentPane.add(rdbtnA4);
- textFieldQntyA1 = new JTextField();
- textFieldQntyA1.setHorizontalAlignment(SwingConstants.CENTER);
- textFieldQntyA1.setEnabled(false);
- textFieldQntyA1.setText("0");
- textFieldQntyA1.setBounds(266, 108, 30, 20);
- contentPane.add(textFieldQntyA1);
- textFieldQntyA1.setColumns(10);
- JLabel lblQuantity = new JLabel("Quantity");
- lblQuantity.setFont(new Font("Tahoma", Font.BOLD, 12));
- lblQuantity.setBounds(252, 88, 57, 14);
- contentPane.add(lblQuantity);
- textField_name = new JTextField();
- textField_name.setEnabled(false);
- textField_name.setBounds(93, 354, 86, 20);
- contentPane.add(textField_name);
- textField_name.setColumns(10);
- textField_number = new JTextField();
- textField_number.setEnabled(false);
- textField_number.setBounds(255, 354, 86, 20);
- contentPane.add(textField_number);
- textField_number.setColumns(10);
- JLabel lblName = new JLabel("Name");
- lblName.setBounds(37, 357, 46, 14);
- contentPane.add(lblName);
- JLabel lblNumber = new JLabel("Number");
- lblNumber.setBounds(199, 357, 46, 14);
- contentPane.add(lblNumber);
- JButton btnSubtotal = new JButton("Calculate");
- btnSubtotal.setEnabled(false);
- btnSubtotal.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- sumSubtotal = 0;
- initialFee = 0;
- double p1 = 0, p2 = 0, p3 = 0, p4 = 0;
- textField_subtotal.setText("");
- textField_initialfee.setText("");
- textField_total.setText("");
- //if one of paper sizes enabled - check combobox or chckbxUrgentPrints and set price p1, p2, p3, p4
- if (textFieldQntyA1.isEnabled()||textFieldQntyA2.isEnabled()||textFieldQntyA3.isEnabled()||textFieldQntyA4.isEnabled()) {
- if (chckbxUrgentPrints.isSelected()) { // urgent prints
- initialFee = 1.80; //set initial fee for urgent prints
- p1 = 2.00; p2 = 0.95; p3 = 0.65; p4 = 0.45;
- }else if (cmboSelectDate.getSelectedIndex() == 5 || cmboSelectDate.getSelectedIndex() == 6) { //Saturday or Sunday
- initialFee = 1.00; //set initial fee on the Saturday or Sunday
- p1 = 1.20; p2 = 0.75; p3 = 0.55; p4 = 0.35; // set price on the Saturday or Sunday
- }else { //other days
- initialFee = 0.40; ////set initial fee on other days
- p1 = 1.00; p2 = 0.65; p3 = 0.45; p4 = 0.25; //set price on other days
- }
- q1=Integer.parseInt(textFieldQntyA1.getText()); //get quantity for A1
- q2=Integer.parseInt(textFieldQntyA2.getText()); //get quantity for A2
- q3=Integer.parseInt(textFieldQntyA3.getText()); //get quantity for A3
- q4=Integer.parseInt(textFieldQntyA4.getText()); //get quantity for A4
- if (q1 < 0 || q2 < 0 || q3 < 0 || q4 < 0) { //if one of the quantities < 0 - show message "Quantity mast be >= 0!"
- JOptionPane.showMessageDialog(null, "Quantity mast be >= 0!");
- }else {
- sumSubtotal += q1*p1 + q2*p2 + q3*p3 + q4*p4; //else calculate subtotal
- }
- }
- lblPriceA1.setText("ВЈ " + String.format("%.2f", p1)); //set A1 price to the label1 (example ВЈ0.25)
- lblPriceA2.setText("ВЈ " + String.format("%.2f", p2)); //set A2 price
- lblPriceA3.setText("ВЈ " + String.format("%.2f", p3)); //set A3 price
- lblPriceA4.setText("ВЈ " + String.format("%.2f", p4)); //set A4 price
- textField_subtotal.setText(String.format("%.2f", sumSubtotal)); //set subtotal
- textField_initialfee.setText(String.format("%.2f",initialFee)); // set initial fee
- textField_total.setText(String.format("%.2f", sumSubtotal+initialFee)); // set subtotal + initial fee
- }
- });
- btnSubtotal.setBounds(156, 300, 89, 23);
- contentPane.add(btnSubtotal);
- JButton btnSave = new JButton("SAVE");
- btnSave.setEnabled(false);
- btnSave.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) { //write to the file orders.txt
- try (FileWriter writer = new FileWriter("src//college//calculator//orders.txt", false)){
- writer.write("Name "+ "Subtotal "+"Initail Fee"+" Total "+"Number " + "\r\n");
- writer.write(textField_name.getText()+" " + textField_subtotal.getText()+" "
- + textField_initialfee.getText()+" " +textField_total.getText()+" "+textField_number.getText());
- writer.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- });
- btnSave.setBounds(151, 408, 89, 23);
- contentPane.add(btnSave);
- JButton btnOpen = new JButton("OPEN");
- btnOpen.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) { // open orders.txt as notepad
- JFileChooser chooser = new JFileChooser();
- int returnVal = chooser.showOpenDialog(null);
- if(returnVal == JFileChooser.APPROVE_OPTION) {
- Runtime runtime = Runtime.getRuntime();
- try {
- runtime.exec("notepad "+ chooser.getSelectedFile().getPath());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- });
- btnOpen.setEnabled(false);
- btnOpen.setBounds(30, 408, 91, 23);
- contentPane.add(btnOpen);
- textField_subtotal = new JTextField();
- textField_subtotal.setHorizontalAlignment(SwingConstants.CENTER);
- textField_subtotal.setEditable(false);
- textField_subtotal.setBounds(49, 252, 50, 20);
- contentPane.add(textField_subtotal);
- textField_subtotal.setColumns(10);
- textField_initialfee = new JTextField();
- textField_initialfee.setHorizontalAlignment(SwingConstants.CENTER);
- textField_initialfee.setEditable(false);
- textField_initialfee.setBounds(159, 252, 50, 20);
- contentPane.add(textField_initialfee);
- textField_initialfee.setColumns(10);
- JButton btnExit = new JButton("EXIT");
- btnExit.setEnabled(false);
- btnExit.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- System.exit(0);
- }
- });
- btnExit.setBounds(270, 408, 91, 23);
- contentPane.add(btnExit);
- JButton btnNewButton = new JButton("login");
- btnNewButton.addActionListener(new ActionListener() { // check user login and password in the file users.txt
- public void actionPerformed(ActionEvent arg0){
- String userName = txtLogin.getText();
- String userPassword = passwordField.getText();
- try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("src//college//calculator//users.txt")))){
- String line;
- boolean flag = false;
- while ((line = reader.readLine()) != null) {
- if((userName + " " + userPassword).equals(line)) {
- cmboSelectDate.setEnabled(true);
- chckbxUrgentPrints.setEnabled(true);
- btnSubtotal.setEnabled(true);
- btnOpen.setEnabled(true);
- btnSave.setEnabled(true);
- btnExit.setEnabled(true);
- rdbtnA1.setEnabled(true);
- rdbtnA2.setEnabled(true);
- rdbtnA3.setEnabled(true);
- rdbtnA4.setEnabled(true);
- textField_name.setEnabled(true);
- textField_number.setEnabled(true);
- JOptionPane.showMessageDialog(null, "Welcome, " + txtLogin.getText() + "!");
- flag = true;
- }
- }
- if (flag == false) { // if user not found in the file users.txt - "Enter correct Username and Password!"
- JOptionPane.showMessageDialog(null, "Enter correct Username and Password!");
- }
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- });
- btnNewButton.setBounds(313, 11, 68, 23);
- contentPane.add(btnNewButton);
- JLabel label = new JLabel("+");
- label.setFont(new Font("Tahoma", Font.PLAIN, 14));
- label.setBounds(122, 250, 10, 21);
- contentPane.add(label);
- JLabel lblPaperSize = new JLabel("Paper Size");
- lblPaperSize.setFont(new Font("Tahoma", Font.BOLD, 12));
- lblPaperSize.setBounds(49, 88, 68, 14);
- contentPane.add(lblPaperSize);
- textFieldQntyA2 = new JTextField();
- textFieldQntyA2.setHorizontalAlignment(SwingConstants.CENTER);
- textFieldQntyA2.setEnabled(false);
- textFieldQntyA2.setText("0");
- textFieldQntyA2.setColumns(10);
- textFieldQntyA2.setBounds(266, 134, 30, 20);
- contentPane.add(textFieldQntyA2);
- textFieldQntyA3 = new JTextField();
- textFieldQntyA3.setHorizontalAlignment(SwingConstants.CENTER);
- textFieldQntyA3.setEnabled(false);
- textFieldQntyA3.setText("0");
- textFieldQntyA3.setColumns(10);
- textFieldQntyA3.setBounds(266, 163, 30, 20);
- contentPane.add(textFieldQntyA3);
- textFieldQntyA4 = new JTextField();
- textFieldQntyA4.setHorizontalAlignment(SwingConstants.CENTER);
- textFieldQntyA4.setEnabled(false);
- textFieldQntyA4.setText("0");
- textFieldQntyA4.setColumns(10);
- textFieldQntyA4.setBounds(266, 189, 30, 20);
- contentPane.add(textFieldQntyA4);
- JLabel label_1 = new JLabel("= \u00A3");
- label_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
- label_1.setBounds(238, 250, 63, 21);
- contentPane.add(label_1);
- JLabel lblSubtotal = new JLabel("Subtotal");
- lblSubtotal.setBounds(49, 227, 69, 14);
- contentPane.add(lblSubtotal);
- JLabel lblInitailFee = new JLabel("Initail fee");
- lblInitailFee.setBounds(161, 227, 58, 14);
- contentPane.add(lblInitailFee);
- JLabel lblTotal = new JLabel("Total");
- lblTotal.setBounds(303, 227, 50, 14);
- contentPane.add(lblTotal);
- textField_total = new JTextField();
- textField_total.setHorizontalAlignment(SwingConstants.CENTER);
- textField_total.setEditable(false);
- textField_total.setBounds(295, 252, 50, 20);
- contentPane.add(textField_total);
- textField_total.setColumns(10);
- JLabel lblPrice = new JLabel("Price");
- lblPrice.setFont(new Font("Tahoma", Font.BOLD, 12));
- lblPrice.setBounds(168, 88, 46, 14);
- contentPane.add(lblPrice);
- contentPane.add(lblPriceA1);
- contentPane.add(lblPriceA2);
- contentPane.add(lblPriceA3);
- contentPane.add(lblPriceA4);
- }
- }
- package college.calculator;
- import java.awt.EventQueue;
- public class Main {
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Gui frame = new Gui();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment