-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContents.swift
130 lines (91 loc) · 3.44 KB
/
Contents.swift
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
import UIKit
// Challenge #3: The Library Challenge
//
// Instructions:
// Complete the class definition so that you get the expected output in the console (specified below the class definitions). See TODOs.
//
class Person {
var name:String!
init(_ fullName:String) {
name = fullName
}
}
class Book {
var title:String!
var author:String!
init(_ bookTitle:String, _ bookAuthor:String) {
title = bookTitle
author = bookAuthor
}
}
// --- Your code goes below this line ---
class Library {
var catalogue = ["ORW":Book("1984", "George Orwell"),
"RAY":Book("Fahrenheit 451", "Ray Bradbury")]
var checkedOutBooks = [String:Person]()
func searchByTitle(_ title:String) -> String {
guard let (bookId, _) = catalogue.first(where: { $1.title == title }) else { return "Not in catalogue" }
guard let person = checkedOutBooks[bookId] else { return "Available" }
return "Checked out by \(person.name!)"
}
func checkOut(_ bookId:String, _ person:Person) -> String {
// TODO: This function adds to the checkedOutBooks dictionary
//
// Returns "Error: Book already checked out" if the book is already in the checkedOutBooks dictionary
//
// Returns "Successfully checked out" and adds the bookId,person key-value pair if the book doesn't currently exist in the checkedOutbooks dictionary
//
// Returns "Book doesn't exist" if the book isn't in the catalogue dictionary
return ""
}
func checkIn(_ bookId:String) -> String {
// TODO: This function removes the bookId,person key-value pair from the checkedOutBooks dictionary
//
// Returns "Book doesn't exist" if the book isn't in the catalogue dictionary
//
// Returns "Error: Can't check in a book that hasn't been checked out" if the book wasn't checked out in the first place
//
// Returns "Successfully checked in"
return ""
}
}
// --- Your code goes above this line ---
// --- Don't edit or add anything below this line ---
let lib = Library()
let borrower1 = Person("Curious George")
let borrower2 = Person("Mark Twain")
// George searches for a book
let searchResult = lib.searchByTitle("1984")
print(searchResult)
// Expected Output in console:
// "Available"
// George checks out the book
let borrowResult = lib.checkOut("ORW", borrower1)
print(borrowResult)
// Expected Output in console:
// "Successfully checked out"
// Mark searches for a book
let searchResult2 = lib.searchByTitle("1984")
print(searchResult2)
// Expected Output in console:
// "Checked out by Curious George"
// Mark tries to borrow a book that's already checked out
let borrowResult2 = lib.checkOut("ORW", borrower2)
print(borrowResult2)
// Expected Output in console:
// "Error: Book already checked out"
// A book is checked in
let checkInResult = lib.checkIn("RAY")
print(checkInResult)
// Expected Output in console:
// "Error: Can't check in a book that hasn't been checked out"
// George checks in his book
let checkInResult2 = lib.checkIn("ORW")
print(checkInResult2)
// Expected Output in console:
// "Successfully checked in"
// Mark attempts to borrow the book again
let borrowResult3 = lib.checkOut("ORW", borrower2)
print(borrowResult3)
// Expected Output in console:
// "Successfully checked out"