Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Checkout line Simulation

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Checkout line Simulation

    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();
     
    		}
     
    	}
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Checkout line Simulation

    We don't get paid, we give our time voluntarily. Making us wade through a butt-load of code and having to figure out what you want/need with only a vague description from you is not going to get you much help. The more specific your question is the more specific our answer will be.
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Feb 2010
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Checkout line Simulation

    I never asked for a handout of code. I obviously have done the work here. I told you what was asked. Everything I typed above, has been implemented other than the GUI part. I said I don't know how to add a customer to the line using the Create Customer button.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Checkout line Simulation

    How many lines of code have you posted? For anyone to be able to help you they would have to spend a considerable amount of time getting familiar with your code. Time that could be better spent on something more intersting to us. I'm not trying to be a bastard. I'm just offering some tips on how you can improve your chances of getting help.
    Improving the world one idiot at a time!

  5. #5
    Member
    Join Date
    Feb 2010
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Checkout line Simulation

    What should I do then in order to get help because I really am at a point where I'm not sure what my next step? I realize I have a lot of code here, but I can't help that.

Similar Threads

  1. Replies: 10
    Last Post: September 16th, 2011, 07:49 PM
  2. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  3. Reading a file line by line using the Scanner class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  4. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM