
/**
 * Models a simple (weighted) coin.
 * 
 * @author Dave Reed
 * @version 9/10/15
 */
public class Coin {
    private int headProbability;
    private int numHeads;
    private int numTails;
    private Die myDie;

    /**
     * Constructor for objects of class Coin.
     *   @param prob Probability that a given flip will come out heads
     */
    public Coin(int prob) {
        this.headProbability = prob;
        this.resetStats();
        this.myDie = new Die(100);
    }

    public void resetStats() {
        this.numHeads = 0;
        this.numTails = 0;        
    }
    
    /**
     * Flips the coin.
     *   @return  either "HEADS" or "TAILS"
     */
    public String flip() {
        if (this.myDie.roll() <= this.headProbability) {
            this.numHeads++;
            return "HEADS";
        }
        else {
            this.numTails++;
            return "TAILS";
        }
    }

    public int getNumber(String coinFace) {
        if (coinFace == "HEADS") {
            return this.numHeads;
        }
        else if (coinFace == "TAILS") {
            return this.numTails;
        }
        else {
            return 0;
        }
    }
    
    public double getPercentage(String coinFace) {
        if (this.numHeads + this.numTails > 0) {
            int faceCount = 0;
            if (coinFace == "HEADS") {
                faceCount = this.numHeads;            
            }
            else if (coinFace == "TAILS") {
                faceCount = this.numTails;
            }
            double perc = 100.0 * faceCount / (this.numHeads + this.numTails);
            return Math.round(10*perc)/10.0; 
        }
        else {
            return 0.0;
        }
    }
                     
    public double doLotsOfFlips(int numFlips) {
        this.resetStats();
        for (int i = 0; i < numFlips; i++) {
            this.flip();
        }
        return this.getPercentage("HEADS");
    }
}
