-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
ClockExample.m
77 lines (59 loc) · 1.5 KB
/
ClockExample.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#import "ClockExample.h"
@implementation Clock {
int _hours, _minutes;
}
+ (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 (_minutes >= 60) {
_hours += _minutes / 60;
_minutes = _minutes % 60;
}
while (_minutes < 0) {
_hours -= 1;
_minutes += 60;
}
if (_hours >= 24) {
_hours = _hours % 24;
}
while (_hours < 0) {
_hours += 24;
}
}
- (Clock *)addMinutes:(int)minutes {
return [Clock clockWithHours: _hours minutes: _minutes + minutes];
}
- (Clock *)subtractMinutes:(int)minutes {
return [self addMinutes:-minutes];
}
- (NSString *)description {
return [NSString stringWithFormat:@"%.2d:%.2d", _hours, _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