Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Commit

Permalink
agregada consigna ej2
Browse files Browse the repository at this point in the history
  • Loading branch information
aguirre-ivan authored Oct 14, 2021
1 parent ff9cd23 commit de5c145
Show file tree
Hide file tree
Showing 2 changed files with 284 additions and 0 deletions.
273 changes: 273 additions & 0 deletions 02-CodigoRepetido/CodigoRepetido-Ejercicio.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
!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: 'tests' 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: 'tests' 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: 'tests' 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: 'tests' 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: 'tests' 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: 'tests' 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: 'tests' stamp: 'NR 4/30/2020 09:08:46'!
test07CanNotSuspendAnInvalidCustomer

| customerBook johnLennon |

customerBook := CustomerBook new.
johnLennon := 'John Lennon'.
customerBook addCustomerNamed: johnLennon.

[ customerBook suspendCustomerNamed: 'Ringo Starr'.
self fail ]
on: CantSuspend
do: [ :anError |
self assert: customerBook numberOfCustomers = 1.
self assert: (customerBook includesCustomerNamed: johnLennon) ]
! !

!CustomerBookTest methodsFor: 'tests' 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: 'initialization' stamp: 'LL 10/30/2020 12:22: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'!
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: 'NR 4/3/2019 10:14:26'!
suspendCustomerNamed: aName

(active includes: aName) ifFalse: [^CantSuspend signal].

active remove: aName.

suspended add: aName
! !


!CustomerBook methodsFor: 'accessing' stamp: 'NR 4/3/2019 10:14:26'!
numberOfActiveCustomers

^active size! !

!CustomerBook methodsFor: 'accessing' stamp: 'NR 4/3/2019 10:14:26'!
numberOfCustomers

^active size + suspended size! !

!CustomerBook methodsFor: 'accessing' stamp: 'NR 9/19/2018 17:36:09'!
numberOfSuspendedCustomers

^suspended size! !


!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: 'signal errors' stamp: 'HernanWilkinson 7/6/2011 17:52'!
signalCustomerAlreadyExists

self error: self class customerAlreadyExistsErrorMessage! !

!CustomerBook methodsFor: 'signal errors' stamp: 'HernanWilkinson 7/6/2011 17:51'!
signalCustomerNameCannotBeEmpty

self error: self class customerCanNotBeEmptyErrorMessage ! !

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

!classDefinition: 'CustomerBook class' category: 'CodigoRepetido-Ejercicio'!
CustomerBook class
instanceVariableNames: ''!

!CustomerBook class methodsFor: 'error messages' stamp: 'NR 4/30/2020 09:05:18'!
customerAlreadyExistsErrorMessage

^'Customer Already Exists'! !

!CustomerBook class methodsFor: 'error messages' stamp: 'NR 4/30/2020 09:05:25'!
customerCanNotBeEmptyErrorMessage

^'Customer Name Cannot Be Empty'! !
11 changes: 11 additions & 0 deletions 02-CodigoRepetido/Consigna.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Código Repetido

Este ejercicio tiene por objetivo que saquen el código repetido que encuentren en el modelo y en los tests. Por ej. entre el test01 y test02.

Los tests provistos ya funcionan, simplemente hay que sacar el código repetido y los tests deben seguir funcionando.

Se pueden modificar las clases provistas, sólo para eliminar código repetido. No se puede modificar lo que verifican los tests. Es decir, sólo se puede hacer un cambio de diseño de tal manera que siga testeando lo mismo, que la funcionalidad sea la misma, pero que no haya código repetido.

Consejo: Comiencen por intentar quitar el código repetido de los tests, y luego sigan por el modelo.

Aclaración: Para hacer este ejercicio más sencillo se modela a un Customer utilizando un String en vez de una clase Customer. No es el objetivo del ejercicio que ustedes corrijan esta decisión, ni las consecuencias que trae consigo (por ej. que no se pueda agregar al CustomerBook dos Customers diferentes con el mismo nombre).

0 comments on commit de5c145

Please sign in to comment.