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:


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;



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){

	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;


import java.util.Random;

public class Customer {

	private double serviceTime;

		Random r = new Random();
		serviceTime = r.nextInt(4);
	public double getServiceTime(){
		return serviceTime;
	public void setServiceTime(double serviceTime){
		this.serviceTime = serviceTime;
	public String toString(){
		return "Service Time: " + serviceTime;



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");
        setLayout(new GridLayout(11,10));
    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();
    public void getServiceTime()
    	for(int i = 0; i < totalServiceTime.length; i++)
    		totalServiceTime[i].setText("" + register.getTotalEnqueue());
    public void buildTopPanel(){
        topPanel = new JPanel();
        topPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
        createCustomer = new JButton("CreateCustomer");
    public void setCustomer(){


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){
	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{

		public void actionPerformed(ActionEvent e) {
5 Years
Discussion Span
Last Post by JamesCherrill
Featured Replies
  • Do you know how to execute a method when a button is pressed? If not, start with the tutorials. Your button handler just needs to inform, the controller that the button has been pressed so a new customer must be created and added to a queue. You already have Model … Read More


I think you need to be a lot more specific about the help you need. What have you done so far? What exactly are you stuck on?


I am stuck on how to add a customer to the Queue (line) of a register when the createCustomer button is clicked. I'm using a grid layout, so I'm not sure how this will work.


Do you know how to execute a method when a button is pressed? If not, start with the tutorials.
Your button handler just needs to inform, the controller that the button has been pressed so a new customer must be created and added to a queue. You already have Model methods to do those things, so the controller just has to call them.
Beginners often make the mistake of doing the GUI first then don't know how to perform the actual functions. You are in a much better place because you have a Model with all the functionality easily available via public method calls. All you have to do is call the appropriate methods when a button is clicked. It really couldn't be much easier! I think if you just get stuck in and give it a go you'll find that there really isn't a problem.

ps: the grid layout is a bit of a distraction here - the whole listener/controller stuff is the same regardless of the GUI layout.

Edited by JamesCherrill: pps

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.