// © National University of Singapore. All rights reserved.

// Department of Computer Science
// National University of Singapore
// 3, Science Drive 2, Singapore 117543
// Tel: (65) 874 2830 Fax: (65) 779 4580


import spades_Java.*;

// PetrolStation.java
// models a petrol station with 4 consecutive service points

public class PetrolStation extends Executive {
	Vehicle vehicle;
        Resource washPoint, dryPoint, topUpPoint, paymentPoint;
        Resource[] service_points = new Resource[4];
	
	public void init()
	{
    		washPoint = new Resource("Wash Point", 1);
		service_points[0] = washPoint;
    		dryPoint = new Resource("Dry Point", 1);
		service_points[1] = dryPoint;
    		topUpPoint = new Resource("Top Up Point", 1);
		service_points[2] = topUpPoint;
    		paymentPoint = new Resource("Payment Counter", 1);
		service_points[3] = paymentPoint;
				
		vehicle = new Vehicle(this);
		mapProcess(vehicle, service_points[0]);
		activate(vehicle, 0.0);
	}

	public static void main(String[] args)
	{
		PetrolStation p = new PetrolStation();
		p.initialize(args.length, args);
		p.startSimulation();
	}
}

// Vehicle.java
// models a moving vehicle arriving at the petrol station
class Vehicle extends SProcess {
	PetrolStation station;
	public static int numVehicle = 0;
	private int loc;
	
	public Vehicle(PetrolStation pt)
	{
		super();
		station = pt;
		loc = 0;
	}
	
	public void execute()
	{
		switch(getPhase())
		{
			case 1	:
			{
				if (loc == 0)
				{
					Vehicle next = new Vehicle(station);
					activate(next, exponential(3.0));
				}
				work(station.service_points[loc++], exponential(3.0), 1);
				if (loc > 3)
					phase = 2;
				break;
			}
			case 2	:
			{	terminate();}
		}
	}
	
	public void run()
	{
		while (! SimulationStopped)
		{
			if (loc == 0)
			{
				Vehicle next = new Vehicle(station);
				activate(next, exponential(3.0));
			}
			work(station.service_points[loc++], exponential(3.0), 1);
			if (loc > 3)
				terminate();
		}
	}
}	
	
