- Create a class file.
- Create a property for a custom class.
- Write methods on your custom class which utilize a custom type.
Open swift-shopping-cart.xcworkspace
and navigate to the Item.swift
file.
The Item
class is already set up for you:
// Item.swift
import Foundation
class Item: Equatable, CustomDebugStringConvertible {
var name = ""
var priceInCents = 0
var debugDescription: String { return name }
init(name: String, priceInCents: Int) {
self.name = name
self.priceInCents = priceInCents
}
}
func ==(lhs: Item, rhs: Item) -> Bool {
let sameName = lhs.name == rhs.name
let samePrice = lhs.priceInCents == rhs.priceInCents
return sameName && samePrice
}
You can see that it has two settable properties, a string called name
and an integer called priceInCents
. It's also written with an initializer that takes an argument for either property.
The debugDescription
property is set up as a "calculated property" which simply returns the name. This property is required for conformance with the CustomDebugStringConvertible
protocol that customizes how the class is read by the debug tools.
You'll notice that the Item
class also conforms to the Equatable
protocol, and the class definition is accompanied by an override of the equality comparator ==
that determines whether two Item
s are equal. This has the effect of allowing you to use the equality comparator ==
on two instances of the Item
class (and allows access to various methods like contains(_:)
that only make sense on arrays of equatable objects).
-
In a new file, create a new class named
Cart
. -
Give it one property, an empty array of the
Item
Type nameditems
.
-
Write a method named
totalPriceInCents()
that takes no arguments and returns anInt
. This method should return the total cost of all the items in theitems
array. -
Write a method named
addItem(_:)
that takes one argument of typeItem
and provides no return. This method should add the argument to the end of theitems
property array. -
Write a method named
removeItem(_:)
that takes one argument of typeItem
and provides no return. This method should remove an instance from theitems
array that matches the argument item. -
Write a method named
itemsWithName(_:)
that takes one string argument and returns an array of typeItem
. This method should collect all of the items in theitems
property array whosename
property matches the submitted string argument. -
Write a method named
itemsWithMinimumPriceInCents(_:)
that takes one integer argument and returns an array of typeItem
. This method should collect all of the items in theitems
property array whosepriceInCents
property is greater than or equal to the submitted integer argument. -
Write a method named
itemsWithMaximumPriceInCents(_:)
that take one integer argument and return an array of typeItem
. The method should collect all of the items in theitems
property array whosepriceInCents
property is less than or equal to the submitted integer argument.