So I have to program this MVC GUI of a checkout line. I have a gui that has 10 registers. Each register has a queue of customers. There is a button that when clicked, will generate a customer and the customer will be entered in the line with the shorted amount of people in the line. I have everything coded but the GUI. I'm not sure how to get the customer created.
I guess where I would need help is in the StoreView and StoreController classes. I need help being able to use my methods and classes that I have created.
Here are my classes:
Store:
import java.util.HashSet; public class Store { private HashSet<Register> regs; public Store(){ this.regs = new HashSet<Register>(); for(int i = 0;i<=10; i++){ regs.add(new Register()); } } private Register leastFullRegister() { int minLineSize = Integer.MAX_VALUE; Register leastFullRegister = null; for ( Register r : regs ) { if ( r.lineSize() < minLineSize ) { minLineSize = r.lineSize(); leastFullRegister = r; } } return leastFullRegister; } public String toString(){ String str = ""; for(Register r: regs){ str += r.toString(); } return str; } }
Register:
import java.util.LinkedList; import java.util.Queue; public class Register { private boolean open; // line open or not? private Queue<Customer> line; //the line of customers public Register(){ open = true; line = new LinkedList<Customer>(); } public double getTotalEnqueue(){ double totalTime = 0; for(Customer cust: line ){ totalTime += cust.getServiceTime(); } return totalTime; } public void addCustomer(Customer c){ line.add(c); } public Customer pollCustomer(){ return line.poll(); } public boolean isOpen(){ return open; } public void setOpen(boolean open){ this.open = open; } public int lineSize(){ return line.size(); } public String toString(){ String str = ""; for(Customer cust: line ) { str += "\n" + cust.toString(); } return str; } }
Customer:
import java.util.Random; public class Customer { private double serviceTime; Customer(){ Random r = new Random(); serviceTime = r.nextInt(4); } public double getServiceTime(){ return serviceTime; } public void setServiceTime(double serviceTime){ this.serviceTime = serviceTime; return; } public String toString(){ return "Service Time: " + serviceTime; } }
StoreView:
import java.awt.Color; import java.awt.GridLayout; import java.util.ArrayList; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class StoreView extends JFrame{ private JPanel topPanel; private JPanel checkoutPanel; private JButton createCustomer; private JTextField[] totalServiceTime = new JTextField[10]; private ImageIcon registerIcon; private JLabel[] registers = new JLabel[10]; private ArrayList<Customer> customers = new ArrayList<Customer>(); private StoreModel model; private Register register = new Register(); public StoreView(StoreModel model){ this.model = model; setTitle("Checkout Simulator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridLayout(11,10)); buildTopPanel(); add(topPanel); createRegisters(); getServiceTime(); pack(); setVisible(true); } public void createRegisters(){ registerIcon = new ImageIcon("C:\\Users\\Dev\\Desktop\\GUI Project\\GuiProject\\src\\register.png"); for(int i = 0; i < registers.length; i++){ registers[i] = new JLabel(); registers[i].setIcon(registerIcon); add(registers[i]); } } public void getServiceTime() { for(int i = 0; i < totalServiceTime.length; i++) { totalServiceTime[i].setText("" + register.getTotalEnqueue()); add(totalServiceTime[i]); } } public void buildTopPanel(){ topPanel = new JPanel(); topPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); createCustomer = new JButton("CreateCustomer"); topPanel.add(createCustomer); } public void setCustomer(){ } }
StoreModel:
public class StoreModel { private Customer cust = new Customer(); private Store store = new Store(); private Register reg = new Register(); public StoreModel(){ setCustomer(new Customer()); setStore(new Store()); setRegister(new Register()); } public StoreModel(Customer customer, Store store, Register register){ setCustomer(customer); setStore(store); setRegister(register); } public Customer getCustomer(){ return cust; } public void setCustomer(Customer customer){ cust = customer; } public Store getStore(){ return store; } public void setStore(Store store){ this.store = store; } public Register getRegister(){ return reg; } public void setRegister(Register reg){ this.reg = reg; } }
Store Controller:
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class StoreController { private StoreModel model; private StoreView view; public StoreController(StoreModel model, StoreView view){ this.model = model; this.view = view; } class AddCustomer implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { model.getCustomer().getServiceTime(); } } }