-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckoutTests.rb
More file actions
140 lines (125 loc) · 2.25 KB
/
checkoutTests.rb
File metadata and controls
140 lines (125 loc) · 2.25 KB
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
require 'test/unit'
class TestCheckout < Test::Unit::TestCase
def test_scan_one_a_total_is_50
Checkout.new(self)
.scan('A')
.total()
assert_equal(50, @total)
end
def test_scan_one_b_total_is_30
Checkout.new(self)
.scan('B')
.total()
assert_equal(30, @total)
end
def test_scan_one_c_total_is_60
Checkout.new(self)
.scan('C')
.total()
assert_equal(60, @total)
end
def test_scan_one_d_total_is_99
Checkout.new(self)
.scan('D')
.total()
assert_equal(99, @total)
end
def test_scan_two_a_total_is_100
Checkout.new(self)
.scan('A')
.scan('A')
.total()
assert_equal(100, @total)
end
def test_scan_one_a_one_b_one_c_one_d_total_is_239
Checkout.new(self)
.scan('A')
.scan('B')
.scan('C')
.scan('D')
.total()
assert_equal(239, @total)
end
def test_scan_three_a_special_offer_total_is_130
Checkout.new(self)
.scan('A')
.scan('A')
.scan('A')
.total()
assert_equal(130, @total)
end
def test_scan_two_b_special_offer_total_is_45
Checkout.new(self)
.scan('B')
.scan('B')
.total()
assert_equal(45, @total)
end
def test_scan_six_a_special_offer_total_is_260
Checkout.new(self)
.scan('A')
.scan('A')
.scan('A')
.scan('A')
.scan('A')
.scan('A')
.total()
assert_equal(260, @total)
end
def test_scan_four_b_special_offer_total_is_90
Checkout.new(self)
.scan('B')
.scan('B')
.scan('B')
.scan('B')
.total()
assert_equal(90, @total)
end
def show_total(total)
@total = total
end
end
class Checkout
def initialize(display)
# constructor
@display = display
@a_count = 0
@b_count = 0
end
def scan(q)
# need to make sure total is initialised.. otherwise things go bang
ensure_set_i
if (q == 'A')
@i += 50
@a_count+=1
end
if (q == 'B')
@i += 30
@b_count+=1
end
if (q == 'C')
@i += 60
end
if (q == 'D')
@i += 99
end
return self
end
def ensure_set_i
# is it set?
if (@i.nil?)
@i = 0
end
end
def total
if (@a_count == 3)
@i -= (@a_count == 3) ? 20 : 0
else
# need to make sure we deal with multiple A SOs
@i -= (@a_count % 3 == 0) ? @a_count / 3 * 20 : 0
end
# b specials
@i -= (@b_count == 2 || @b_count % 2 == 0) ? @b_count /2 * 15 : 0
@display.show_total(@i)
end
end