-
Notifications
You must be signed in to change notification settings - Fork 0
/
BabySitterCharges.pm
146 lines (114 loc) · 2.93 KB
/
BabySitterCharges.pm
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package BabySitterCharges;
use strict;
use warnings;
use constant START_PAY => 12;
use constant BED_PAY => 8;
use constant MIDNIGHT_PAY => 16;
sub new {
my ( $class, %args ) = @_;
my $self = bless{}, $class;
return $self;
};
sub calculate() {
my ( $self ) = @_;
my $total = 0;
$total += START_PAY * ( $self->{bed_hour} - $self->{start_hour} );
$total += BED_PAY * ( 24 - $self->{bed_hour} );
$total += MIDNIGHT_PAY * ( $self->{end_hour} - 24 );
return '$' . sprintf('%.2f', $total);
}
sub set_start_time($) {
my ( $self, $start_time ) = @_;
return 0 unless $start_time && $start_time =~ /\:/;
my @tmp = split(':',$start_time);
my $hour = $tmp[0];
# Add 12 to the hour if it's in the PM
if ( $start_time =~ /PM/ ) {
$hour += 12;
}
if ( $hour >= 17 ) {
$self->{start_time} = $start_time;
$self->{start_hour} = $hour;
return 1;
}
else {
# Don't save it if it's before 5 PM
return 0;
}
}
sub set_end_time($) {
my ( $self, $end_time ) = @_;
return 0 unless $end_time && $end_time =~ /\:/;
my @tmp = split(':',$end_time);
my $hour = $tmp[0];
# Add 12 to the hour if it's in the PM
if ( $end_time =~ /PM/ ) {
$hour += 12;
}
elsif ( $hour < 12 ) {
# It's in the morning, so make the hour be for
# the next day so subtracting is easier later
$hour += 24;
}
if ( $hour <= 28 ) {
$self->{end_time} = $end_time;
$self->{end_hour} = $hour;
return 1;
}
else {
# Don't save it if it's after 4 AM
return 0;
}
}
sub set_bed_time($) {
my ( $self, $bed_time ) = @_;
return 0 unless $bed_time && $bed_time =~ /\:/;
# Can't set bedtime unless we have start and end time
return 0 unless $self->{start_time} && $self->{end_time};
my @tmp = split(':',$bed_time);
my $hour = $tmp[0];
# Add 12 to the hour if it's in the PM
if ( $bed_time =~ /PM/ ) {
$hour += 12;
}
if ( $hour > $self->{start_hour} && $hour < $self->{end_hour} ) {
if ( $hour >= 24 ) {
# No kid that still needs babysat
# should be up past midnight
return 0;
}
$self->{bed_time} = $bed_time;
$self->{bed_hour} = $hour;
return 1;
}
else {
# Don't save it if it's not between the start and end times
return 0;
}
}
sub get_start_time() {
my ( $self ) = @_;
return $self->{start_time};
}
sub get_end_time() {
my ( $self ) = @_;
return $self->{end_time};
}
sub get_bed_time() {
my ( $self ) = @_;
return $self->{bed_time};
}
###### Functions for testing only ######
sub get_start_hour() {
my ( $self ) = @_;
return $self->{start_hour};
}
sub get_end_hour() {
my ( $self ) = @_;
return $self->{end_hour};
}
sub get_bed_hour() {
my ( $self ) = @_;
return $self->{bed_hour};
}
1;