package cours;

public class Cours {

	public Cours(int numeroCours, String intitulé, int nbPlacesMax) {
		this.numeroCours = numeroCours;
		this.intitulé = intitulé;
		this.nbPlacesMax = nbPlacesMax;
		this.nbInscrits = 0;
	}

	private final int nbPlacesMax;
	private final int numeroCours;
	private final String intitulé;
	private int nbInscrits;

	public void inscription(int nbPlaces) throws PasAsseezDePlacesException {
		synchronized (this) {
			if (this.nombrePlacesRestant() < nbPlaces)
				throw new PasAsseezDePlacesException(this, nbPlaces);
			this.nbInscrits += nbPlaces;
			System.out.println(
					nbPlaces + " inscriptions réussies au cours " + this);
		}
	}

	private int nombrePlacesRestant() {
		synchronized (this) {
			return (this.nbPlacesMax - this.nbInscrits);
		}
	}

	@Override
	public String toString() {
		return String.valueOf(this.numeroCours) + " : " + intitulé + " "
				+ this.nombrePlacesRestant() + " places restants";
	}
}
