feat: add authors
This commit is contained in:
@@ -1,4 +1,8 @@
|
|||||||
|
|
||||||
|
/**
|
||||||
|
* @author Yadunand Prem
|
||||||
|
* @version CS2030S AY22/23 Semester 2
|
||||||
|
*/
|
||||||
class ArrivalEvent extends BaseShopEvent {
|
class ArrivalEvent extends BaseShopEvent {
|
||||||
|
|
||||||
double serviceTime;
|
double serviceTime;
|
||||||
|
|||||||
@@ -4,8 +4,8 @@
|
|||||||
* simulation. Your task is to replace this
|
* simulation. Your task is to replace this
|
||||||
* class with new classes, following proper OOP principles.
|
* class with new classes, following proper OOP principles.
|
||||||
*
|
*
|
||||||
* @author Wei Tsang
|
* @author Yadunand Prem
|
||||||
* @version CS2030S AY21/22 Semester 2
|
* @version CS2030S AY22/23 Semester 2
|
||||||
*/
|
*/
|
||||||
|
|
||||||
abstract class BaseShopEvent extends Event {
|
abstract class BaseShopEvent extends Event {
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
|
/**
|
||||||
|
* @author Yadunand Prem
|
||||||
|
* @version CS2030S AY22/23 Semester 2
|
||||||
|
*/
|
||||||
public class Customer {
|
public class Customer {
|
||||||
final private int id;
|
final private int id;
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
|
/**
|
||||||
|
* @author Yadunand Prem
|
||||||
|
* @version CS2030S AY22/23 Semester 2
|
||||||
|
*/
|
||||||
class DepartureEvent extends BaseShopEvent {
|
class DepartureEvent extends BaseShopEvent {
|
||||||
|
|
||||||
public DepartureEvent(double time, Customer customer, Shop shop) {
|
public DepartureEvent(double time, Customer customer, Shop shop) {
|
||||||
|
|||||||
24
Event.java
24
Event.java
@@ -1,16 +1,16 @@
|
|||||||
/**
|
/**
|
||||||
* The Event class is an abstract class that encapsulates a
|
* The Event class is an abstract class that encapsulates a
|
||||||
* discrete event to be simulated. An event encapsulates the
|
* discrete event to be simulated. An event encapsulates the
|
||||||
* time the event occurs. A subclass of event _must_ override
|
* time the event occurs. A subclass of event _must_ override
|
||||||
* the simulate() method to implement the logic of the
|
* the simulate() method to implement the logic of the
|
||||||
* simulation when this event is simulated. The simulate method
|
* simulation when this event is simulated. The simulate method
|
||||||
* returns an array of events, which the simulator will then
|
* returns an array of events, which the simulator will then
|
||||||
* add to the event queue. Note that an event also implements
|
* add to the event queue. Note that an event also implements
|
||||||
* the Comparable interface so that a PriorityQueue can
|
* the Comparable interface so that a PriorityQueue can
|
||||||
* arrange the events in the order of event time.
|
* arrange the events in the order of event time.
|
||||||
*
|
*
|
||||||
* @author Wei Tsang
|
* @author Yadunand Prem
|
||||||
* @version CS2030S AY21/22 Semester 2
|
* @version CS2030S AY22/23 Semester 2
|
||||||
*/
|
*/
|
||||||
abstract class Event implements Comparable<Event> {
|
abstract class Event implements Comparable<Event> {
|
||||||
/** The time this event occurs in the simulation. */
|
/** The time this event occurs in the simulation. */
|
||||||
@@ -38,8 +38,8 @@ abstract class Event implements Comparable<Event> {
|
|||||||
* Compare this event with a given event e.
|
* Compare this event with a given event e.
|
||||||
*
|
*
|
||||||
* @param e The other event to compare to.
|
* @param e The other event to compare to.
|
||||||
* @return 1 if this event occurs later than e;
|
* @return 1 if this event occurs later than e;
|
||||||
* 0 if they occur the same time;
|
* 0 if they occur the same time;
|
||||||
* -1 if this event occurs earlier.
|
* -1 if this event occurs earlier.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ import java.util.Scanner;
|
|||||||
/**
|
/**
|
||||||
* The main class for CS2030S Lab 1.
|
* The main class for CS2030S Lab 1.
|
||||||
*
|
*
|
||||||
* @author Wei Tsang
|
* @author Yadunand Prem
|
||||||
* @version CS2030S AY21/22 Semester 2
|
* @version CS2030S AY22/23 Semester 2
|
||||||
*/
|
*/
|
||||||
class Lab1 {
|
class Lab1 {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@@ -12,8 +12,8 @@ class Lab1 {
|
|||||||
// Create a scanner to read from standard input.
|
// Create a scanner to read from standard input.
|
||||||
Scanner sc = new Scanner(System.in);
|
Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
// Create a simulation. The ShopSimulation
|
// Create a simulation. The ShopSimulation
|
||||||
// constructor will read the simulation parameters
|
// constructor will read the simulation parameters
|
||||||
// and initial events using the scanner.
|
// and initial events using the scanner.
|
||||||
Simulation simulation = new ShopSimulation(sc);
|
Simulation simulation = new ShopSimulation(sc);
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
|
/**
|
||||||
|
* @author Yadunand Prem
|
||||||
|
* @version CS2030S AY22/23 Semester 2
|
||||||
|
*/
|
||||||
class ServiceBeginEvent extends BaseShopEvent {
|
class ServiceBeginEvent extends BaseShopEvent {
|
||||||
|
|
||||||
double serviceTime;
|
double serviceTime;
|
||||||
@@ -18,9 +21,9 @@ class ServiceBeginEvent extends BaseShopEvent {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Event[] simulate() {
|
public Event[] simulate() {
|
||||||
this.counter.setAvailable(false);
|
this.counter.occupy();
|
||||||
double endTime = this.getTime() + this.serviceTime;
|
double endTime = this.getTime() + this.serviceTime;
|
||||||
return new Event[] {
|
return new Event[] {
|
||||||
new ServiceEndEvent(endTime, this.customer, this.shop, this.counter) };
|
new ServiceEndEvent(endTime, this.customer, this.shop, this.counter) };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
|
/**
|
||||||
|
* @author Yadunand Prem
|
||||||
|
* @version CS2030S AY22/23 Semester 2
|
||||||
|
*/
|
||||||
class ServiceEndEvent extends BaseShopEvent {
|
class ServiceEndEvent extends BaseShopEvent {
|
||||||
|
|
||||||
ShopCounter counter;
|
ShopCounter counter;
|
||||||
@@ -16,7 +19,7 @@ class ServiceEndEvent extends BaseShopEvent {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Event[] simulate() {
|
public Event[] simulate() {
|
||||||
this.counter.setAvailable(true);
|
this.counter.free();
|
||||||
return new Event[] { new DepartureEvent(this.getTime(), customer, shop) };
|
return new Event[] { new DepartureEvent(this.getTime(), customer, shop) };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,7 @@
|
|||||||
|
/**
|
||||||
|
* @author Yadunand Prem
|
||||||
|
* @version CS2030S AY22/23 Semester 2
|
||||||
|
*/
|
||||||
public class Shop {
|
public class Shop {
|
||||||
private ShopCounter[] counters;
|
private ShopCounter[] counters;
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
|
/**
|
||||||
|
* @author Yadunand Prem
|
||||||
|
* @version CS2030S AY22/23 Semester 2
|
||||||
|
*/
|
||||||
public class ShopCounter {
|
public class ShopCounter {
|
||||||
final private int id;
|
final private int id;
|
||||||
private boolean available;
|
private boolean available;
|
||||||
@@ -6,8 +10,12 @@ public class ShopCounter {
|
|||||||
return available;
|
return available;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAvailable(boolean available) {
|
public void occupy() {
|
||||||
this.available = available;
|
this.available = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void free() {
|
||||||
|
this.available = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ShopCounter(int id) {
|
public ShopCounter(int id) {
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ import java.util.Scanner;
|
|||||||
/**
|
/**
|
||||||
* This class implements a shop simulation.
|
* This class implements a shop simulation.
|
||||||
*
|
*
|
||||||
* @author Wei Tsang
|
* @author Yadunand Prem
|
||||||
* @version CS2030S AY21/22 Semester 2
|
* @version CS2030S AY22/23 Semester 2
|
||||||
*/
|
*/
|
||||||
class ShopSimulation extends Simulation {
|
class ShopSimulation extends Simulation {
|
||||||
|
|
||||||
|
|||||||
@@ -1,19 +1,19 @@
|
|||||||
/**
|
/**
|
||||||
* This class is a general abstract class that
|
* This class is a general abstract class that
|
||||||
* encapsulates a simulation. To implement a
|
* encapsulates a simulation. To implement a
|
||||||
* simulation, inherit from this class and implement
|
* simulation, inherit from this class and implement
|
||||||
* the `getInitialEvents` method.
|
* the `getInitialEvents` method.
|
||||||
*
|
*
|
||||||
* @author Wei Tsang
|
* @author Yadunand Prem
|
||||||
* @version CS2030S AY21/22 Semester 2
|
* @version CS2030S AY22/23 Semester 2
|
||||||
*/
|
*/
|
||||||
abstract class Simulation {
|
abstract class Simulation {
|
||||||
/**
|
/**
|
||||||
* An abstract method to return an array of events
|
* An abstract method to return an array of events
|
||||||
* used to initialize the simulation.
|
* used to initialize the simulation.
|
||||||
*
|
*
|
||||||
* @return An array of initial events that the
|
* @return An array of initial events that the
|
||||||
* simulator can use to kick-start the
|
* simulator can use to kick-start the
|
||||||
* simulation.
|
* simulation.
|
||||||
*/
|
*/
|
||||||
public abstract Event[] getInitialEvents();
|
public abstract Event[] getInitialEvents();
|
||||||
|
|||||||
@@ -3,19 +3,19 @@ import java.util.PriorityQueue;
|
|||||||
/**
|
/**
|
||||||
* This class implements a discrete event simulator.
|
* This class implements a discrete event simulator.
|
||||||
* The simulator maintains a priority queue of events.
|
* The simulator maintains a priority queue of events.
|
||||||
* It runs through the events and simulates each one until
|
* It runs through the events and simulates each one until
|
||||||
* the queue is empty.
|
* the queue is empty.
|
||||||
*
|
*
|
||||||
* @author Wei Tsang
|
* @author Yadunand Prem
|
||||||
* @version CS2030S AY21/22 Semester 2
|
* @version CS2030S AY22/23 Semester 2
|
||||||
*/
|
*/
|
||||||
public class Simulator {
|
public class Simulator {
|
||||||
/** The event queue. */
|
/** The event queue. */
|
||||||
private final PriorityQueue<Event> events;
|
private final PriorityQueue<Event> events;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The constructor for a simulator. It takes in
|
* The constructor for a simulator. It takes in
|
||||||
* a simulation as an argument, and calls the
|
* a simulation as an argument, and calls the
|
||||||
* getInitialEvents method of that simulation to
|
* getInitialEvents method of that simulation to
|
||||||
* initialize the event queue.
|
* initialize the event queue.
|
||||||
*
|
*
|
||||||
@@ -30,10 +30,10 @@ public class Simulator {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Run the simulation until no more events is in
|
* Run the simulation until no more events is in
|
||||||
* the queue. For each event in the queue (in
|
* the queue. For each event in the queue (in
|
||||||
* increasing order of time), print out its string
|
* increasing order of time), print out its string
|
||||||
* representation, then simulate it. If the
|
* representation, then simulate it. If the
|
||||||
* simulation returns one or more events, add them
|
* simulation returns one or more events, add them
|
||||||
* to the queue, and repeat.
|
* to the queue, and repeat.
|
||||||
*/
|
*/
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|||||||
Reference in New Issue
Block a user