Borrowing Books
Part of the Object Oriented Programming section of Coddy's Kotlin journey. Lesson 51 of 57.
Challenge
EasyAdd the BORROW command. Your code from the previous step is already in Library.kt; keep every command working exactly as before.
BORROW <name> <id>: check in this order and reply with the first problem:no member bo,no book 9,Dune is not available,ada has reached the limit(members may hold at most 2 books). Otherwise record the loan on both the book and the member and replyada borrowed Dune.
The supplied code passes every input line to library.handle(line) 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.kt, Member.kt and Library.kt. Main.kt holds the supplied input/output code and cannot be edited.
Try it yourself
fun main() {
// Supplied input/output code: keep it as it is
val input = generateSequence(::readLine).toList()
val library = Library()
for (line in input) {
for (reply in library.handle(line)) println(reply)
}
}
All lessons in Object Oriented Programming
1Classes and Objects
Working With FilesDefining ClassesPrimary Constructorsinit Blocks and ValidationSecondary ConstructorsRecap - Library Card4Inheritance
Open Classes and SubclassesOverriding Memberssuper and ConstructorsAbstract ClassesRecap - Staff Directory2Properties and Encapsulation
Custom AccessorsVisibility ModifiersPrivate Setterslazy and lateinitRecap - Bank Account5Interfaces
Declaring InterfacesDefault MethodsMultiple InterfacesInterfaces as TypesRecap - Payment Methods8Generics
Generic FunctionsGeneric ClassesType ConstraintsVariance With in and outRecap - Priority Queue11Project: Library Management
Books and MembersBorrowing BooksPractice on your own: Kotlin playground