Returning Books
Part of the Object Oriented Programming section of Coddy's Swift journey. Lesson 52 of 57.
Challenge
EasyAdd the RETURN command. Keep every command from the previous steps working exactly as before.
RETURN <name> <id>: replyno member boorno book 9as for borrowing; if the member does not hold that book replyada does not have Dune; otherwise remove the loan from the member, make the book available and replyada returned Dune.
The supplied code passes every input line to library.handle(_:) and prints each returned reply on its own line. Member names are single lower-case words, book ids are whole numbers, and the input holds only the commands described so far. Any other command replies unknown command.
Your code goes in Book.swift, Member.swift and Library.swift. main.swift holds the supplied input/output code and cannot be edited.
Try it yourself
// Supplied input/output code: keep it as it is
var input: [String] = []
while let line = readLine() { input.append(line) }
let library = Library()
for line in input {
for reply in library.handle(line) { print(reply) }
}
All lessons in Object Oriented Programming
1Classes and Objects
Working With FilesDefining ClassesMethods and selfInitializersClasses Are ReferencesRecap - Library Card4Encapsulation
Access ControlRead-Only From OutsideGuarding StateFailable InitializersRecap - Bank Account7Polymorphism and Generics
PolymorphismType CastingGeneric FunctionsGeneric TypesRecap - Shape Calculator5Inheritance
SubclassesOverriding MethodsInitializers and superfinal and HierarchiesRecap - Employee Hierarchy11Project: Library Management
Books and MembersBorrowing BooksPractice on your own: Swift playground