Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Project: Bookstore

import scala.io.StdIn.readLine
import scala.collection.mutable.ListBuffer

object BookstoreApp {

  case class Book(id: Int, title: String, author: String, price: Double)
  case class Customer(id: Int, name: String, email: String)
  case class Order(id: Int, customerId: Int, bookId: Int)

  val books: ListBuffer[Book] = ListBuffer.empty
  val customers: ListBuffer[Customer] = ListBuffer.empty
  val orders: ListBuffer[Order] = ListBuffer.empty

  var nextBookId: Int = 1
  var nextCustomerId: Int = 1
  var nextOrderId: Int = 1

  def main(args: Array[String]): Unit = {
    var continue = true

    while (continue) {
      println("\nBookstore App")
      println("1. Add Book")
      println("2. List Books")
      println("3. Add Customer")
      println("4. List Customers")
      println("5. Place Order")
      println("6. List Orders")
      println("7. Exit")
      print("Choose an option: ")

      readLine() match {
        case "1" => addBook()
        case "2" => listBooks()
        case "3" => addCustomer()
        case "4" => listCustomers()
        case "5" => placeOrder()
        case "6" => listOrders()
        case "7" => continue = false
        case _ => println("Invalid option. Please try again.")
      }
    }
  }

  def addBook(): Unit = {
    print("Enter book title: ")
    val title = readLine()
    print("Enter book author: ")
    val author = readLine()
    print("Enter book price: ")
    val price = readLine().toDouble

    books += Book(nextBookId, title, author, price)
    println(s"Book added with id $nextBookId: $title by $author at $$${price}")
    nextBookId += 1
  }

  def listBooks(): Unit = {
    if (books.isEmpty) {
      println("No books available.")
    } else {
      println("Books:")
      books.foreach(book => println(s"${book.id}. ${book.title} by ${book.author} - $$${book.price}"))
    }
  }

  def addCustomer(): Unit = {
    print("Enter customer name: ")
    val name = readLine()
    print("Enter customer email: ")
    val email = readLine()

    customers += Customer(nextCustomerId, name, email)
    println(s"Customer added with id $nextCustomerId: $name, $email")
    nextCustomerId += 1
  }

  def listCustomers(): Unit = {
    if (customers.isEmpty) {
      println("No customers available.")
    } else {
      println("Customers:")
      customers.foreach(customer => println(s"${customer.id}. ${customer.name} (${customer.email})"))
    }
  }

  def placeOrder(): Unit = {
    print("Enter customer id: ")
    val customerId = readLine().toInt
    print("Enter book id: ")
    val bookId = readLine().toInt

    if (customers.exists(_.id == customerId) && books.exists(_.id == bookId)) {
      orders += Order(nextOrderId, customerId, bookId)
      println(s"Order placed with id $nextOrderId: Customer $customerId ordered Book $bookId")
      nextOrderId += 1
    } else {
      println("Invalid customer id or book id.")
    }
  }

  def listOrders(): Unit = {
    if (orders.isEmpty) {
      println("No orders placed.")
    } else {
      println("Orders:")
      orders.foreach(order => {
        val customer = customers.find(_.id == order.customerId).map(_.name).getOrElse("Unknown Customer")
        val book = books.find(_.id == order.bookId).map(_.title).getOrElse("Unknown Book")
        println(s"Order ${order.id}: Customer ${order.customerId} (${customer}) ordered Book ${order.bookId} (${book})")
      })
    }
  }
}