- Create a custom class file that inherits from
NSObject
. - Import another custom class.
- Declare a property on a custom class.
- Write methods for the custom class to perform on a property that it owns.
Open the iOShoppingCart.xcworkspace
file. You should see the familiar FISAppDelegate
class and a FISItem
class set up already.
I. Create a new FISShoppingCart
class the inherits from NSObject
.
Import FISItem
into FISShoppingCart
's header file.
Declare one property, an NSMutableArray
called items
.
Declare the following 11 methods in FISShoppingCart.h
:
calculateTotalPriceInCents
which takes no arguments and returns anNSUInteger
,addItem:
which takes oneFISItem
argument calleditem
and provides no return,removeItem:
which takes oneFISItem
argument calleditem
and provides no return,removeAllItemsLikeItem:
which takes onFISItem
argument calleditem
and provides no return,sortItemsByNameAsc
which takes no arguments and provides no return,sortItemsByNameDesc
which takes no arguments and provides no return,sortItemsByPriceInCentsAsc
which takes no arguments and provides no return,sortItemsByPriceInCentsDesc
which takes no arguments and provides no return,allItemsWithName:
which takes oneNSString
argument calledname
and returns anNSArray
,allItemsWithMinimumPriceInCents:
which takes oneNSUInteger
argument and returns anNSArray
, andallItemsWithMaximumPriceInCents:
which takes oneNSUInteger
argument and returns anNSArray
.
Define the initial method implementations in FISShoppingCart.m
to either:
return nil
for methods that provide a return, or- do nothing for methods that do not provide a return.
Run the tests with โ
U
to check that the tests in FISShoppingCartSpec.m
initially fail.
II. Complete each of the method implementations so that they pass their tests:
-
calculateTotalPriceInCents
should calculate the sum of thepriceInCents
property of all the items in theitems
array. Naturally, this should return0
if theitems
array is empty. -
addItem:
should add the submitted item to theitems
array, even if the item is already in theitems
array (sometimes you just need to stock up on hot pockets!). -
removeItem:
should remove a single occurrence of an item in theitems
array.
Hint: UseNSMutableArray
'sindexOfObject:
andremoveObjectAtIndex:
method. -
removeAllItemsLikeItem:
should remove all occurrences of an item in theitems
array. -
sortItemsByNameAsc
should sort the contents of theitems
array by theirname
property in ascending order (alphabetical).
Hint: UseNSSortDescriptor
for these methods. -
sortItemsByNameDesc
should sort the contents of theitems
array by theirname
property in descending order. -
sortItemsByPriceInCentsAsc
should sort the contents of theitems
array by thepriceInCents
property in ascending order. -
sortItemsByPriceInCentsDesc
should sort the the contents of theitems
array by thepriceInCents
property in descending order. -
allItemsWithName:
should return an array containing only the items whosename
property matches the argument string.
Hint: UseNSPredicate
for these methods. -
allItemsWithMinimumPriceInCents:
should return an array containing only the items whosepriceInCents
property is greater than or equal to the argument integer. -
allItemsWithMaximumPriceInCents:
should return an array containing only those items whosepriceInCents
property is less than or equal to the argument integer.