import java.util.Vector; public class Bateau extends Thread { private final String Bateau_NAME; private final int CAPACITY; private int currentPort; private boolean goingUp = true; private Zonemarine thisZonemarine; private long serviceTime; private long travelTime; private int numPassengers; private Vector[] passengers; private int numberPorts; private boolean running = true; long startTime = System.currentTimeMillis(); public Bateau(String name, int numberOfPorts, int startingPort, int capacity, Zonemarine office, long serviceTimeMS, long travelTimeMS) { Bateau_NAME = name; numberPorts = numberOfPorts; currentPort = startingPort; thisZonemarine = office; serviceTime = serviceTimeMS; travelTime = travelTimeMS; numPassengers = 0; CAPACITY = capacity; } public void stopBateau() { running = false; } public synchronized int getCurrentPort() { return currentPort; } public void run() { System.out.println(toString() + " pret"); while (running) { System.out.println(toString()+" est maintenant au port "+currentPort +" a: "+ (System.currentTimeMillis()- startTime)); if (currentPort == numberPorts-1){ goingUp = false; } else if (currentPort == 0) { goingUp = true; } notifyPassengers(); thisZonemarine.tellAt(); try{ sleep(serviceTime); } catch (InterruptedException exc) { System.out.println(toString() + " sommeil interrompu "); } System.out.println(toString() + " maintenant quitte le port "+currentPort +" a: "+ (System.currentTimeMillis()- startTime)); if (goingUp) { currentPort++; } else { currentPort--; } try{ sleep(travelTime); } catch (InterruptedException exc) { System.out.println(toString() + " sommeil interrompu "); } } } public synchronized int takeBateau(int destPort, int currPort, Vehicule waiter) { if (currentPort == currPort && numPassengers < CAPACITY) { numPassengers++; System.out.println(waiter + " Embarque a bord de la " + toString() + " a la port " + currPort+" a: "+ (System.currentTimeMillis()- startTime)); while (currentPort != destPort){ try{ wait(); } catch (InterruptedException ie){ System.out.println(toString()+" interrompu: "+ie.toString()); } } numPassengers--; return destPort; } else { return currPort; } } private synchronized void notifyPassengers(){ notifyAll(); } public String toString(){ return Bateau_NAME; } public boolean isGoingUp(){ return goingUp; } }