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.180 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.179 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.178 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.177 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.176 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.175 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.174 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.173 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.172 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.171 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.170 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.169 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.167 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.166 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.165 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.164 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.163 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.162 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.161 tracks/objective-c/exercises/clock/ClockExample.m
trackler-2.2.1.160 tracks/objective-c/exercises/clock/ClockExample.m