Learn Courses My Dashboard

Module 2 challenge 10

Hello, I am having fun with the challenge however I can not print out the peoples names renting out the books. How should I go about it ? HInts apreciated. Ewa


// The Library Challenge
//
// Instructions:
// Complete the Library 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 {
        
        // TODO: This function searches the catalogue dictionary for a title
        for _ in catalogue {
            
            if (title == "1984" && lib.catalogue["ORW"] != nil) || (title == "Fahrenheit 451" &&  lib.catalogue["RAY"] != nil) {
                return "Available"
            } else if title == "1984" && lib.checkedOutBooks["ORW"] != nil  {
                return "Checked out by \(String(describing: checkedOutBooks["ORW"]))"
            } else if
                title == "Fahrenheit 451" && lib.checkedOutBooks["RAY"] != nil {
                return "Checked out by \(String(describing: checkedOutBooks["RAY"]) )"
            } else {
                return "Not in catalogue"
            }
        }
        // Returns "Available" if the book exists and isn't checked out
        //
        // Returns "Checked out by name" if the book exists and is checked out
        //
        // Returns "Not in catalogue" if the book doesn't exist
        
        return ""
    }
    
    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
        if lib.checkedOutBooks[bookId] != nil {
            return "Error: Book already checked out"
        } else if lib.checkedOutBooks[bookId] == nil {
            lib.checkedOutBooks = [bookId:person]
            return "Successfully checked out the \(bookId) for \(person)"
        } else if
            lib.checkedOutBooks[bookId] == nil && lib.catalogue[bookId] == nil {
            return "Book doesn't exist"
        }
        
        // 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
        lib.checkedOutBooks[bookId] = nil
        // Returns "Book doesn't exist" if the book isn't in the catalogue dictionary
        if lib.catalogue[bookId] == nil {
            return "Book doesn't exist"
        } else if lib.catalogue[bookId] != nil {
            return "Error: Can't check in a book that hasn't been checked out"
        }
        // 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"

you can simply loop though your checkedOutBooks
its basically similar to your “searchByTitle” logic

if the checkedOutBooks is empty just print “no checked out books”

if its not then do a loop using the keys of the checkedOutBooks

for code in checkedOutBooks.keys {
//something like print(catalogue[book] is checked out by checkedOutBooks[code]
}

thank you Francis I tried, it doesn’t work , can you see what should I improve? I think I am not appending anything to checkedoutBooks dict. I won’t look into solution before I skin this chicken lol


// The Library Challenge
//
// Instructions:
// Complete the Library 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 {
        
        // TODO: This function searches the catalogue dictionary for a title
        for _ in catalogue {
            
            if (title == "1984" && lib.catalogue["ORW"] != nil) || (title == "Fahrenheit 451" &&  lib.catalogue["RAY"] != nil) {
                return "Available"
            } else if title == "1984" && lib.checkedOutBooks["ORW"] != nil  {
                return "Checked out by \(String(describing: checkedOutBooks["ORW"]))"
            } else if
                title == "Fahrenheit 451" && lib.checkedOutBooks["RAY"] != nil {
                return "Checked out by \(String(describing: checkedOutBooks["RAY"]) )"
            } else {
                return "Not in catalogue"
            }
        }
        // Returns "Available" if the book exists and isn't checked out
        //
        // Returns "Checked out by name" if the book exists and is checked out
        //
        // Returns "Not in catalogue" if the book doesn't exist
        
        return ""
    }
    
    func checkOut(_ bookId:String, _ person:Person) -> String {
        
        // TODO: This function adds to the checkedOutBooks dictionary
        for _ in checkedOutBooks {
        // Returns "Error: Book already checked out" if the book is already in the checkedOutBooks dictionary
        if lib.checkedOutBooks[bookId] != nil {
            return "Error: Book already checked out"
        } else if lib.checkedOutBooks[bookId] == nil {
            lib.checkedOutBooks = [bookId:person]
            return "Successfully checked out the \(bookId) for \(person)"
        } else if
            lib.checkedOutBooks[bookId] == nil && lib.catalogue[bookId] == nil {
            return "Book doesn't exist"
        }
        }
        
        // 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
        lib.checkedOutBooks[bookId] = nil
        // Returns "Book doesn't exist" if the book isn't in the catalogue dictionary
        if lib.catalogue[bookId] == nil {
            return "Book doesn't exist"
        } else if lib.catalogue[bookId] != nil {
            return "Error: Can't check in a book that hasn't been checked out"
        }
        // 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"

ok ok, I think I am solving it in a sec.

remember to create a new function so you can do it there :smile:

what function? are you messing with my mind lol?

Hey so I solved it it works theres just one thing, I can not print out name of person who rent a book when I use the searchByTitle function, it prints an object , cheers

// The Library Challenge
//
// Instructions:
// Complete the Library 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 {
        
        // TODO: This function searches the catalogue dictionary for a title
        for _ in catalogue {
            //                                                    change here conditional of 1984
            if (title == "1984" && lib.catalogue["ORW"] != nil) && (title == "1984" &&  lib.checkedOutBooks["ORW"] == nil) {
                return "Available"
            } else if title == "Fahrenheit 451" && lib.catalogue["RAY"] != nil  && title == "Fahrenheit 451" && lib.checkedOutBooks["RAY"] == nil{
                return "Available"
            } else if
                ( title == "1984" && lib.catalogue["ORW"] != nil) && (title == "1984" && (lib.checkedOutBooks["ORW"] != nil)) {
                return "Checked out by \(lib.checkedOutBooks["ORW"])"
            } else  if
                
                ( title == "Fahrenheit 451" && lib.catalogue["RAY"] != nil) && (title == "Fahrenheit 451") && (lib.checkedOutBooks["RAY"] != nil) {
                return "Checked out by \(lib.checkedOutBooks["RAY"])"
                
            }
        }
        // Returns "Available" if the book exists and isn't checked out
        //
        // Returns "Checked out by name" if the book exists and is checked out
        //
        // Returns "Not in catalogue" if the book doesn't exist
        
        return ""
    }
    
    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
        if lib.checkedOutBooks[bookId] != nil {
            return "Error: Book already checked out"
        } else if lib.checkedOutBooks[bookId] == nil {
            lib.checkedOutBooks = [bookId:person]
            
            return "Successfully checked out the \(bookId) for \(person.name!)"
        } else if
            
            lib.checkedOutBooks[bookId] == nil && lib.catalogue[bookId] == nil {
            return "Book doesn't exist"
        }
        
        
        // 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
        if lib.catalogue[bookId] != nil && lib.checkedOutBooks[bookId] != nil {
            lib.checkedOutBooks[bookId] = nil
            return "Successfuly checked in"
        } else if lib.catalogue[bookId] != nil  && lib.checkedOutBooks[bookId] == nil{
            return "Error: Can't check in a book that hasn't been checked out"
        } else {
            return "that book doesnt exist"
        }
        // 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  - done
let searchResult = lib.searchByTitle("1984")
print(searchResult)

// Expected Output in console: - correct
// "Available"

// George checks out the book - Done
let borrowResult = lib.checkOut("ORW", borrower1)
print(borrowResult)
print(lib.checkedOutBooks)
// Expected Output in console: - correct
// "Successfully checked out"

// Mark searches for a book - correct
let searchResult2 = lib.searchByTitle("1984")
print(searchResult2)

// Expected Output in console:
// "Checked out by Curious George" - correct

// 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" -correct

// 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" - correct

// George checks in his book
let checkInResult2 = lib.checkIn("ORW")
print(checkInResult2)

// Expected Output in console:- correct
// "Successfully checked in"

// Mark attempts to borrow the book again
let borrowResult3 = lib.checkOut("ORW", borrower2)
print(borrowResult3)

// Expected Output in console: - correct
// "Successfully checked out"

Wait a minute why are you using “lib” inside your library class it should be “self” instead…

Also remember that you checkedOutBooks has the value of “Person” class saved… That means you need to use dot notation (.) to access the name… So something as simple as checkedOutBooks[“ORW”].name

1 Like

Thank you, you are a star, learned a lot from that x

1 Like