Object-Oriented Programming with Java, part I – Exercise 84

This exercise implements an overloaded counter class to increase and decrease a starting value.

My Code

Main

public class Main {
    public static void main(String[] args) {
        // write testcode here
        Counter count = new Counter(20,true);
        System.out.println(count.value());
        count.increase();
        System.out.println(count.value());
        count.increase(2);
        System.out.println(count.value());
        count.decrease();
        System.out.println(count.value());
        count.decrease(20);
        System.out.println(count.value());
        
    }
}

Counter

public class Counter {

    int startingValue;
    boolean check;

    public Counter(int startingValue, boolean check) {
        this.startingValue = startingValue;
        this.check = check;

    }

    public Counter(int startingValue) {
        this(startingValue, false);

    }

    public Counter(boolean check) {
        this(0, check);

    }

    public Counter() {
        this(0, false);

    }

    public int value() {
        return this.startingValue;
    }

    public void increase() {
        increase(1);
    }

    public void increase(int increaseAmount) {
        if (!(this.startingValue < 0) && (increaseAmount > 0)) {
            this.startingValue += increaseAmount;
        }

    }

    public void decrease() {
        if ((this.startingValue > 0) && this.check) {
            this.startingValue -= 1;
        } else if (!this.check) {
            this.startingValue -= 1;
        }

    }

    public void decrease(int decreaseAmount) {
        int i = 0;
        while (decreaseAmount > i) {
            if (this.check) {
                if (this.startingValue >= 0) {
                    decrease();
                }

            }
            if (!this.check) {
                decrease();
            }
            i++;
        }

    }

}

Model Code

Main

public class Main {
    public static void main(String[] args) {
        // write testcode here
        Counter count = new Counter(20,true);
        System.out.println(count.value());
        count.increase();
        System.out.println(count.value());
        count.increase(2);
        System.out.println(count.value());
        count.decrease();
        System.out.println(count.value());
        count.decrease(20);
        System.out.println(count.value());
        
    }
}

Counter

public class Counter {

 

    private int value;

    private boolean check;

 

    public Counter(int value, boolean check) {

        this.value = value;

        this.check = check;

    }

 

    public Counter(int value) {

        this(value, false);

    }

 

    public Counter(boolean tarkista) {

        this(0, tarkista);

    }

 

    public Counter() {

        this(0, false);

    }

 

    public int value() {

        return this.value;

    }

 

    public void increase() {

        increase(1);

    }

 

    public void decrease() {

        decrease(1);

    }

 

    public void increase(int by) {

        if (by >= 0) {

            this.value += by;

        }

    }

 

    public void decrease(int by) {

        if (by < 0) {

            return;

        }

 

        this.value -= by;

        

        if (this.check && this.value <0) {

            this.value = 0;

        }                

 

    }

}

Comments

As you can see by looking at my code and the Model Code I am a long way out here. The model code is a lot clearer and simpler although I quite like my reusing of the decrease() method in the decreaseAmount() method.

Leave a comment