
package reservationManager;
/**
 *  Some kind of resource, that protects himself from simulteneos
 * reading and writting
 */
public class Resource {
	private int ammount;
	
	public Resource(int ammount){
		this.ammount = ammount;
	}

	/**
	 * returns the amount of resource left
	 * @return the amount of resource left
	 */
	synchronized public int getAmount(){
		return ammount;
	}
	
	/**
	 * Simulates a using of the resource, by decreasing
	 * the amount of resource left by the asked number
	 * @param howMuch how much resource are being used
	 * @throws Exception if someone tries to use more of the resource 
	 * than there is available
	 */
	synchronized public void useResource(int howMuch) throws Exception{
		if (ammount < howMuch) throw new Exception();
		ammount-=howMuch;
	}
	
	/**
	 * Simulates a retturn of the resource (after using it), by increasing
	 * the amount of resource left by the asked number
	 * @param howMuch how much resource are being returned
	 */
	synchronized public void returnResource(int howMuch){
		ammount+=howMuch;
	}
}
