diff --git a/udemy-course/src/challenges/library-challenge.kt b/udemy-course/src/challenges/library-challenge.kt index 3bef754..5c23c6d 100644 --- a/udemy-course/src/challenges/library-challenge.kt +++ b/udemy-course/src/challenges/library-challenge.kt @@ -1,74 +1,78 @@ package challenges -/** - * @author Peter Sommerhoff - * - * Solution for the coding challenge on object orientation inside the Udemy course. - * Defines some simple interfaces and classes for a library inventory system. - */ -interface Lendable { +interface Lendable +{ fun borrow() + fun returnItem() } -// The properties title, genre, and publicationYear can be included in the parent class because both books and DVDs -// have these properties. The author and length properties however are included only in the child classes. -abstract class InventoryItem(val title: String, - val genre: String, - var publicationYear: Int, - var borrowed: Boolean) : Lendable { +interface Viewable +{ + fun view() +} + +abstract class InventoryItem( + val title: String, + val genre: String, + val publicationYear: Int +): Lendable, Viewable +{ + private var borrowed = false + private var returned = true override fun borrow() { - if (!borrowed) { - borrowed = true - } else { - println("This item is already borrowed.") - } + this.borrowed = true } - // If you want, you can also add a returnItem() method so that you can also un-borrow items. - + override fun returnItem() { + this.returned = true + } override fun toString(): String { - return "InventoryItem(title='$title', genre='$genre', publicationYear=$publicationYear, borrowed=$borrowed)" + return "InventoryItem(title=$title, genre=$genre, year=$publicationYear, borrowed=$borrowed" } - - abstract fun copy(): InventoryItem - } -// A book will be not borrowed by default. -// The class is called LibraryBook to prevent name clashes with previous challenges. -class LibraryBook(title: String, - val author: String, - genre: String, - publicationYear: Int) : InventoryItem(title, genre, publicationYear, false) { - - fun read() { - println("Reading a book by $author...") +class Book( + title: String, + genre: String, + publicationYear: Int, + private val author: String +): InventoryItem(title, genre, publicationYear) +{ + override fun view() { + println("Reading $title ($genre) by $author published $publicationYear ...") } +} - override fun copy(): InventoryItem { - val copy = LibraryBook(title, author, genre, publicationYear) - copy.borrowed = this.borrowed - - return copy +class Dvd( + title: String, + genre: String, + publicationYear: Int, + private val lengthInMinutes: Int +): InventoryItem(title, genre, publicationYear) +{ + override fun view() { + println("Watching $title ($genre). Length $lengthInMinutes min. Released $publicationYear") } } -// A DVD will also be not borrowed by default. -class LibraryDVD(title: String, - genre: String, - val length: Int, - publicationYear: Int) : InventoryItem(title, genre, publicationYear, false) { +fun main() { + val book = Book( + "A Book Title", + "A Fun Genre", + 2000, + "An Author Name" + ) - fun watch() { - println("Watching $title...") - } + println(book) - override fun copy(): InventoryItem { - val copy = LibraryDVD(title, genre, length, publicationYear) - copy.borrowed = this.borrowed + val dvd = Dvd( + "A DVD Title", + "A Fun Genre", + 2000, + 180 + ) - return copy - } -} \ No newline at end of file + println(dvd) +}