package thisandthat;

import stdlib.StdOut;
import stdlib.StdRandom;

/**
 * Counter
 */
public class Counter implements Comparable<Counter> {

	/**
	 * counter name
	 */
    private final String name;
    
    /**
     * maximum value
     */
    private final int maxCount;
    
    /**
     * current value
     */
    private int count;

    /**
     * create a new counter with the given parameters
     * @param id
     * @param max
     */
    public Counter(String id, int max) {
        name = id;
        maxCount = max;
        count = 0;
    } 

    /**
     * increment the counter by 1
     */
    public void increment() {
        if(count < maxCount) count++;
        assert count >= 1;
    } 

    /**
     * @return the current count
     */
    public int value() {
        return count;
    } 

    /**
     * return a string representation of this counter
     */
    public String toString() {
        return name + ": " + count;
    } 

    /**
     * compare two Counter objects based on their count
     */
    public int compareTo(Counter that) {
        if     (this.count < that.count) return -1;
        else if(this.count > that.count) return +1;
        else                             return  0;
    }


    // test client
    public static void main(String[] args) {
    	int i;
    	args = new String[]{"6","600000"};
        int N = Integer.parseInt(args[0]);
        int T = Integer.parseInt(args[1]);

        // create N counters
        Counter[] hits = new Counter[N];
        for(i=0; i<N; i++)
            hits[i] = new Counter(i + "", T);

        // increment T counters at random
        for(i=0; i<T; i++)
            hits[StdRandom.uniform(N)].increment();

        // print results
        for(i=0; i<N; i++)
            StdOut.println(hits[i]);
   } 
} 
