/***********************************************
 Main Class:       DieHardIII.java
 Author:           Mark Rich  richm@cs.wisc.edu
 Completion Date:  2/11/2000
 Updated:          2/8/2001

 Course:           CS 302

 Platform:         Unix
************************************************/

/**
 * This class is for use in the Die Hard III example for pouring
 * water into jugs and defusing a bomb.  See DieHardIII.java for 
 * a clearer description of the game.
 *
 * Bugs: None known
 **/
class WaterJug {
    
    private int capacity;     // maximum value for contents
    private int contents;     // current amount of water in the jug

    /**
     * Constructs a WaterJug based on the capacity cap
     * @param cap the capacity of our jug
     **/
    public WaterJug(int cap) {
	capacity = cap;
	contents = 0;
    }

    /**
     * Constructs a WaterJug based on the capacity cap and 
     * the contents cont.
     * @param cap the capacity of our jug
     * @param cont the initial contents of our jug
     **/
    public WaterJug(int cap, int cont) {
	capacity = cap;
	contents = 0;
	fill(cont);
    }

    /**
     * Takes in water, adds that amount to the contents, and returns any
     * overflow of water which cannot be held in the jug
     * @param amount the amount of water we are trying to add
     * @return the leftover amount of water, or overflow
     **/
    public int fill(int amount) {
	contents += amount;
	int overflow = contents - capacity;
	int absoverflow = Math.abs(overflow);
	overflow += absoverflow;
	overflow /= 2;
	contents -= overflow;
	return overflow;
    }

    /**
     * Takes the contents out of the jug and returns them 
     * @return the current previous contents of the jug
     **/
    public int empty() {
	int temp = contents;
	contents = 0;
	return temp;
    }

    /**
     * Returns the capacity of the jug
     * @return capacity of jug
     **/
    public int getCapacity() {
	return capacity;
    }

    /**
     * Returns the current contents of the jug without removing them
     * @return current contents of jug
     **/
    public int getContents() {
	return contents;
    }

    /**
     * Prints out the current capacity and contents of the jug
     **/
    public void print() {
	System.out.println("Capacity = " + capacity);
	System.out.println("Contents = " + contents + "\n");
    }
}










