Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Point of Sale UI in JAVA Point of sale interfaces are designed to simplify the p

ID: 3693864 • Letter: P

Question

Point of Sale UI in JAVA

Point of sale interfaces are designed to simplify the process of making transactions, often in a retail environment. We often see them used in restaurants where managers can input the menu and waiters can quickly enter customer orders, which are transmitted to the kitchen and recorded. Modern systems usually include a touchscreen interface, which we will simulate with a mouse-based GUI.

The program you should design and build will read a menu from a text file formatted with a menu item and a price separated by a |. To simplify your text-parsing code, we will omit the dollar sign from the price.

For example:

The program should load the file at launch and create a panel full of large buttons (ideal for a touchscreen) for each menu item. A waiter should be able to click on each menu item to add it to the current order. This should add the item to a receipt panel which displays the full order and the total cost. The total cost should be accurate at all times, updated as each item is added (not only when the order is finalized).

The system only takes credit card as payment type however it can handle/validate multiple types of credit cards. (Please see credit card section below).

The waiter should be able to click a “Place Order” button that simulates transmitting the order to the kitchen by printing the order to System.out (in addition to showing the confirmation on screen). You should also include a “Clear” button that will clear the current order (used when a waiter makes a mistake and needs to start over).

Credit Card

In your system you have the following class structure for the credit cards:
     a class CreditCard,
     classes VisaCC, MasterCC, AmExCC that are all subclasses of CreditCard,
     you assume more subclasses for other credit card types will be added later on.
You now have to design the method(s) (and maybe additional classes) that verifies that the credit card number is a possible account number, and creates an instance of the appropriate credit card class.


Important details: Credit card numbers cannot exceed 19 digits, including a single check digit in the rightmost position. The exact algorithm for calculating the check digit as defined in ISO 2894/ANSI 4.13 is not important for this assignment. You can also determine the card issuer based on the credit card number:

Hint: you face here (at least) two problems, one has to do with how you figure out what kind of card a specific record is about, the other one with how you create the appropriate objects. Look at behavioural patterns and at creational patterns.

Starter codes:

public class McPatterns {
public static void main(String[] args){
McPatternsGUI gui = new McPatternsGUI(new McPatternsPresenter());
}
}

--------------------------------------------------------------------------------------------------------------

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class McPatternsGUI extends JFrame {
   McPatternsPresenter presenter;
  
   public McPatternsGUI(McPatternsPresenter presenter) {
      
       this.presenter = presenter;
       presenter.attachView(this);
       showGUI();

   }
   private void showGUI() {
       presenter.loadMenuItems();

       JFrame theFrame = new JFrame("Swing Example");
       theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       theFrame.setLayout(new BorderLayout());
      
       JPanel title = new JPanel(new FlowLayout());
       title.add(new JLabel("Welcome to McPatterns"));

       JPanel orderPane = new JPanel();
       orderPane.setLayout(new BoxLayout(orderPane, BoxLayout.PAGE_AXIS));
       JLabel orderDetails = new JLabel("Your order");
       orderPane.setBorder(BorderFactory.createRaisedBevelBorder());
       orderPane.add(orderDetails);
       JTextField ccEntry = new JTextField("Enter CC #");

       JButton confirm = new JButton("Place Order");
       confirm.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               //TODO: Add the function to handle confirmed order
               //Think about where you will store order and who should manipulate.
               //Handle the Payment validation before confirming order. Who should validate?

               orderDetails.setText("Order confrimed for " + ccEntry.getText());
           }

       });
       JButton cancel = new JButton("Cancel Order");
       cancel.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               //TODO: Add the function to handle cancel order
               //Think about where you will store order and who should manipulate.

               orderDetails.setText("Order cancelled");
           }

       });

      
       orderPane.add(ccEntry);
       orderPane.add(confirm);
       orderPane.add(cancel);

       JPanel buttonPanel = new JPanel();
       buttonPanel.setLayout(new FlowLayout());
      // TODO: Ask the presenter for the buttons to create. Iterate over the buttons and create them
       buttonPanel.add(new JButton("Replace with actual Menu Items"));
      
       theFrame.add(title,BorderLayout.NORTH);
       theFrame.add(buttonPanel, BorderLayout.CENTER);
       theFrame.add(orderPane, BorderLayout.EAST);
       theFrame.setSize(800,600);
      

      
       theFrame.pack();
       theFrame.setVisible(true);
      
   }
}

--------------------------------------------------------------------------------------------------------------

class McPatternsPresenter {
//This is the class that will handle the model <-> UI communication.
MenuModel model;
McPatternsGUI view;

void loadMenuItems() {
// TODO: Add code to read a file and load the menu items.
}

void attachView(McPatternsGUI view) {
this.view = view;
}
// Add functions to return the menu items.
}

--------------------------------------------------------------------------------------------------------------

class MenuModel {
// Add your implementation for Menu Items here.
// Determine what data you want to store for each item.

}

MasterCard First digit is a 5, second digit is in range 1 through 5 inclusive. Only valid lenght of number is 16 digits. Visa First digit is a 4. Length is either 13 or 16 digits. AmericanExpress First digit is a 3 and second digit a 4 or 7. Length is 15 digits. Discover First four digits are 6011. Length is 16 digits.

Explanation / Answer

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

class McPatternsGUI extends JFrame {
   McPatternsPresenter presenter;
   CreditCardFactory ccFactory;

   public McPatternsGUI(McPatternsPresenter presenter) {

       this.presenter = presenter;
       presenter.attachView(this);
       showGUI();

   }

   private void showGUI() {
       presenter.loadMenuItems();

       // JFRAME
       JFrame theFrame = new JFrame("Navjot's Store");
       theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       theFrame.setLayout(new BorderLayout());
       Color blue = Color.decode("#e5e5ff");
       theFrame.getContentPane().setBackground(blue);

       // JPANEL
       JPanel title = new JPanel(new FlowLayout());
       title.setBackground(blue);
       title.add(new JLabel("Welcome to Navjot's Store!"));

       // ORDER PANE
       JPanel orderPane = new JPanel();
       orderPane.setBackground(blue);
       orderPane.setLayout(new BoxLayout(orderPane, BoxLayout.PAGE_AXIS));
       JLabel orderDetails = new JLabel("Your order");
       orderPane.setBorder(BorderFactory.createRaisedBevelBorder());
       orderPane.add(orderDetails);
       orderPane.setBackground(blue);
       JTextField ccEntry = new JTextField("Enter CC #");

       // TEXT AREA

       JTextArea currentOrder = new JTextArea(20, 20);
       currentOrder
               .setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
       theFrame.add(currentOrder);
       JScrollPane scrollPane = new JScrollPane(currentOrder,
               JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
               JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

       // TOTAL OF ORDER
       JLabel yourTotal = new JLabel("Your total is: $0.00");

       // Confirm Button
       JButton confirm = new JButton("Place Order");
       confirm.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               // TODO: Add the function to handle confirmed order
               // Think about where you will store order and who should
               // manipulate.
               // Handle the Payment validation before confirming order. Who
               // should validate?
               ccFactory = new CreditCardFactory(ccEntry.getText());
               if (ccFactory.verifyCC() == true) {
                   orderDetails.setText(ccFactory.ccName + " card accepted!");
                   System.out.println(presenter.returnOrder());

                   // Reciept Confirmation Frame
                   JFrame recieptFrame = new JFrame("Reciept");
                   // recieptFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   recieptFrame.setLayout(new BorderLayout());
                   recieptFrame.getContentPane().setBackground(blue);
                   JTextArea reciept = new JTextArea(20, 20);
                   reciept.setText(presenter.returnReciept());
                   reciept.setEditable(false);
                   recieptFrame.add(reciept);
                   recieptFrame.setVisible(true);
                   recieptFrame.pack();
               } else {
                   orderDetails.setText("Payment not accepted. Re-enter CC.");
               }
           }
       });

       // Cancel Button
       JButton cancel = new JButton("Cancel Order");
       cancel.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               // TODO: Add the function to handle cancel order
               // Think about where you will store order and who should
               // manipulate.
               orderDetails.setText("Order cancelled");
               currentOrder.setText("");
               ccEntry.setText("Enter CC #");
               presenter.model.order = new ArrayList<String>();
               presenter.model.totalPrice = 0;
               yourTotal.setText("Your total is: $0.00");
           }
       });

       // Clear Button
       JButton clearButton = new JButton("Clear");
       clearButton.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               orderDetails.setText("Your Order");
               currentOrder.setText("");
               ccEntry.setText("Enter CC #");
               presenter.model.order = new ArrayList<String>();
               presenter.model.totalPrice = 0;
               yourTotal.setText("Your total is: $0.00");
           }
       });

       orderPane.add(scrollPane);
       orderPane.add(yourTotal);
       orderPane.add(ccEntry);
       orderPane.add(confirm);
       orderPane.add(cancel);
       orderPane.add(clearButton);

       // BUTTON PANEL
       JPanel buttonPanel = new JPanel();
       buttonPanel.setLayout(new FlowLayout());
       int buttonPanelSize = (presenter.model.menu.size()) / 2;
       buttonPanel.setBackground(blue);
       buttonPanel.setLayout(new GridLayout(buttonPanelSize, buttonPanelSize));

       // TODO: Ask the presenter for the buttons to create. Iterate over the
       // buttons and create them

       // CREATING BUTTONS
       ArrayList buttonsToCreate = presenter.getButtons();
       for (int i = 0; i < buttonsToCreate.size(); i++) {
           JButton currentButton = (JButton) buttonsToCreate.get(i);
           currentButton.setSize(5, 20);
           buttonPanel.add(currentButton);
           currentButton.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                   presenter.addToOrder(((JComponent) e.getSource()).getName());
                   currentOrder.setText(currentOrder.getText() + " "
                           + ((JComponent) e.getSource()).getName());
                   presenter.updatePrice(((AbstractButton) e.getSource())
                           .getActionCommand());
                   yourTotal.setText("Your total is: $"
                           + presenter.returnTotal());
               }
           });
       }

       theFrame.add(title, BorderLayout.NORTH);
       theFrame.add(buttonPanel, BorderLayout.CENTER);
       theFrame.add(orderPane, BorderLayout.EAST);
       theFrame.setSize(800, 600);
       theFrame.pack();
       theFrame.setVisible(true);
   }
}


McPatternsPresenter.java
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JButton;

class McPatternsPresenter {
   // This is the class that will handle the model <-> UI communication.
   MenuModel model;
   McPatternsGUI view;
   String file = null;

   public McPatternsPresenter(String filename) {
       model = new MenuModel();
       file = filename;
   }

   void loadMenuItems() {

       BufferedReader in;
       try {
           in = new BufferedReader(new FileReader(file));

           String str;
           while ((str = in.readLine()) != null) {
               String[] parts = str.split("\|");
               String item = parts[0];
               String priceHold = parts[1];
               String[] newPrice = priceHold.split("\.");
               String price = (newPrice[0] + newPrice[1]);
               int intPrice = Integer.parseInt(price);
               model.addToMenu(item);
               model.addToMenu(priceHold);
               model.addToPriceList(intPrice);
           }
           createButtons();
       } catch (FileNotFoundException e) {
           System.out.println("Cannot find file");
           e.printStackTrace();
       } catch (IOException e) {
           System.out.println("IO Exception");
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
   }

   public void attachView(McPatternsGUI view) {
       this.view = view;
   }

   // Add functions to return the menu items.

   public void createButtons() {
       model.createButtons();
   }

   public ArrayList getButtons() {
       return model.buttonList;
   }

   public ArrayList returnMenu() {
       return model.menu;
   }

   void addToOrder(String item) {
       model.addToOrder(item);
   }

   String returnOrder() {
       return model.getOrder();
   }

   String returnReciept() {
       return model.getReciept();
   }

   public void updatePrice(String aPrice) {
       model.updateTotalPrice(aPrice);
   }

   public String returnTotal() {
       return model.totalPriceString;
   }
}

McPatterns.java
public class McPatterns {
   public static void main(String[] args) {
       String filename = args[0];
       McPatternsGUI gui = new McPatternsGUI(new McPatternsPresenter(filename));
   }
}

MenuModel.java
import java.util.ArrayList;

import javax.swing.JButton;

class MenuModel {
   // Add your implementation for Menu Items here.
   // Determine what data you want to store for each item.
   ArrayList<String> order = new ArrayList<String>();
   ArrayList<Integer> price = new ArrayList<Integer>();
   ArrayList<JButton> buttonList = new ArrayList<JButton>();
   ArrayList<String> menu = new ArrayList<String>();
   int totalPrice = 0;
   String totalPriceString = null;

   public void addToMenu(String newItem) {
       menu.add(newItem);
   }

   public void createButtons() {

       for (int i = 0; i < menu.size(); i += 2) {
           String item = (String) menu.get(i);
           String testPrice = null;
           String price = null;
           if (menu.size() > i + 1) {
               testPrice = menu.get(i + 1);
               String[] newPrice = testPrice.split("\.");
               price = (newPrice[0] + newPrice[1]);

           }
           JButton itemButton = new JButton(item + " - $" + testPrice);
           itemButton.setActionCommand(price);
           itemButton.setName(item + " (" + testPrice + ")");
           buttonList.add(itemButton);
       }

   }

   void addToOrder(String item) {
       order.add(item);
   }

   void addToPriceList(int currentPrice) {
       price.add(currentPrice);
   }

   String getOrder() {
       String orderText = " " + "The order is below: " + " "
               + "--------------------" + " ";
       for (int i = 0; i < order.size(); i++) {
           orderText = orderText + order.get(i) + " ";
       }
       return orderText;
   }

   String getReciept() {
       String orderText = " " + " Store Reciept " + " "
               + "------------------" + " ";
       for (int i = 0; i < order.size(); i++) {
           orderText = orderText + order.get(i) + " ";
       }
       orderText = orderText + " " + "Total Items: " + order.size() + " "
               + "------------------" + " " + "Total: $" + totalPriceString;
       return orderText;
   }

   public void updateTotalPrice(String price) {
       int intPrice = Integer.parseInt(price);
       totalPrice = totalPrice + intPrice;
       String newPrice = Integer.toString(totalPrice);
       totalPriceString = newPrice.substring(0, newPrice.length() - 2) + "."
               + newPrice.substring(newPrice.length() - 2, newPrice.length());
   }
}

CreditCardFactory.java
public class CreditCardFactory {
   AmExCC amex;
   MasterCC master;
   VisaCC visa;
   DiscoverCC discover;
   String ccNumber;
   String ccName = null;

   public CreditCardFactory(String stringCC) {
       ccNumber = stringCC;
       amex = new AmExCC(stringCC);
       master = new MasterCC(stringCC);
       visa = new VisaCC(stringCC);
       discover = new DiscoverCC(stringCC);
   }

   public boolean verifyCC() {
       boolean checker = false;
       if (ccNumber.length() <= 19) {
           if (amex.verifyCreditCard(ccNumber) == true) {
               ccName = "Amex";
               checker = true;
           } else if (master.verifyCreditCard(ccNumber) == true) {
               ccName = "Master";
               checker = true;
           } else if (visa.verifyCreditCard(ccNumber) == true) {
               ccName = "Visa";
               checker = true;
           } else if (discover.verifyCreditCard(ccNumber) == true) {
               ccName = "Discover";
               checker = true;
           }
       }
       return checker;
   }
}


CreditCard.java
public class CreditCard {

   public String creditCardNumber;

   public CreditCard(String number) {
       assignCardNumber(number);
   }

   void assignCardNumber(String number) {
       creditCardNumber = number;
   }

   boolean verifyCreditCard() {
       int length = creditCardNumber.length();
       if (length <= 19) {
           return true;
       }
       return false;
   }
}

AmExCC.java
public class AmExCC extends CreditCard {


   public AmExCC(String number) {
       super(number);
       assignCardNumber(number);
   }

   void assignCardNumber(String number) {
       creditCardNumber = number;
   }

   boolean verifyCreditCard(String ccNumber) {
       int length = ccNumber.length();
       if (length == 15) {
           String firstDigit = ccNumber.substring(0, 1);
           String secondDigit = ccNumber.substring(1, 2);

           if (firstDigit.equals("3")) {

               if (secondDigit.equals("4") || secondDigit.equals("7")) {
                   return true;
               }
           }

       }
       return false;
   }
}

MasterCC.java
public class MasterCC extends CreditCard {

   public MasterCC(String number) {
       super(number);
       assignCardNumber(number);
   }

   void assignCardNumber(String number) {
       creditCardNumber = number;
   }

   boolean verifyCreditCard(String ccNumber) {
       int length = ccNumber.length();
       if (length == 16) {
           //System.out.println("mastercheck");
           String firstDigit = ccNumber.substring(0, 1);
           String secondDigit = ccNumber.substring(1, 2);

           if (firstDigit.equals("5")) {
               if (secondDigit.compareTo("1") >= 0
                       && secondDigit.compareTo("5") <= 0) {
                   return true;
               }
           }
       }

       return false;
   }
}

DiscoverCC.java
public class DiscoverCC extends CreditCard {

   public DiscoverCC(String number) {
       super(number);
       assignCardNumber(number);
   }

   void assignCardNumber(String number) {
       creditCardNumber = number;
   }

   boolean verifyCreditCard(String ccNumber) {
       int length = ccNumber.length();
       if (length == 16) {
           String firstDigit = ccNumber.substring(0, 1);
           String secondDigit = ccNumber.substring(1, 2);
           String thirdDigit = ccNumber.substring(2, 3);
           String fourthDigit = ccNumber.substring(3, 4);

           if (firstDigit.equals("6") && secondDigit.equals("0")
                   && thirdDigit.equals("1") && fourthDigit.equals("1")) {
               return true;
           }
       }
       return false;
   }
}


VisaCC.java
public class VisaCC extends CreditCard {

   public VisaCC(String number) {
       super(number);
       assignCardNumber(number);
   }

   void assignCardNumber(String number) {
       creditCardNumber = number;
   }

   boolean verifyCreditCard(String ccNumber) {
       int length = ccNumber.length();
       if (length == 13 || length == 16) {
           String firstDigit = ccNumber.substring(0, 1);

           if (firstDigit.equals("4")) {
               return true;
           }
       }
       return false;
   }
}


Orders.txt
Royale with cheese|3.99
Le Big Mac|4.99
Milkshake|5.00
McBola|2.99
Pizza|3.87
CheeseSticks|6.99
Soup|4.00
Cookie|2.00
Brownie|2.00
Fries|3.50