-
Notifications
You must be signed in to change notification settings - Fork 0
/
[Ori]CodigoRepetido-Ejercicio.st
271 lines (185 loc) · 7.62 KB
/
[Ori]CodigoRepetido-Ejercicio.st
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
!classDefinition: #CantSuspend category: 'CodigoRepetido-Ejercicio'!
Error subclass: #CantSuspend
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'CodigoRepetido-Ejercicio'!
!classDefinition: #NotFound category: 'CodigoRepetido-Ejercicio'!
Error subclass: #NotFound
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'CodigoRepetido-Ejercicio'!
!classDefinition: #CustomerBookTest category: 'CodigoRepetido-Ejercicio'!
TestCase subclass: #CustomerBookTest
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'CodigoRepetido-Ejercicio'!
!CustomerBookTest methodsFor: 'testing' stamp: 'NR 4/3/2019 10:50:19'!
test01AddingCustomerShouldNotTakeMoreThan50Milliseconds
| customerBook millisecondsBeforeRunning millisecondsAfterRunning |
customerBook := CustomerBook new.
millisecondsBeforeRunning := Time millisecondClockValue * millisecond.
customerBook addCustomerNamed: 'John Lennon'.
millisecondsAfterRunning := Time millisecondClockValue * millisecond.
self assert: (millisecondsAfterRunning-millisecondsBeforeRunning) < (50 * millisecond)
! !
!CustomerBookTest methodsFor: 'testing' stamp: 'NR 4/3/2019 10:50:13'!
test02RemovingCustomerShouldNotTakeMoreThan100Milliseconds
| customerBook millisecondsBeforeRunning millisecondsAfterRunning paulMcCartney |
customerBook := CustomerBook new.
paulMcCartney := 'Paul McCartney'.
customerBook addCustomerNamed: paulMcCartney.
millisecondsBeforeRunning := Time millisecondClockValue * millisecond.
customerBook removeCustomerNamed: paulMcCartney.
millisecondsAfterRunning := Time millisecondClockValue * millisecond.
self assert: (millisecondsAfterRunning-millisecondsBeforeRunning) < (100 * millisecond)
! !
!CustomerBookTest methodsFor: 'testing' stamp: 'HernanWilkinson 5/9/2012 18:12'!
test03CanNotAddACustomerWithEmptyName
| customerBook |
customerBook := CustomerBook new.
[ customerBook addCustomerNamed: ''.
self fail ]
on: Error
do: [ :anError |
self assert: anError messageText = CustomerBook customerCanNotBeEmptyErrorMessage.
self assert: customerBook isEmpty ]! !
!CustomerBookTest methodsFor: 'testing' stamp: 'HAW 8/28/2017 08:57:25'!
test04CanNotRemoveAnInvalidCustomer
| customerBook johnLennon |
customerBook := CustomerBook new.
johnLennon := 'John Lennon'.
customerBook addCustomerNamed: johnLennon.
[ customerBook removeCustomerNamed: 'Paul McCartney'.
self fail ]
on: NotFound
do: [ :anError |
self assert: customerBook numberOfCustomers = 1.
self assert: (customerBook includesCustomerNamed: johnLennon) ]
! !
!CustomerBookTest methodsFor: 'testing' stamp: 'NR 4/3/2019 10:50:25'!
test05SuspendingACustomerShouldNotRemoveItFromCustomerBook
| customerBook paulMcCartney|
customerBook := CustomerBook new.
paulMcCartney := 'Paul McCartney'.
customerBook addCustomerNamed: paulMcCartney.
customerBook suspendCustomerNamed: paulMcCartney.
self assert: 0 equals: customerBook numberOfActiveCustomers.
self assert: 1 equals: customerBook numberOfSuspendedCustomers.
self assert: 1 equals: customerBook numberOfCustomers.
self assert: (customerBook includesCustomerNamed: paulMcCartney).
! !
!CustomerBookTest methodsFor: 'testing' stamp: 'NR 4/3/2019 10:50:28'!
test06RemovingASuspendedCustomerShouldRemoveItFromCustomerBook
| customerBook paulMcCartney|
customerBook := CustomerBook new.
paulMcCartney := 'Paul McCartney'.
customerBook addCustomerNamed: paulMcCartney.
customerBook suspendCustomerNamed: paulMcCartney.
customerBook removeCustomerNamed: paulMcCartney.
self assert: 0 equals: customerBook numberOfActiveCustomers.
self assert: 0 equals: customerBook numberOfSuspendedCustomers.
self assert: 0 equals: customerBook numberOfCustomers.
self deny: (customerBook includesCustomerNamed: paulMcCartney).
! !
!CustomerBookTest methodsFor: 'testing' stamp: 'NR 9/17/2020 06:03:47'!
test07CanNotSuspendAnInvalidCustomer
| customerBook johnLennon |
customerBook := CustomerBook new.
johnLennon := 'John Lennon'.
customerBook addCustomerNamed: johnLennon.
[ customerBook suspendCustomerNamed: 'George Harrison'.
self fail ]
on: CantSuspend
do: [ :anError |
self assert: customerBook numberOfCustomers = 1.
self assert: (customerBook includesCustomerNamed: johnLennon) ]
! !
!CustomerBookTest methodsFor: 'testing' stamp: 'NR 9/19/2018 17:57:11'!
test08CanNotSuspendAnAlreadySuspendedCustomer
| customerBook johnLennon |
customerBook := CustomerBook new.
johnLennon := 'John Lennon'.
customerBook addCustomerNamed: johnLennon.
customerBook suspendCustomerNamed: johnLennon.
[ customerBook suspendCustomerNamed: johnLennon.
self fail ]
on: CantSuspend
do: [ :anError |
self assert: customerBook numberOfCustomers = 1.
self assert: (customerBook includesCustomerNamed: johnLennon) ]
! !
!classDefinition: #CustomerBook category: 'CodigoRepetido-Ejercicio'!
Object subclass: #CustomerBook
instanceVariableNames: 'suspended active'
classVariableNames: ''
poolDictionaries: ''
category: 'CodigoRepetido-Ejercicio'!
!CustomerBook methodsFor: 'testing' stamp: 'NR 4/3/2019 10:14:26'!
includesCustomerNamed: aName
^(active includes: aName) or: [ suspended includes: aName ]! !
!CustomerBook methodsFor: 'testing' stamp: 'NR 4/3/2019 10:14:26'!
isEmpty
^active isEmpty and: [ suspended isEmpty ]! !
!CustomerBook methodsFor: 'initialization' stamp: 'NR 9/17/2020 07:23:04'!
initialize
active := OrderedCollection new.
suspended:= OrderedCollection new.! !
!CustomerBook methodsFor: 'customer management' stamp: 'NR 4/3/2019 10:14:26'!
addCustomerNamed: aName
aName isEmpty ifTrue: [ self signalCustomerNameCannotBeEmpty ].
((active includes: aName) or: [suspended includes: aName]) ifTrue: [ self signalCustomerAlreadyExists ].
active add: aName ! !
!CustomerBook methodsFor: 'customer management' stamp: 'NR 4/3/2019 10:14:26'!
numberOfActiveCustomers
^active size! !
!CustomerBook methodsFor: 'customer management' stamp: 'NR 4/3/2019 10:14:26'!
numberOfCustomers
^active size + suspended size! !
!CustomerBook methodsFor: 'customer management' stamp: 'NR 9/19/2018 17:36:09'!
numberOfSuspendedCustomers
^suspended size! !
!CustomerBook methodsFor: 'customer management' stamp: 'NR 4/3/2019 10:14:26'!
removeCustomerNamed: aName
1 to: active size do:
[ :index |
aName = (active at: index)
ifTrue: [
active removeAt: index.
^ aName
]
].
1 to: suspended size do:
[ :index |
aName = (suspended at: index)
ifTrue: [
suspended removeAt: index.
^ aName
]
].
^ NotFound signal.
! !
!CustomerBook methodsFor: 'customer management' stamp: 'HernanWilkinson 7/6/2011 17:52'!
signalCustomerAlreadyExists
self error: self class customerAlreadyExistsErrorMessage! !
!CustomerBook methodsFor: 'customer management' stamp: 'HernanWilkinson 7/6/2011 17:51'!
signalCustomerNameCannotBeEmpty
self error: self class customerCanNotBeEmptyErrorMessage ! !
!CustomerBook methodsFor: 'customer management' stamp: 'NR 4/3/2019 10:14:26'!
suspendCustomerNamed: aName
(active includes: aName) ifFalse: [^CantSuspend signal].
active remove: aName.
suspended add: aName
! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
!classDefinition: 'CustomerBook class' category: 'CodigoRepetido-Ejercicio'!
CustomerBook class
instanceVariableNames: ''!
!CustomerBook class methodsFor: 'error messages' stamp: 'NR 4/9/2023 22:25:52'!
customerAlreadyExistsErrorMessage
^'Customer already exists!!!!!!'! !
!CustomerBook class methodsFor: 'error messages' stamp: 'NR 4/9/2023 22:25:56'!
customerCanNotBeEmptyErrorMessage
^'Customer name cannot be empty!!!!!!'! !