private fun updateBook() { println("Pick a book by its number to update:") viewBooks(false) println("\nSelection: ") val bookSelection = try { books[readLine()!!.toInt() - 1] } catch (e: Exception) { println("Invalid selection! Aborting..") return } println("\nSelected:\n$bookSelection.\n\nChoose a variable to update:") printBookVariables() println("\nSelection: ") val enumSelection = try { readLine()!!.toInt() } catch (e: Exception) { println("Invalid selection! Aborting..") return } val selected = BookVariables.values().associateBy(BookVariables::id)[enumSelection] println("\nSelected modifier:\n$selected\n\nType a new ${selected.toString().toLowerCase()}:") try { val newValue = readLine()!! if (newValue.isBlank()) { println("Modification can't be empty! Aborting..") return } when (enumSelection) { BookVariables.TITLE.id -> bookSelection.title = newValue BookVariables.AUTHOR.id -> bookSelection.author = newValue BookVariables.PUBLICATION_YEAR.id -> { if (newValue.toInt() <= 1600) { println("Publication year is too old! Aborting..") return } bookSelection.publicationYear = newValue.toInt() } BookVariables.NUMBER_OF_PAGES.id -> { if (newValue.toInt() <= 0) { println("There must be at least one page! Aborting..") return } bookSelection.numberOfPages = newValue.toInt() } BookVariables.ISBN_NUMBER.id -> bookSelection.isbn = newValue.toLong() } } catch (e: NumberFormatException) { println("Invalid selection! Aborting..") return } println("\nUpdated book info:\n$bookSelection") } private fun deleteBook() { println("Pick a book by its number to delete:") viewBooks(false) println("\nSelection: ") val bookSelection = try { books[readLine()!!.toInt() - 1] } catch (e: Exception) { println("Invalid selection! Aborting..") return } books.remove(bookSelection) println("Book deleted!") } private fun viewBookWithMostPages() { val find = books.maxByOrNull { it.numberOfPages } if (find == null) { println("No books found! Aborting..") return } println("Book with most pages:\n${find.title} (Pages: ${find.numberOfPages})") } private fun viewBookWithLeastPages() { val find = books.minByOrNull { it.numberOfPages } if (find == null) { println("No books found! Aborting..") return } println("Book with least pages:\n${find.title} (Pages: ${find.numberOfPages})") } private fun viewBooksPagesAboveOrEqual(number: Int) { println("Books with pages above or equal to $number:") val find = books.filter { it.numberOfPages >= number } viewBooks(false, find) } private fun viewBooksPagesBelow(number: Int) { println("Books with pages below $number:") val find = books.filter { it.numberOfPages < number } viewBooks(false, find) } private fun viewBooksPagesBetweenInclusive(min: Int, max: Int) { println("Books with pages between $min and $max:") val find = books.filter { it.numberOfPages in min..max } viewBooks(false, find) } // fun viewBooksWithPagesInRange(books: MutableList<Book>, range: IntRange = 0..Int.MAX_VALUE) { // val tempBooksList = mutableListOf<Book>() // println("Books with pages between ${range.first} and ${range.last}: ") // for (book in books) { // if(book.numberOfPages in range) { // tempBooksList.add(book) // } // } // printBooks(tempBooksList, false) // } } class Book( var title: String, var author: String, var publicationYear: Int, var numberOfPages: Int, var isbn: Long ) { override fun toString(): String { return "$title, $author, $publicationYear, $numberOfPages, https://biblio.com/$isbn" } fun toTab(): String { return "$title\t$author\t$publicationYear\t$numberOfPages\t$isbn" } }