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;
public ArrivalEvent(double time, int customerId, boolean[] available, double serviceTime) {
super(time, customerId, available);
this.availableCounters = available;
public ArrivalEvent(double time, Customer customer, Shop shop, double serviceTime) {
super(time, customer, shop);
this.serviceTime = serviceTime;
}
@Override
public Event[] simulate() {
int counter = -1;
for (int i = 0; i < this.availableCounters.length; i++) {
if (this.availableCounters[i]) {
counter = i;
break;
}
ShopCounter availableCounter = this.shop.getAvailableCounter();
if (availableCounter == null) {
return new Event[] { new DepartureEvent(this.getTime(), customer, shop) };
}
if (counter == -1) {
return new Event[] { new DepartureEvent(this.getTime(), customerId, availableCounters) };
}
return new Event[] {
new ServiceBeginEvent(this.getTime(), this.customerId, this.availableCounters, this.serviceTime,
counter) };
new ServiceBeginEvent(this.getTime(), customer, shop, this.serviceTime, availableCounter) };
}
@Override
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 {
final int customerId;
boolean[] availableCounters;
final Customer customer;
Shop shop;
public BaseShopEvent(double time, int customerId, boolean[] availableCounters) {
public BaseShopEvent(double time, Customer customer, Shop shop) {
super(time);
this.customerId = customerId;
this.availableCounters = availableCounters;
this.customer = customer;
this.shop = shop;
}
}

View File

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

View File

@ -2,26 +2,25 @@
class ServiceBeginEvent extends BaseShopEvent {
double serviceTime;
int counterId;
ShopCounter counter;
public ServiceBeginEvent(double time, int customerId, boolean[] available, double serviceTime, int counterId) {
super(time, customerId, available);
public ServiceBeginEvent(double time, Customer customer, Shop shop, double serviceTime, ShopCounter counter) {
super(time, customer, shop);
this.serviceTime = serviceTime;
this.counterId = counterId;
// TODO Auto-generated constructor stub
this.counter = counter;
}
@Override
public String 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
public Event[] simulate() {
this.availableCounters[this.counterId] = false;
this.counter.setAvailable(false);
double endTime = this.getTime() + this.serviceTime;
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 {
int counterId;
ShopCounter counter;
public ServiceEndEvent(double time, int customerId, boolean[] available, int counterId) {
super(time, customerId, available);
this.counterId = counterId;
// TODO Auto-generated constructor stub
public ServiceEndEvent(double time, Customer customer, Shop shop, ShopCounter counter) {
super(time, customer, shop);
this.counter = counter;
}
@Override
public String 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
public Event[] simulate() {
this.availableCounters[counterId] = true;
return new Event[] { new DepartureEvent(this.getTime(), customerId, availableCounters) };
this.counter.setAvailable(true);
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
*/
class ShopSimulation extends Simulation {
/**
* The availability of counters in the shop.
*/
public boolean[] availableCounters;
/**
* The list of customer arrival events to populate
* the simulation with.
*/
public Event[] initEvents;
private Event[] initEvents;
/**
* Constructor for a shop simulation.
@ -31,16 +27,18 @@ class ShopSimulation extends Simulation {
initEvents = new Event[sc.nextInt()];
int numOfCounters = sc.nextInt();
availableCounters = new boolean[numOfCounters];
ShopCounter[] availableCounters = new ShopCounter[numOfCounters];
for (int i = 0; i < numOfCounters; i++) {
availableCounters[i] = true;
availableCounters[i] = new ShopCounter(i);
}
Shop shop = new Shop(availableCounters);
int id = 0;
while (sc.hasNextDouble()) {
double arrivalTime = 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;
}
}