In: Computer Science
Print three outputs corresponding to three tests that are based on different proposed bar sizes, such as a small bar (25 seats), a medium sized bar (60 seats) and a big bar (100 seats).
CSMSoftwareGurusBar.java
public class CSMSoftwareGurusBar { private int freeChairs = 50; private double profit = 0.0; private SimulationFramework simulation = new SimulationFramework(); private int[] beerType = {100, 60, 25}; public static void main(String[] args) { CSMSoftwareGurusBar world = new CSMSoftwareGurusBar(); System.out.println(world.beerType.length); CSMSoftwareGurusBar world2 = new CSMSoftwareGurusBar(); System.out.println(world2); CSMSoftwareGurusBar world3 = new CSMSoftwareGurusBar(); } CSMSoftwareGurusBar() { int t = 0; while (t < 240) { // simulate 4 hours of bar operation t += randBetween(2, 5); // new group every 2-5 minutes if (t <= 240) simulation.scheduleEvent(new ArriveEvent(t, randBetween(1, 5))); } // group size ranges from 1 to 5 simulation.run(); System.out.println("Total profits " + profit); } private int randBetween(int low, int high) { return low + (int) ((high - low + 1) * Math.random()); } public boolean canSeat(int numberOfPeople) { System.out.println("Group of " + numberOfPeople + " customers arrives at time " + simulation.time()); if (numberOfPeople < freeChairs) { System.out.println("Group is seated"); freeChairs -= numberOfPeople; return true; } else System.out.println("No Room, Group Leaves"); return false; } private void order(int beerType) { System.out.println("Serviced order for beer type " + beerType + " at time " + simulation.time()); // update profit knowing beerType (left for you) profit += beerType + 1; } private void leave(int numberOfPeople) { System.out.println("Group of size " + numberOfPeople + " leaves at time " + simulation.time()); freeChairs += numberOfPeople; } private class ArriveEvent extends Event { private int groupSize; ArriveEvent(int time, int gs){ super(time); groupSize = gs; } public void processEvent(){ if (canSeat(groupSize)){ // place an order within 2 & 10 minutes simulation.scheduleEvent (new OrderEvent(time + randBetween(2,10), groupSize)); } } } private class OrderEvent extends Event { private int groupSize; OrderEvent(int time, int gs) { super(time); groupSize = gs; } public void processEvent() { // each member of the group orders a beer (type 1,2,3) for (int i = 0; i < groupSize; i++) { order(1 + simulation.weightedProbability(beerType)); order(2 + simulation.weightedProbability(beerType)); order(3 + simulation.weightedProbability(beerType)); // schedule a leaveEvent for this group // all the group leaves together (left for you) simulation.scheduleEvent(new LeaveEvent(time + randBetween(30, 60), groupSize)); } } } private class LeaveEvent extends Event { LeaveEvent(int time, int gs) { super(time); groupSize = gs; } private int groupSize; public void processEvent() { leave(groupSize); } } }
SimulationFrameWork.java
public class SimulationFramework { public void scheduleEvent(Event newEvent) { // put or addElement “newEvent” to the “eventQueue” // MinHeap Priority Queue (left for you) eventQueue.addElement(newEvent); } public void run () { while (! eventQueue.isEmpty()) { Event nextEvent = (Event) eventQueue.removeMin(); currentTime = nextEvent.time; nextEvent.processEvent(); } } public int time() { return currentTime; } private int currentTime = 0; private FindMin eventQueue = new Heap(new DefaultComparator()); public int weightedProbability(int[] beerType){ int sumOfArray = 0; for (int i=0; i < beerType.length; i++){ sumOfArray += beerType[i]; } int probability = 1 + (int) ((sumOfArray) * Math.random()); if (probability <= 15){ return 0; } else if (probability <= 75){ return 1; } else{ return 2; } } private class FindMin { public Object removeMin() { return currentTime ; } public boolean isEmpty() { return true; } public void addElement(Event newEvent) { System.out.println(newEvent); } } private class Heap extends FindMin { public Heap(DefaultComparator defaultComparator) { super(); } } }
Event.java
public abstract class Event implements Comparable { public final int time; public Event (int t) { time = t; } abstract void processEvent (); public int compareTo (Object o) { Event right = (Event) o; if (time < right.time) { return -1; } if (time == right.time){ return 0; } return 1; } }
DefaultComparator.java
public class DefaultComparator implements java.util.Comparator { public int compare(java.lang.Object left, java.lang.Object right) { Event i = (Event) left; Event j = (Event) right; if (i.time > j.time){ return 1; } else if (i.time < j.time){ return -1; } else{ return 0; } } public boolean equals(java.lang.Object obj){ return this.equals(obj); } }
The point where you might be getting it wrong may be you're saving the file with name with SimulationFrameWork.java, rather you should save it with the name SimulationFramework.java. and then the program will run smoothly. In a folder save these files as shown:
Just pass different inputs in line 5th of CSMSoftwareGurusBar.java
1.
private int[] beerType = {100, 60, 25};
OUTPUT:
2.
private int[] beerType = {10,8,5};
OUTPUT:
3.
private int[] beerType = {100, 60, 25};
OUTPUT: