Skip to content

Commit

Permalink
HW1
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolDude53 committed Sep 6, 2016
0 parents commit e989123
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/out/
.idea/
OOD.iml
53 changes: 53 additions & 0 deletions src/HW1/BetterDiningHallSimulation.java
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;
}
}
54 changes: 54 additions & 0 deletions src/HW1/CashRegister.java
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);
}
}
}
46 changes: 46 additions & 0 deletions src/HW1/Customer.java
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;
}
}

0 comments on commit e989123

Please sign in to comment.