-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e989123
Showing
4 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/out/ | ||
.idea/ | ||
OOD.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package HW1; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class BetterDiningHallSimulation | ||
{ | ||
private static final int SIMULATION_TIME = 50000; // A simulation is for 50,000 imaginary seconds. | ||
private static final int CUST_ARRIVAL_PCT = 18; // There is a 18% chance a customer arrives each second. | ||
private static final int NUM_REGISTERS = 4; // There are 4 cash registers. | ||
|
||
private static ArrayList<CashRegister> registers = new ArrayList<>(); | ||
|
||
// statistics about the cash registers | ||
private static int[] customersServed = new int[NUM_REGISTERS]; // del HW1.CashRegister | ||
private static int[] totalWaitTimes = new int[NUM_REGISTERS]; // del HW1.Customer | ||
|
||
public static void main(String[] args) | ||
{ | ||
// First, initialize the cash register arrays. | ||
for (int r = 0; r < NUM_REGISTERS; r++) | ||
registers.add(new CashRegister()); | ||
|
||
// Then perform the simulation for the specified number of seconds. | ||
for (int t = 0; t < SIMULATION_TIME; t++) | ||
{ | ||
if (aCustomerArrives()) | ||
{ | ||
Customer newCustomer = new Customer(); | ||
newCustomer.smallestRegister(registers).addCustomerInLine(newCustomer); | ||
} | ||
|
||
for (CashRegister register : registers) | ||
register.elapseOneSecond(); | ||
} | ||
|
||
// Print out the statistics. | ||
for (int r = 0; r < NUM_REGISTERS; r++) | ||
{ | ||
CashRegister register = registers.get(r); | ||
|
||
System.out.println("Register " + r); | ||
System.out.println("\tNumber of arrivals = " + register.getCustomersServed()); | ||
System.out.println("\tAverage wait time = " + register.getAverageWaitTime()); | ||
} | ||
} | ||
|
||
// keep this here because it is driving the simulation | ||
private static boolean aCustomerArrives() | ||
{ | ||
int n = (int) (Math.random() * 100); // an integer between 0 and 99 | ||
return n < CUST_ARRIVAL_PCT; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package HW1; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class CashRegister | ||
{ | ||
private ArrayList<Customer> customersInLine = new ArrayList<>(); | ||
private int customersServed = 0; | ||
private int totalWaitTime = 0; | ||
|
||
public void addCustomerInLine(Customer customer) | ||
{ | ||
customersInLine.add(customer); | ||
totalWaitTime += customer.getWaitTime(); | ||
} | ||
|
||
public void customerServed(Customer customer) | ||
{ | ||
customersInLine.remove(customer); | ||
customersServed++; | ||
} | ||
|
||
public int getCustomersServed() | ||
{ | ||
return customersServed; | ||
} | ||
|
||
public int getLine() | ||
{ | ||
return customersInLine.size(); | ||
} | ||
|
||
public int getAverageWaitTime() | ||
{ | ||
return totalWaitTime / customersServed; | ||
} | ||
|
||
public void elapseOneSecond() | ||
{ | ||
// If the list is empty, there are no customers to process. | ||
if (getLine() == 0) | ||
return; | ||
|
||
// Otherwise, the first customer in line gets processed. | ||
if (customersInLine.get(0).waited() <= 0) | ||
{ | ||
// First update the register's statistics. | ||
customersServed++; | ||
|
||
// Then remove the customer. | ||
customersInLine.remove(0); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package HW1; | ||
|
||
import java.util.List; | ||
|
||
public class Customer | ||
{ | ||
// is the wait time the customer is going to have | ||
private int waitTime; | ||
|
||
public Customer() | ||
{ | ||
this.waitTime = 2 * howManyItems() + 10; | ||
} | ||
|
||
// determines how many items this customer is going to get | ||
private int howManyItems() | ||
{ | ||
int n = (int) (Math.random() * 10); | ||
return n + 1; | ||
} | ||
|
||
// determines the shortest line for the customer to go to | ||
public CashRegister smallestRegister(List<CashRegister> registers) | ||
{ | ||
CashRegister smallestLine = null; | ||
|
||
for (CashRegister register : registers) | ||
{ | ||
if (smallestLine == null || register.getLine() < smallestLine.getLine()) | ||
smallestLine = register; | ||
} | ||
|
||
return smallestLine; | ||
} | ||
|
||
public int getWaitTime() | ||
{ | ||
return waitTime; | ||
} | ||
|
||
public int waited() | ||
{ | ||
waitTime--; | ||
return waitTime; | ||
} | ||
} |