Code Review

OOP: Classes and Objects

?

public class Time {
    private int hours;
    private int minutes;
    private int seconds;

    Time (int totalSeconds) {
        initHoursMinutesSeconds(totalSeconds);
    }

    Time (int hours, int minutes, int seconds) {
        this.hours = hours;
        this.minutes = minutes;
        this.seconds = seconds;
    }

?

private void initHoursMinutesSeconds(int totalSeconds) {
    this.hours = (int) TimeUnit.SECONDS.toHours(totalSeconds);
    this.minutes = (int) (TimeUnit.SECONDS.toMinutes(totalSeconds) - TimeUnit.HOURS.toMinutes(TimeUnit.SECONDS.toHours(totalSeconds)));
    this.seconds = (int) (TimeUnit.SECONDS.toSeconds(totalSeconds) - TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(totalSeconds)));
}

private int parseHoursMinutesSecondsToSeconds() {
    return (int) (seconds + TimeUnit.MINUTES.toSeconds(minutes) + TimeUnit.HOURS.toSeconds(hours));
}

?

public class Utils {
    public static int getSquareOfNumber(int number) {
        return (int) Math.pow(number, 2);
    }

}

?

public int getDistance(Point point) {
    return getRadicalOfNumber(Utils.getSquareOfNumber(this.x - point.x) + Utils.getSquareOfNumber(this.y - point.y));
}

private int getRadicalOfNumber(int number) {
    return (int) Math.sqrt(number);
}

?

public class Rectangle {
    private int xLength;
    private int yLength;
    Point topLeftPoint;
    Point bottomRightPoint;

    Rectangle(Point topLeftPoint, Point bottomRightPoint) {
        this.topLeftPoint = topLeftPoint;
        this.bottomRightPoint = bottomRightPoint;
        initLengthSide();
    }

?

private final int banknote20 = Integer.parseInt(Banknotes.TWENTY.getBanknote());
private final int banknote50 = Integer.parseInt(Banknotes.FIFTY.getBanknote());
private final int banknote100 = Integer.parseInt(Banknotes.ONE_HUNDRED.getBanknote());

public enum Banknotes {
    TWENTY ("20"),
    FIFTY ("50"),
    ONE_HUNDRED ("100");

    private final String banknote;

    Banknotes(String banknote) {
        this.banknote = banknote;
    }

    public String getBanknote() {
        return banknote;
    }
}

?

public class Atm {
    private final int numberBanknotes20;
    private final int numberBanknotes50;
    private final int numberBanknotes100;
    private int amount;
    private final int banknote20 = Integer.parseInt(Banknotes.TWENTY.getBanknote());
    private final int banknote50 = Integer.parseInt(Banknotes.FIFTY.getBanknote());
    private final int banknote100 = Integer.parseInt(Banknotes.ONE_HUNDRED.getBanknote());

?

public boolean isPossibleIssue(int amount) {
        this.amount = amount;
        if (amount > getAllAtmMoney()) {
            return false;
        }
        checkBanknotes(banknote100);
        checkBanknotes(banknote50);
        checkBanknotes(banknote20);
        return this.amount == 0;
    }

    private int getAllAtmMoney() {
        return numberBanknotes20 * banknote20 +
                numberBanknotes50 * banknote50 +
                numberBanknotes100 * banknote100;
    }

    private void checkBanknotes(int banknoteNominal) {
        if (amount >= banknoteNominal) {
            this.amount = amount % banknoteNominal;
        }
    }
public Time(int hours, int minutes, int seconds){
    this.hours = hours;
    this.minutes = minutes;
    this.seconds = seconds;
}

?

public class Time {
    private int hours = 0;
    private int minutes = 0;
    private int seconds = 0;

?

public int getHours() {
    return this.hours;
}

public void setHours(int hours) {
    this.hours = hours;
}

public int getMinutes() {
    return this.minutes;
}

public void setMinutes(int minutes) {
    this.minutes = minutes;
}

public int getSeconds() {
    return this.seconds;
}

public void setSeconds(int seconds) {
    this.seconds = seconds;
}

public int getTotalSeconds() {
    return (this.hours * 3600) + (this.minutes * 60) + this.seconds;
}

?

public class Atm {
    private int numberBanknotes20;
    private int numberBanknotes50;
    private int numberBanknotes100;

    public Atm(int numberBanknotes20, int numberBanknotes50, int numberBanknotes100){
        this.numberBanknotes20 = numberBanknotes20;
        this.numberBanknotes50 = numberBanknotes50;
        this.numberBanknotes100 = numberBanknotes100;
    }

    public int getNumberBanknotes20() {
        return this.numberBanknotes20;
    }

    public int getNumberBanknotes50() {
        return this.numberBanknotes50;
    }

    public int getNumberBanknotes100() {
        return this.numberBanknotes100;
    }

    public void addBanknotes20(int number) {
        this.numberBanknotes20 -= number;
    }

    public void addBanknotes50(int number) {
        this.numberBanknotes50 -= number;
    }

    public void addBanknotes100(int number) {
        this.numberBanknotes100 -= number;
    }
}

?

private int getLengthSideX() {
    int xTopLeftPoint = topLeftPoint.getX();
    int xBottomRightPoint = bottomRightPoint.getX();
    int lengthSideX;
    if (xTopLeftPoint < 0 && xBottomRightPoint < 0) {
        lengthSideX = Math.abs(xTopLeftPoint - xBottomRightPoint);
    } else if (xTopLeftPoint < 0) {
        lengthSideX = Math.abs(xTopLeftPoint) + xBottomRightPoint;
    } else {
        lengthSideX = xBottomRightPoint - xTopLeftPoint;
    }
    return lengthSideX;
}

private int getLengthSideY() {
    int yTopLeftPoint = topLeftPoint.getY();
    int yBottomRightPont = bottomRightPoint.getY();
    int lengthSideY;
    if (yTopLeftPoint < 0 && yBottomRightPont < 0) {
        lengthSideY = Math.abs(yBottomRightPont - yTopLeftPoint);
    } else if (yBottomRightPont < 0) {
        lengthSideY = yTopLeftPoint + Math.abs(yBottomRightPont);
    } else {
        lengthSideY = yTopLeftPoint - yBottomRightPont;
    }
    return lengthSideY;
}

?

public class Time {
    private final int SECONDS_IN_HOUR = 3600;
    private final int SECONDS_IN_MINUTE = 60;
    private int hours;
    private int minutes;
    private int seconds;

    public Time(int totalSeconds) {
        convertSecondToHoursMinutesSeconds(totalSeconds);
    }

    public void convertSecondToHoursMinutesSeconds(int totalSeconds) {
        this.hours = totalSeconds / SECONDS_IN_HOUR;
        int totalTimeWithoutHours = totalSeconds - hours * SECONDS_IN_HOUR;
        this.minutes = totalTimeWithoutHours / SECONDS_IN_MINUTE;
        this.seconds = totalTimeWithoutHours - minutes * SECONDS_IN_MINUTE;
    }

    public Time(int hours, int minutes, int seconds) {
        this.hours = hours;
        this.minutes = minutes;
        this.seconds = seconds;
    }

    public int getHours() {
        return hours;
    }

    public void setHours(int hours) {
        this.hours = hours;
    }

    public int getMinutes() {
        return minutes;
    }

    public void setMinutes(int minutes) {
        this.minutes = minutes;
    }

    public int getSeconds() {
        return seconds;
    }

    public void setSeconds(int seconds) {
        this.seconds = seconds;
    }

    public int getTotalSeconds() {
        return convertTimeToSeconds();
    }

    private int convertTimeToSeconds() {
        return SECONDS_IN_HOUR * hours + SECONDS_IN_MINUTE * minutes + seconds;
    }
}

?

public class TimeTest {
    @Test
    void convertSecondToHoursMinutesSecondsTest1() {
        Time time = new Time(3661);
        Assertions.assertEquals(1, time.getHours());
        Assertions.assertEquals(1, time.getMinutes());
        Assertions.assertEquals(1, time.getSeconds());
    }

    @Test
    void convertSecondToHoursMinutesSecondsTest2() {
        Time time = new Time(20715);
        Assertions.assertEquals(5, time.getHours());
        Assertions.assertEquals(45, time.getMinutes());
        Assertions.assertEquals(15, time.getSeconds());
    }

    @Test
    void convertSecondToHoursMinutesSecondsTest3() {
        Time time = new Time(11601);
        Assertions.assertEquals(3, time.getHours());
        Assertions.assertEquals(13, time.getMinutes());
        Assertions.assertEquals(21, time.getSeconds());
    }

    @Test
    void convertSecondToHoursMinutesSecondsTest4() {
        Time time = new Time(3601);
        Assertions.assertEquals(1, time.getHours());
        Assertions.assertEquals(0, time.getMinutes());
        Assertions.assertEquals(1, time.getSeconds());
    }

    @Test
    void convertSecondToHoursMinutesSecondsTest5() {
        Time time = new Time(0);
        Assertions.assertEquals(0, time.getHours());
        Assertions.assertEquals(0, time.getMinutes());
        Assertions.assertEquals(0, time.getSeconds());
    }

    @Test
    void convertSecondToHoursMinutesSecondsTest6() {
        Time time = new Time(621);
        Assertions.assertEquals(0, time.getHours());
        Assertions.assertEquals(10, time.getMinutes());
        Assertions.assertEquals(21, time.getSeconds());
    }
    @Test
    void convertSecondToHoursMinutesSecondsTest7() {
        Time time = new Time(7860);
        Assertions.assertEquals(2, time.getHours());
        Assertions.assertEquals(11, time.getMinutes());
        Assertions.assertEquals(0, time.getSeconds());
    }
}

?

public class AtmTest {
    // @Test       the code is not flexible. need to think about the variability
    //void withdrawMoneyTest1() {
    //    Atm atm = new Atm(10, 5, 10);
    //    Assertions.assertEquals(true, atm.isPossibleIssue(1270));
    // }

    @Test
    void withdrawMoneyTest2() {
        Atm atm = new Atm(10, 5, 10);
        Assertions.assertFalse(atm.isPossibleIssue(1300));
    }