SHARE
TWEET

Chris_Ching's_Swift_Chalange_03

sleepy_coder May 26th, 2019 (edited) 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. // CodeWithChris Learn Swift for Beginners
  3. // http://codewithchris.com/learn-swift
  4. //
  5. // Challenge #3: The Library Challenge
  6. //
  7. // Instructions:
  8. // Complete the class definition so that you get the expected output in the console(specified below the class definitions). See TODOs.
  9. //
  10.  
  11. class Person {
  12.  
  13.     var name:String!
  14.  
  15.     init(_ fullName:String) {
  16.         name = fullName
  17.     }
  18. }
  19.  
  20.  
  21. //let newPerson = Person("Sebet")
  22.  
  23.  
  24.  
  25. class Book {
  26.  
  27.     var title:String!
  28.     var author:String!
  29.  
  30.     init(_ bookTitle:String, _ bookAuthor:String) {
  31.         title = bookTitle
  32.         author = bookAuthor
  33.     }
  34.  
  35. }
  36.  
  37. //let newBook = Book("The Adventures of Tom Soyare", "Samuel Longhorn Clemens")
  38.  
  39. // --- Your code goes below this line ---
  40.  
  41. class Library {
  42.  
  43.     var catalogue = ["ORW":Book("1985", "George Orwell"), "RAY":Book("Fahrenheit 451", "Ray Bradbury")]
  44.     var checkedOutBooks = [String:Person]()
  45.  
  46.     func searchByTitle(_ title:String) -> String {
  47.  
  48.         // TODO: This function searches the catalogue dictionary for a title
  49.         //
  50.         // Returns "Available" if the book exists and isn't checked out
  51.         //
  52.         // Returns "Checked out by name" if the book exists and is checked out
  53.         //
  54.         // Returns "Not in catalogue" if the book doesn't exist
  55.  
  56.         for item in catalogue {
  57.             if item.value.title.lowercased() == title.lowercased() && checkedOutBooks[item.key] == nil {
  58.                 return "Available"
  59.             } else if item.value.title.lowercased() == title.lowercased() && checkedOutBooks[item.key] != nil {
  60.                 return "Checked out by " + (String)(checkedOutBooks[item.key]?.name ?? "nobody")
  61.             }
  62.         }
  63.  
  64.         return "Not in catalogue"
  65.     }
  66.  
  67.     func checkOut(_ bookId:String, _ person:Person) -> String {
  68.  
  69.         // TODO: This function adds to the checkedOutBooks dictionary
  70.         //
  71.         // Returns "Error: Book already checked out" if the book is already in the checkedOutBooks dictionary
  72.         //
  73.         // Returns "Successfully checked out" and adds the bookId,person key-value pair if the book doesn't currently exist in the checkedOutbooks dictionary
  74.         //
  75.         // Returns "Book doesn't exist" if the book isn't in the catalogue dictionary
  76.  
  77.         if catalogue[bookId] != nil {
  78.             if checkedOutBooks[bookId] != nil {
  79.                 return "Error: Book already checked out"
  80.             } else if checkedOutBooks[bookId] == nil  {
  81.                 checkedOutBooks[bookId] = person
  82.                 return "Successfully checked out"
  83.             }
  84.         }
  85.  
  86.         return "Book doesn't exist"
  87.     }
  88.  
  89.     func checkIn(_ bookId:String) -> String {
  90.  
  91.         // TODO: This function removes the bookId,person key-value pair from the checkedOutBooks dictionary
  92.         //
  93.         // Returns "Book doesn't exist" if the book isn't in the catalogue dictionary
  94.         //
  95.         // 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
  96.         //
  97.         // Returns "Successfully checked in"
  98.  
  99.         if catalogue[bookId] != nil {
  100.             if checkedOutBooks[bookId] != nil {
  101.                 checkedOutBooks.removeValue(forKey: bookId)
  102.                 return "Successfully checked in"
  103.             } else if checkedOutBooks[bookId] == nil  {
  104.                 return "Error: Can't check in a book that hasn't been checked out"
  105.             }
  106.         }
  107.  
  108.         return "Book doesn't exist"
  109.     }
  110.  
  111. }
  112. // --- Your code goes above this line ---
  113.  
  114. // --- Don't edit or add anything below this line ---
  115.  
  116. let lib = Library()
  117. let borrower1 = Person("Curious George")
  118. let borrower2 = Person("Mark Twain")
  119.  
  120. // George searches for a book
  121. let searchResult = lib.searchByTitle("1985")
  122. print(searchResult)
  123.  
  124. // Expected Output in console:
  125. // "Available"
  126.  
  127. // George checks out the book
  128. let borrowResult = lib.checkOut("ORW", borrower1)
  129. print(borrowResult)
  130.  
  131. // Expected Output in console:
  132. // "Successfully checked out"
  133.  
  134. // Mark searches for a book
  135. let searchResult2 = lib.searchByTitle("1985")
  136. print(searchResult2)
  137.  
  138. // Expected Output in console:
  139. // "Checked out by Curious George"
  140.  
  141. // Mark tries to borrow a book that's already checked out
  142. let borrowResult2 = lib.checkOut("ORW", borrower2)
  143. print(borrowResult2)
  144.  
  145. // Expected Output in console:
  146. // "Error: Book already checked out"
  147.  
  148. // A book is checked in
  149. let checkInResult = lib.checkIn("RAY")
  150. print(checkInResult)
  151.  
  152. // Expected Output in console:
  153. // "Error: Can't check in a book that hasn't been checked out"
  154.  
  155. // George checks in his book
  156. let checkInResult2 = lib.checkIn("ORW")
  157. print(checkInResult2)
  158.  
  159. // Expected Output in console:
  160. // "Successfully checked in"
  161.  
  162. // Mark attempts to borrow the book again
  163. let borrowResult3 = lib.checkOut("ORW", borrower2)
  164. print(borrowResult3)
  165.  
  166. // Expected Output in console:
  167. // "Successfully checked out"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top