Im new to swift language and I'm trying to find a solution for my problem. I have created a class called Book
I am tring to create Book. A book has name (can’t be changed after creation), purchaseid (can’t be changed after creation), and market price (can be changed)
and also a class Shelf. A shelf has name (that can’t be changed after creation) and an array of books (that can be changed by calling add and delete methods and is unique on book purchaseId - ie. doesn’t have duplicate purchaseIds). Shelf also has a method for computing the average price of books in the shelf.
I am trying to find a solution on how to check an array before adding to it and taking books purchaseId and deleting a book from the array.
here is my code:
class Book{
let name: String
let purchaseID: Int
let marketPrice: Double
init(name: String, purchaseID: Int, marketPrice: Double) {
self.name = name
self.purchaseID = purchaseID
self.marketPrice = marketPrice
}
func bookPrinter() -> String {
return "Name: \(self.name) Price: \(marketPrice)"
}
}
class Shelf{
private let shelfName: String
private var arrayOfBooks = [Book]()
init(shelfName: String) {
self.shelfName = shelfName
}
func add(book : Book ){
}
func delete(book : Book){
}
func printer() {
for b in self.arrayOfBooks{
print(b.bookPrinter())
}
}
}
purchaseId
uniquely identifies a Book?Set