feat: update everything

This commit is contained in:
Yadunand Prem
2022-08-26 11:31:46 +08:00
parent fda7834239
commit 2bd2b18bbe
6 changed files with 36 additions and 48 deletions

View File

@@ -3,32 +3,24 @@ class ArrivalEvent extends BaseShopEvent {
double serviceTime; double serviceTime;
public ArrivalEvent(double time, int customerId, boolean[] available, double serviceTime) { public ArrivalEvent(double time, Customer customer, Shop shop, double serviceTime) {
super(time, customerId, available); super(time, customer, shop);
this.availableCounters = available;
this.serviceTime = serviceTime; this.serviceTime = serviceTime;
} }
@Override @Override
public Event[] simulate() { public Event[] simulate() {
int counter = -1; ShopCounter availableCounter = this.shop.getAvailableCounter();
for (int i = 0; i < this.availableCounters.length; i++) { if (availableCounter == null) {
if (this.availableCounters[i]) { return new Event[] { new DepartureEvent(this.getTime(), customer, shop) };
counter = i;
break;
}
} }
if (counter == -1) {
return new Event[] { new DepartureEvent(this.getTime(), customerId, availableCounters) };
}
return new Event[] { return new Event[] {
new ServiceBeginEvent(this.getTime(), this.customerId, this.availableCounters, this.serviceTime, new ServiceBeginEvent(this.getTime(), customer, shop, this.serviceTime, availableCounter) };
counter) };
} }
@Override @Override
public String toString() { public String toString() {
return super.toString() + String.format(": Customer %d arrives", this.customerId); return super.toString() + String.format(": %s arrives", this.customer);
} }
} }

View File

@@ -10,13 +10,13 @@
abstract class BaseShopEvent extends Event { abstract class BaseShopEvent extends Event {
final int customerId; final Customer customer;
boolean[] availableCounters; Shop shop;
public BaseShopEvent(double time, int customerId, boolean[] availableCounters) { public BaseShopEvent(double time, Customer customer, Shop shop) {
super(time); super(time);
this.customerId = customerId; this.customer = customer;
this.availableCounters = availableCounters; this.shop = shop;
} }
} }

View File

@@ -1,14 +1,14 @@
class DepartureEvent extends BaseShopEvent { class DepartureEvent extends BaseShopEvent {
public DepartureEvent(double time, int customerId, boolean[] available) { public DepartureEvent(double time, Customer customer, Shop shop) {
super(time, customerId, available); super(time, customer, shop);
} }
@Override @Override
public String toString() { public String toString() {
return super.toString() return super.toString()
+ String.format(": Customer %d departed", this.customerId); + String.format(": %s departed", this.customer);
} }
@Override @Override

View File

@@ -2,26 +2,25 @@
class ServiceBeginEvent extends BaseShopEvent { class ServiceBeginEvent extends BaseShopEvent {
double serviceTime; double serviceTime;
int counterId; ShopCounter counter;
public ServiceBeginEvent(double time, int customerId, boolean[] available, double serviceTime, int counterId) { public ServiceBeginEvent(double time, Customer customer, Shop shop, double serviceTime, ShopCounter counter) {
super(time, customerId, available); super(time, customer, shop);
this.serviceTime = serviceTime; this.serviceTime = serviceTime;
this.counterId = counterId; this.counter = counter;
// TODO Auto-generated constructor stub
} }
@Override @Override
public String toString() { public String toString() {
return super.toString() return super.toString()
+ String.format(": Customer %d service begin (by Counter %d)", this.customerId, this.counterId); + String.format(": %s service begin (by %s)", this.customer, this.counter);
} }
@Override @Override
public Event[] simulate() { public Event[] simulate() {
this.availableCounters[this.counterId] = false; this.counter.setAvailable(false);
double endTime = this.getTime() + this.serviceTime; double endTime = this.getTime() + this.serviceTime;
return new Event[] { return new Event[] {
new ServiceEndEvent(endTime, this.customerId, this.availableCounters, this.counterId) }; new ServiceEndEvent(endTime, this.customer, this.shop, this.counter) };
} }
} }

View File

@@ -1,23 +1,22 @@
class ServiceEndEvent extends BaseShopEvent { class ServiceEndEvent extends BaseShopEvent {
int counterId; ShopCounter counter;
public ServiceEndEvent(double time, int customerId, boolean[] available, int counterId) { public ServiceEndEvent(double time, Customer customer, Shop shop, ShopCounter counter) {
super(time, customerId, available); super(time, customer, shop);
this.counterId = counterId; this.counter = counter;
// TODO Auto-generated constructor stub
} }
@Override @Override
public String toString() { public String toString() {
return super.toString() return super.toString()
+ String.format(": Customer %d service done (by Counter %d)", this.customerId, this.counterId); + String.format(": %s service done (by %s)", this.customer, this.counter);
} }
@Override @Override
public Event[] simulate() { public Event[] simulate() {
this.availableCounters[counterId] = true; this.counter.setAvailable(true);
return new Event[] { new DepartureEvent(this.getTime(), customerId, availableCounters) }; return new Event[] { new DepartureEvent(this.getTime(), customer, shop) };
} }
} }

View File

@@ -7,16 +7,12 @@ import java.util.Scanner;
* @version CS2030S AY21/22 Semester 2 * @version CS2030S AY21/22 Semester 2
*/ */
class ShopSimulation extends Simulation { class ShopSimulation extends Simulation {
/**
* The availability of counters in the shop.
*/
public boolean[] availableCounters;
/** /**
* The list of customer arrival events to populate * The list of customer arrival events to populate
* the simulation with. * the simulation with.
*/ */
public Event[] initEvents; private Event[] initEvents;
/** /**
* Constructor for a shop simulation. * Constructor for a shop simulation.
@@ -31,16 +27,18 @@ class ShopSimulation extends Simulation {
initEvents = new Event[sc.nextInt()]; initEvents = new Event[sc.nextInt()];
int numOfCounters = sc.nextInt(); int numOfCounters = sc.nextInt();
availableCounters = new boolean[numOfCounters]; ShopCounter[] availableCounters = new ShopCounter[numOfCounters];
for (int i = 0; i < numOfCounters; i++) { for (int i = 0; i < numOfCounters; i++) {
availableCounters[i] = true; availableCounters[i] = new ShopCounter(i);
} }
Shop shop = new Shop(availableCounters);
int id = 0; int id = 0;
while (sc.hasNextDouble()) { while (sc.hasNextDouble()) {
double arrivalTime = sc.nextDouble(); double arrivalTime = sc.nextDouble();
double serviceTime = sc.nextDouble(); double serviceTime = sc.nextDouble();
initEvents[id] = new ArrivalEvent(arrivalTime, id, availableCounters, serviceTime); initEvents[id] = new ArrivalEvent(arrivalTime, new Customer(id), shop, serviceTime);
id += 1; id += 1;
} }
} }