intermediate/ShoppingSimulator

From ggc

Jump to: navigation, search

001 package intermediate;
002 //start auto-imports
003 import java.util.*;
004 //end auto-imports
005 
006 import wiki.Wiki;
007 
008 /**
009  * All about my application.
010  @author My Name Here
011  */
012 public class ShoppingSimulator
013 {
014   private ArrayList<Cashier> cashier;
015 
016   /**Customers currently in the store*/
017   private ArrayList<Customer> customer;
018 
019   /**Customers who have exited the store*/
020   private ArrayList<Customer> exitCustomers;
021 
022   private double currentTime;
023 
024   /**shoppers/second*/
025   private double rateShoppersEnter;
026 
027   private int minItems;
028   private int maxItems;
029   private double shoppingTimePerItem;
030   private double checkoutTimePerItem;
031 
032   private Random random;
033 
034   private double nextEntryTime;
035 
036   public ShoppingSimulator()
037   {
038     cashier=new ArrayList<Cashier>();
039 
040     customer=new ArrayList<Customer>();
041 
042     exitCustomers=new ArrayList<Customer>();
043 
044     currentTime=0;
045 
046     rateShoppersEnter=0.5;
047 
048     minItems=1;
049     maxItems=100;
050 
051     shoppingTimePerItem=2;
052 
053     random=new Random(0);
054   }
055 
056   public void addCashier(Cashier ... c)
057   {
058     for(int i=0; i<c.length; i++)
059     {
060       cashier.add(c[i]);
061     }
062   }
063 
064   public int getItemCount()
065   {
066     return random.nextInt(maxItems-minItems)+minItems;
067   }
068 
069   public void generateNextEntry()
070   {
071     double cumulative=random.nextDouble();
072     double time=-Math.log(cumulative)/rateShoppersEnter;
073     nextEntryTime=currentTime+time;
074   }
075 
076   public void simulateNextEvent()
077   {
078     /**the next customer that would enter a line*/
079     Customer firstToEnterLane=null;
080     for(Customer cust: customer)
081     {
082       if(firstToEnterLane==null || firstToEnterLane.getEnterLaneTime()>cust.getEnterLaneTime())
083       {
084         firstToEnterLane=cust;
085       }
086     }
087     /**the next customer that would exit the store*/
088     Customer firstToExitLane=null;
089     for(Customer cust: customer)
090     {
091       if(firstToExitLane==null || firstToExitLane.getExitLaneTime()>cust.getExitLaneTime())
092       {
093         firstToExitLane=cust;
094       }
095     }
096     //act on next event
097     if(firstToEnterLane.getEnterLaneTime()<firstToExitLane.getExitLaneTime() &&
098             firstToEnterLane.getEnterLaneTime()<nextEntryTime)
099     {
100       System.out.println("Entering a line.");
101       enterLane(firstToEnterLane);
102     }
103     else if(firstToExitLane.getExitLaneTime()<nextEntryTime)
104     {
105       System.out.println("Exiting store.");
106       exitLane(firstToExitLane);
107     }
108     else
109     {
110       System.out.println("Enter store.");
111       enterStore();
112     }
113   }
114 
115   public void enterStore()
116   {
117     currentTime=nextEntryTime;
118     Customer c=new Customer();
119     c.setEnterStoreTime(currentTime);
120     int items=random.nextInt(maxItems-minItems)+minItems;
121     c.setItems(items);
122     c.setShoppingRate(shoppingTimePerItem);
123     customer.add(c);
124     generateNextEntry();
125   }
126 
127   public void exitLane(Customer cust)
128   {
129     for(Cashier c: cashier)
130     {
131       if(c.hasCustomer(cust))
132       {
133         currentTime=c.getNextAvailableTime();
134         c.scanNextCustomer(currentTime);
135         System.out.println("Customer "+cust+" exits from cashier "+c);
136       }
137     }
138   }
139 
140   public void enterLane(Customer cust)
141   {
142     int items=cust.getItems();
143     Cashier fastest=null;
144     for(Cashier c: cashier)
145     {
146       if(fastest==null && c.canJoin(cust))
147       {
148         fastest=c;
149       }
150       else if(fastest.getNumberInQueue()>c.getNumberInQueue())
151       {
152         fastest=c;
153       }
154     }
155     fastest.joinLane(cust);
156     currentTime=cust.getEnterLaneTime();
157     if(fastest.getNumberInQueue()==1)
158     {
159       fastest.scanNextCustomer(currentTime);
160     }
161     Wiki.out.println("customer "+cust+" joins cashier lane "+fastest+" at time "+currentTime);
162   }
163 
164   public String toString()
165   {
166     String customers="";
167     for(Customer c: customer)
168     {
169       customers+="\t\t"+c+" with "+c.getItems()+" items\n";
170     }
171     String cashiers="";
172     for(Cashier c: cashier)
173     {
174       cashiers+="\t\t"+c+"\n";
175     }
176     String text="";
177     text+="Current Time: "+currentTime+"\n";
178     text+="\tCustomers: \n";
179     text+=customers+"\n";
180     text+="\tCashiers: \n";
181     text+=cashiers+"\n";
182     return text;
183   }
184 
185   public static void main(String[] args)
186   {
187     System.out.println("Simulation is:");
188     ShoppingSimulator simulator=new ShoppingSimulator();
189     Cashier one=new Cashier();
190     Cashier two=new Cashier();
191     simulator.addCashier(one, two);
192     System.out.println(simulator.toString());
193     simulator.generateNextEntry();
194     simulator.enterStore();
195     System.out.println(simulator.toString());
196     simulator.enterStore();
197     System.out.println(simulator.toString());
198     simulator.enterStore();
199     System.out.println(simulator.toString());
200     for(int i=0; i<10; i++)
201     {
202       simulator.simulateNextEvent();
203       System.out.println(simulator.toString());
204     }
205   }
206 }


Download/View intermediate/ShoppingSimulator.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter