Sha256: 4f0f6d9f12ef4a7771db6eafda2e82b54c34a224bf729600122ae09c98567aa7

Contents?: true

Size: 1.64 KB

Versions: 363

Compression:

Stored size: 1.64 KB

Contents

#import "ClockExample.h"

@interface Clock ()

@property (nonatomic, assign) int hours;
@property (nonatomic, assign) int minutes;

@end

@implementation Clock

+ (Clock *)clockWithHours:(int)hours minutes:(int)minutes {
    return [[Clock alloc] initWithHours:hours minutes:minutes];
}

+ (Clock *)clockWithHours:(int)hours {
    return [[Clock alloc] initWithHours:hours minutes:0];
}

- (instancetype)initWithHours:(int)hours minutes:(int)minutes {
    if (self = [super init]) {
        _hours = hours;
        _minutes = minutes;
        [self normalize];
    }
    
    return self;
}

- (void)normalize {
    if (self.minutes >= 60) {
        self.hours += self.minutes / 60;
        self.minutes = self.minutes % 60;
    }
    
    while (self.minutes < 0) {
        self.hours -= 1;
        self.minutes += 60;
    }
    
    if (self.hours >= 24) {
        self.hours = self.hours % 24;
    }
    
    while (self.hours < 0) {
        self.hours += 24;
    }
}

- (Clock *)addMinutes:(int)minutes {
    return [Clock clockWithHours: self.hours minutes: self.minutes + minutes];
}

- (Clock *)subtractMinutes:(int)minutes {
    return [self addMinutes:-minutes];
}

- (NSString *)description {
    return [NSString stringWithFormat:@"%.2d:%.2d", self.hours, self.minutes];
}

- (BOOL)isEqual:(id)object {
    if (self == object) {
        return YES;
    }
    
    if (![object isKindOfClass:[Clock class]]) {
        return NO;
    }
    
    return [self isEqualToClock:(Clock *)object];
}

- (BOOL)isEqualToClock:(Clock *)clock {
    return [[self description] isEqualToString:[clock description]];
}

-(NSUInteger)hash {
    return [[self description] hash];
}

@end

Version data entries

363 entries across 363 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.118 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.117 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.116 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.115 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.114 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.113 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.111 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.110 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.109 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.108 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.107 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.106 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.105 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.104 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.103 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.102 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.101 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.100 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.99 tracks/objective-c/exercises/clock/ClockExample.m