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

This exercise implements various methods to compare amounts of money in euros and cents.

My Code

Main

public class Main {

    public static void main(String[] args) {
        // Test your code here!
        Money a = new Money(10, 0);
        Money b = new Money(3, 50);

        Money c = a.minus(b);

        System.out.println(a);  // 10.00e
        System.out.println(b);  // 3.50e
        System.out.println(c);  // 6.50e

        c = c.minus(a);         // NOTE: new Money-object is created and reference to that is assigned to variable c
        //       the Money object 6.50e that variable c used to hold, is not referenced anymore

        System.out.println(a);  // 10.00e
        System.out.println(b);  // 3.50e
        System.out.println(c);  // 0.00e
    }
}

Money


public class Money {

    private final int euros;
    private final int cents;

    public Money(int euros, int cents) {

        if (cents > 99) {
            euros += cents / 100;
            cents %= 100;
        }

        this.euros = euros;
        this.cents = cents;
    }

    public int euros() {
        return euros;
    }

    public int cents() {
        return cents;
    }

    public Money plus(Money added) {
        Money result = new Money(this.euros + added.euros, this.cents + added.cents);
        return result;
    }

    public boolean less(Money compared) {
        int first = this.cents + (this.euros * 100);
        int second = compared.cents + (compared.euros * 100);
        return first < second;
    }

    public Money minus(Money decremented) {
        Money result = new Money(0, 0);
        int first = this.cents + (this.euros * 100);
        int second = decremented.cents + (decremented.euros * 100);
        if ((first - second) > 0 ){
            result = new Money(0, first - second);
        }
        return result;
    }

    @Override
    public String toString() {
        String zero = "";
        if (cents < 10) {
            zero = "0";
        }

        return euros + "." + zero + cents + "e";
    }

}

Model Code

Main

public class Main {

    public static void main(String[] args) {
        // Test your code here!
        Money a = new Money(10, 0);
        Money b = new Money(3, 50);

        Money c = a.minus(b);

        System.out.println(a);  // 10.00e
        System.out.println(b);  // 3.50e
        System.out.println(c);  // 6.50e

        c = c.minus(a);         // NOTE: new Money-object is created and reference to that is assigned to variable c
        //       the Money object 6.50e that variable c used to hold, is not referenced anymore

        System.out.println(a);  // 10.00e
        System.out.println(b);  // 3.50e
        System.out.println(c);  // 0.00e
    }
}

Money

public class Money {

 

    private final int euros;

    private final int cents;

 

    public Money(int euros, int cents) {

 

        if (cents > 99) {

            euros += cents / 100;

            cents %= 100;

        }

 

        this.euros = euros;

        this.cents = cents;

    }

 

    public int euros() {

        return euros;

    }

 

    public int cents() {

        return cents;

    }

 

    @Override

    public String toString() {

        String zero = "";

        if (cents < 10) {

            zero = "0";

        }

 

        return euros + "." + zero + cents + "e";

    }

 

    public Money plus(Money added) {

        int euroSum = euros + added.euros();

        int centSum = cents + added.cents();

 

        if (centSum > 99) {

            centSum -= 100;

            euroSum++;

        }

 

        return new Money(euroSum, centSum);

    }

 

    public boolean less(Money compared) {

        return (100 * euros + cents) < (100 * compared.euros() + compared.cents());

    }

 

    public Money minus(Money decremented) {

        int euroDifference = euros - decremented.euros();

        int centDifference = cents - decremented.cents();

 

        if (centDifference < 0) {

            centDifference += 100;

            euroDifference--;

        }

 

        if (euroDifference < 0) {

            return new Money(0, 0);

        }

 

        return new Money(euroDifference, centDifference);

    }

}

Comments

Although it looks like there is a lot of difference at first glance. In fact there wasn’t so much once I started going through it. I probably should have created a new method to calculate the euros and cents back into cents for comparison though as I copied and pasted that.

Leave a comment