Skip to content

Update library-challenge.kt #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 56 additions & 52 deletions udemy-course/src/challenges/library-challenge.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
println(dvd)
}