export class Clock {
  private _hour = 0;
  private _minute = 0;

  private setTime(hour: number, minute: number = 0): void {
    this._minute = minute % 60;
    if (this._minute < 0) {
      this._minute = 60 + this._minute;
    }

    this._hour = (hour + Math.floor(minute / 60)) % 24;
    if (this._hour < 0) {
      this._hour = (this._hour % 24) + 24;
    }
  }

  constructor(hour: number, minute?: number) {
    this.setTime(hour, minute);
  }

  public toString(): string {
    return `${String(this._hour).padStart(2, "0")}:${String(
      this._minute
    ).padStart(2, "0")}`;
  }

  public plus(minutes: number): Clock {
    return new Clock(this._hour, this._minute + minutes);
  }

  public minus(minutes: number): Clock {
    return new Clock(this._hour, this._minute - minutes);
  }

  public equals(other: Clock): boolean {
    return this.toString() === other.toString();
  }
}
