Results 1 to 9 of 9
  1. #1
    jav4newbie is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Exclamation Help with GUI interface Multiple checkout

    Hi. I am new to Java and this forum so please go easy on me. I am working on this assignment where i have create a simple single checkout simulation based on random numbers. This also returns statistical data such as mean, max size, min size, variance and standard deviation from the queue length. This was all done from the console simulation and I am now aiming to implement a multiple checkout on a GUI. Basically i want a text field where a user inputs the number of tills they want and it prints all this out in a textarea so say if they input 10 then it would input all the simulation for those 10 tills but each having their different values of queue sizes etc. I have done some research and found that i would need a Store class for this to store the multiple checkouts but i don't want to use an arraylsit as i am storing objects NOT primitives. However i am confused as how i would go about this. How would i return the chosen size of checkouts entered from the text field to the text area? This is what i have so far:

    Checkout Class:

    Java Code:
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Random;
    import java.util.Collections;
    
    
    
    
    public class Checkout{
     
     private Queue<String> tillQueue;
     private int rndNumber; 
     private int currentLen; 
     private ArrayList <Integer>lengthList;
     private Random r;
     private double mean;
     private double largeLength;
     private double smallLength;
     
     
     
     
     public Checkout()
     {
         
        tillQueue = new LinkedList<String>(); 
        currentLen = 0;      
        lengthList = new ArrayList <Integer>();
        mean = 0; 
        r = new Random();
        
     }
     
     public void simulation()
     {
      
      System.out.println("Time"+ "\t" +"Rnd"+ "\t" +"Queue Status");
      
         for (int t=2; t<10; t++) //number of time-steps in the simulation.
         {
             rndNumber = r.nextInt(6)+1; // generates random number between 1 and 6.       
             if (rndNumber==2 || rndNumber==4 || rndNumber==6) 
             {            
                  // if rndNumber is 4, t is added to a queue.
                     tillQueue.add(String.valueOf(t));
                     currentLen++;
             }
             else if ((rndNumber==1 || rndNumber==3) && tillQueue.peek()!=null)
             {                         
                 tillQueue.remove();
                 currentLen--;
              }
             
             else if (rndNumber==5)
             {
              
             }
             
             if(tillQueue.isEmpty())
             {    
                 System.out.println(t + "\t" + rndNumber + "\t" + "Queue is empty");
             }
             else
             {         
                 System.out.println(t + "\t" + rndNumber+"\t" + tillQueue.toString() ); 
             }
             
             lengthList.add(new Integer(currentLen));  //using  wrapper         
         }
     }
     
      //CALCULATE MEAN LENGTH
      public double getMeanLength()
      {
       double mean=0.0;
       int sumOfLengths =0;
       
       for (int i =0; i<lengthList.size();i++)
       {
        sumOfLengths=sumOfLengths+(Integer)lengthList.get(i).intValue(); //cast
       }
       mean = (double)sumOfLengths/lengthList.size();
       return mean;
      }
      
      //CALCULATE MAX LENGTH
      public double CalculateMaxLength()
      {
         largeLength = Collections.max(lengthList);
         return largeLength; 
      } 
      
       //CALCULATE MIN LENGTH
      public double CalculateMinLength()
      {
         smallLength = Collections.min(lengthList);
         return smallLength; 
      } 
      
      //CALCULATE VARIANCE
      public double getVariance()
      {
      
         double variance;
         variance = getMeanLength()-1;
         return variance;
    
      }
      
      
       //CALCULATE STANDARD DEVIATION
      public double CalculateStandardDev()
      {
         double stddev;
         stddev = Math.sqrt(getVariance());
         return stddev;
    
      } 
      
     
     public boolean saveStringToFile(String fileName, String saveString)
     {
      boolean saved = false;
      BufferedWriter bw = null;
      try {
       bw = new BufferedWriter(new FileWriter(fileName)); 
       try{
        bw.write(saveString);
        saved = true;
       }
       
       finally{
        bw.close();
       }
      } catch (IOException ex) {
       
       ex.printStackTrace();
       
      }
      return saved;
     }
     
    }
    Controller Class:

    Java Code:
    public class Controller {
    	
    	
    	
    	public static Checkout till1, till2;
    	
    	public static void main(String[] args) {
    		
    		//Till 1
    		
    		new GUI();
    		System.out.println("Till 1");
    		till1 = new Checkout();
    		till1.simulation();
    		System.out.println("");
    		double meanlength=till1.getMeanLength();
            System.out.println("mean length = "+ meanlength); 
            
            double largelength=till1.CalculateMaxLength();
            System.out.println("max length = " + largelength );
            
            double smalllength=till1.CalculateMinLength();
            System.out.println("min length = " + smalllength );
            
            double variance=till1.getVariance();
            System.out.println("variance = " + variance );
            
            double stddev=till1.CalculateStandardDev();
            System.out.println("standard deviation = " + stddev );
            
            till1.saveStringToFile("test.txt", "Mean queue length =" +Double.toString(meanlength));
            
    		System.out.println("");
    		
    		//Till 2
    		System.out.println("Till 2");
    		till2 = new Checkout();
    		till2.simulation();
    		System.out.println("");
    		double meanlength2=till2.getMeanLength();
            System.out.println("mean length = "+ meanlength2); 
            
            double largelength2=till2.CalculateMaxLength();
            System.out.println("max length = " + largelength2 );
            
            double smalllength2=till2.CalculateMinLength();
            System.out.println("min length = " + smalllength2 );
            
           double variancetwo=till2.getVariance();
           System.out.println("variance = " + variancetwo);
           
           double stddev2=till1.CalculateStandardDev();
           System.out.println("standard deviation = " + stddev2 );
            
            till2.saveStringToFile("test.txt", "Mean queue length =" +Double.toString(meanlength2));
     
           
    
    	}
    
    }
    Store class:

    Java Code:
    public class Store
    
    {
    	private Checkout [] till;
    	private static int count;
    	private int sizeIn;
    	
    	public Store (int sizeIn)
    	{
    		till = new Checkout[sizeIn];
    	}
    	
    	public void add(Checkout c)
    	{
    		if (count < sizeIn){
    			till[count] = c;
    			count++;
    		}
    	}
    	
    	public void displayAll()
    	{
    		for(int i=0; i < count; ++i){
    			System.out.println(till[i]);
    		}
    	}
    	
    	public int getCount()
    	{
    		return count;
    	}
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Help with GUI interface Multiple checkout

    i don't want to use an arraylsit as i am storing objects NOT primitives.
    Did you say that backwards? ArrayLists only store objects, NOT primitives.

    How would i return the chosen size of checkouts entered from the text field to the text area?
    get the value out of the text field and append it to the text area. ??? I don't see any text fields or text areas in your code. I don't see any GUI classes in your code.


    Why don't you start with making a skeleton GUI for your project and leave out all this code you have posted.
    Later when the GUI is working you can copy and paste into the GUI code the methods you need from this code.
    Last edited by Norm; 11-30-2011 at 01:46 AM.

  3. #3
    jav4newbie is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Help with GUI interface Multiple checkout

    Did you say that backwards? ArrayLists only store objects, NOT primitives.
    Whoopsi yes i got that the other way round


    get the value out of the text field and append it to the text area. ??? I don't see any text fields or text areas in your code.
    Sorry my mistake i forgot to add that class as i was still working on it. This is what i have so far but it comes with a whole load of errors:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    
    public class GUI extends JFrame
    {
    	private Store till;
    	JButton runSimulation;
    	JButton viewStatistics;
    	JTextField tillAmount;
    	JTextArea viewMultipleSimulation;
    	
    	public GUI()
    	{
    		
    		setLayout(new FlowLayout());
    		runSimulation = new JButton("Run Simulation");
    		add(runSimulation);
    		viewStatistics = new JButton("View Statistical Data");
    		add(viewStatistics);
    		tillAmount = new JTextField(4);
    		add(tillAmount);
    		viewMultipleSimulation = new JTextArea(8,45);
    		add(viewMultipleSimulation);
    		
    		
    		
    		
    
    		myEventHandler h = new myEventHandler();
    		
    		runSimulation.addActionListener(h);
    		viewStatistics.addActionListener(h);
    		tillAmount.addActionListener(h);	
    	}
    	
    	private class myEventHandler implements ActionListener
    	{
    		
    		public void actionPerformed(ActionEvent e)
    		
    		
    		{
    			if(e.getSource() == runSimulation)
    			{
    				String tillEntered = tillAmount.getText();
    				
    				if(tillEntered.length() == 0)
    				{
    					viewMultipleSimulation.setText("A till amount must be entered");
    				}
    				else 
    				{
    					Checkout c = new Checkout(Integer.parseInt(tillEntered));
    					till.add(c);
    					tillAmount.setText("");
    					viewMultipleSimulation.setText(tillEntered + " of tills have been added.");					
    				}
    			}
    			
    			if(e.getSource() == runSimulation)
    			{
    				viewMultipleSimulation.append(tillAmount.getText());
    			}
    		}
    	}
    }
    And now my main problem is i am unsure how i add the simulation method and methods from my Store class to set the number of tills and view the simulation

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Help with GUI interface Multiple checkout

    it comes with a whole load of errors:
    Please copy and paste the text of the error messages here.

    how i add the simulation method and methods from my Store class to set the number of tills and view the simulation
    What are the sequence of events that you want to happen with the GUI version? You show the GUI, the user enters some data and presses a button and then what?

  5. #5
    jav4newbie is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Help with GUI interface Multiple checkout

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at GUI$myEventHandler.actionPerformed(GUI.java:56)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2351)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6373)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:613 8)
    at java.awt.Container.processEvent(Container.java:208 5)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4735)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2143)
    at java.awt.Component.dispatchEvent(Component.java:45 65)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4621)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4282)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4212)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2129)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:45 65)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:679)
    at java.awt.EventQueue.access$000(EventQueue.java:85)
    at java.awt.EventQueue$1.run(EventQueue.java:638)
    at java.awt.EventQueue$1.run(EventQueue.java:636)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:652)
    at java.awt.EventQueue$2.run(EventQueue.java:650)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 649)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    Here is the error i'm having, it's completely different to the other errors i had:


    What are the sequence of events that you want to happen with the GUI version? You show the GUI, the user enters some data and presses a button and then what?
    So i want either of these arrays....

    Java Code:
    public class Store
    
    {
    	private Checkout [] till;
    	private static int count;
    	private int sizeIn;
    	
    	public Store (int sizeIn)
    	{
    		till = new Checkout[sizeIn];
    	}
    	
    	public void add(Checkout c)
    	{
    		if (count < sizeIn){
    			till[count] = c;
    			count++;
    		}
    	}
    	
    	public void displayAll()
    	{
    		for(int i=0; i < count; ++i){
    			System.out.println(till[i]);
    		}
    	}
    	
    	public int getCount()
    	{
    		return count;
    	}
    
    }
    ...to be passed into the text field so when a user inputs a number say 10, and clicks on the run simulation button, it displays 10 of the following simulation details in the textfield area:


    Time Rnd Queue Status
    2 6 [2]
    3 3 Queue is empty
    4 2 [4]
    5 5 [4]
    6 2 [4, 6]
    7 4 [4, 6, 7]
    8 2 [4, 6, 7, 8]
    9 6 [4, 6, 7, 8, 9]


    Also my simulation is in a separate class so i will have to call that in the (ActionPerformed e) statement but unsure as to how i would go about that either.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Help with GUI interface Multiple checkout

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at GUI$myEventHandler.actionPerformed(GUI.java:56)
    At line 56 in the actionPerformed method, the code uses a variable with a null value.
    Find the variable and then find out why the variable does not have a valid value.

    i want either of these arrays....
    I only see one array: till
    to be passed into the text field
    How much of the array's contents do you want displayed in the text field? I would imagine you only want one element from the array, not all of them.

    displays 10 of the following simulation details in the textfield area:
    Textfields contain a single line of data. Your display shows more than one line.
    You could use a text area to show more than one line.

  7. #7
    jav4newbie is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Help with GUI interface Multiple checkout

    At line 56 in the actionPerformed method, the code uses a variable with a null value.
    Find the variable and then find out why the variable does not have a valid value.

    i changed the (ActionEvent e) to:

    Java Code:
    private class myEventHandler implements ActionListener
    	{
    		
    		public void actionPerformed(ActionEvent e)
    		
    		
    		{
    			
    			if(e.getSource() == runSimulation)
    			{
    				String tillEntered = tillAmount.getText();
    				Store till = new Store(Integer.parseInt(tillEntered));
    				if(tillEntered.length() == 0)
    				{
    					viewMultipleSimulation.setText("A till amount must be entered");
    				}
    				else 
    				{
    					Checkout c = new Checkout(Integer.parseInt(tillEntered));
    					till.add(c);
    					tillAmount.setText("");
    					viewMultipleSimulation.setText(tillEntered + " number of tills have been added.");
    					
    				}
    			}
    			
    			if(e.getSource() == runSimulation)
    			{
    				viewMultipleSimulation.append(tillAmount.getText());
    			}
    		}
    	}
    it seemed to get rid of the error but i don't think i went about it the right way

    and i changed one of the lines of code above to the following as i need to call the simulation from the checkout class but it shows with a red line under simulation(); :

    Java Code:
    viewMultipleSimulation.setText(tillEntered + " number of tills have been added." + "\n" + (simulation());
    i wanna set the number of tills in the text field and for this to be passed into that array till.

    How much of the array's contents do you want displayed in the text field? I would imagine you only want one element from the array, not all of them.
    Well whatever number the user enters, say 5 this will be entered into the text field and if i'm not mistaken, i will have to use one of the following from the store class:

    Java Code:
    public Store (int sizeIn)
    	{
    		till = new Checkout[sizeIn];
    	}
    	
    	public void add(Checkout c)
    	{
    		if (count < sizeIn){
    			till[count] = c;
    			count++;
    		}
    	}
    to set that to the text field. and when they input 5 it will store 5 objects of tills in that array and display the following simulation 5 times in the textarea i meant to say:

    Time Rnd Queue Status
    2 6 [2]
    3 4 [2, 3]
    4 1 [3]
    5 3 Queue is empty
    6 5 Queue is empty
    7 6 [7]
    8 3 Queue is empty
    9 3 Queue is empty

    and i will need to call this method i believe from the Store class to show all 5 tills:

    Java Code:
    public void displayAll()
    	{
    		for(int i=0; i < count; ++i){
    			System.out.println(till[i]);
    		}
    	}
    I hope this makes sense?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Help with GUI interface Multiple checkout

    In your first posted code, lines 9 & 27 look like they make same test. Why?

    it shows with a red line under simulation();
    Ask the IDE why it shows a red line. I have no idea. The posted code is out of the viewing area for me.

    I can not make any sense from the bits and pieces of code you have coded. You use the variable: till two ways on line 3 and line 9 in your second bit of posted code. One way as an array, the other not. That code should not compile.

    In the third posted code, the method uses global variables:count and till to do its job.

  9. #9
    alexpayne5657 is offline Member
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Default Re: Help with GUI interface Multiple checkout

    Having looked at your code i want to point out that the variance calculation you have is wrong..thats not how you work out the variance you basically have the mean and then minus 1 which is incorrect its a bit more complex than that just thought id let you know. Have another go and i'l let you know if its right.

Similar Threads

  1. Replies: 1
    Last Post: 06-07-2011, 07:22 AM
  2. Multiple GUI Interface Instances
    By mutagen in forum AWT / Swing
    Replies: 2
    Last Post: 03-19-2011, 03:06 PM
  3. Replies: 6
    Last Post: 02-01-2010, 09:30 AM
  4. Interface to connect multiple databases
    By sruz in forum Advanced Java
    Replies: 1
    Last Post: 11-04-2009, 03:20 PM
  5. Multiple views on user interface application
    By dand_dd in forum SWT / JFace
    Replies: 36
    Last Post: 04-24-2009, 09:05 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •