116 lines
2.6 KiB
Java
116 lines
2.6 KiB
Java
package usherbrooke.ift630;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.concurrent.locks.Lock;
|
|
import java.util.concurrent.locks.ReentrantLock;
|
|
|
|
public class Stop implements Runnable {
|
|
private String name;
|
|
private int id;
|
|
private int maxCapacity;
|
|
private ArrayList<Passenger> passengers;
|
|
private Lock mutex = new ReentrantLock();
|
|
|
|
// TODO lock mutex when stop is used
|
|
@Override
|
|
public void run() {
|
|
// xd do nothing for now
|
|
//for (Passenger p : passengers) {
|
|
// //
|
|
//}
|
|
}
|
|
|
|
// run once. Then, queue the same thread, and exit.
|
|
// Message a bus their current number of passenger ; and the
|
|
// number of passenger willing to take this bus.
|
|
Stop(int id, int maxCapacity) {
|
|
this.id = id;
|
|
this.name = "Stop " + id;
|
|
this.passengers = new ArrayList<Passenger>();
|
|
this.maxCapacity = maxCapacity;
|
|
}
|
|
|
|
public Passenger getPassenger() {
|
|
return passengers.get(0);
|
|
}
|
|
|
|
public void addPassenger(Passenger p) {
|
|
passengers.add(p);
|
|
}
|
|
|
|
// return all passenger that stops at a stop in the list
|
|
public ArrayList<Passenger> getPassengerByDest(ArrayList<Stop> list) {
|
|
// res for a little cleaner code
|
|
ArrayList<Passenger> res = new ArrayList<Passenger>();
|
|
for (Stop s : list) {
|
|
for (Passenger p : passengers) {
|
|
if (p.getDest() == s) {
|
|
// if we got one, return & abort loop
|
|
res.add(p);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
public String getName() {
|
|
return name;
|
|
}
|
|
|
|
public int getCurrentCapacity() {
|
|
return passengers.size();
|
|
}
|
|
|
|
public int getMaxCapacity() {
|
|
return maxCapacity;
|
|
}
|
|
|
|
public Lock getMutex() {
|
|
return mutex;
|
|
}
|
|
|
|
public void printDetails(int indent, int color) {
|
|
Logger.getInstance().print(color, "\t".repeat(indent) + "---".repeat(3) + " Stop details " + "---".repeat(3));
|
|
Logger.getInstance().print(color, "\t".repeat(indent) + name);
|
|
|
|
for (Passenger p : passengers) {
|
|
p.printDetails(indent + 1);
|
|
}
|
|
}
|
|
|
|
public void printDetails(int indent) {
|
|
Logger.getInstance().print(id, "\t".repeat(indent) + "---".repeat(3) + " Stop details " + "---".repeat(3));
|
|
Logger.getInstance().print(id, "\t".repeat(indent) + name);
|
|
|
|
for (Passenger p : passengers) {
|
|
p.printDetails(indent + 1);
|
|
}
|
|
}
|
|
|
|
public void printDetails() {
|
|
printDetails(0);
|
|
}
|
|
|
|
public void busArrive() {
|
|
synchronized(this) {
|
|
mutex.lock();
|
|
}
|
|
}
|
|
|
|
public void busLeave() {
|
|
synchronized(this) {
|
|
mutex.unlock();
|
|
}
|
|
}
|
|
|
|
public void removePassenger(Passenger p) {
|
|
// Logger.getInstance().print(id, "Passenger " + p.getName() + " left " + name);
|
|
try {
|
|
passengers.remove(p);
|
|
} catch(Exception e) {
|
|
System.out.println("exception" + e.getMessage());
|
|
}
|
|
}
|
|
}
|