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

Let's build a simple version with functionalities to

  • add books
  • list books
  • add customers
  • list customers
  • place orders

Define the Bookstore Structure

Create a Scala object to hold your application logic. Create a new file in the src/main/scala directory named BookstoreApp.scala.

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

object BookstoreApp {

  // TODO: case class Book, Customer, Order

  // TODO: Mutable lists of books, customers and orders

  
  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 = {
    // TODO
  }

  def listBooks(): Unit = {
     // TODO
  }

  def addCustomer(): Unit = {
    // TODO
  }

  def listCustomers(): Unit = {
    // TODO
  }

  def placeOrder(): Unit = {
    // TODO
  }

  def listOrders(): Unit = {
   // TODO
}

Structure

  1. Case Classes:
  • Book: Represents a book with id, title, author, and price.
  • Customer: Represents a customer with id, name, and email.
  • Order: Represents an order with id, customerId, and bookId.
  1. Lists:
  • books: A mutable list to hold books.
  • customers: A mutable list to hold customers.
  • orders: A mutable list to hold orders.
  1. Counters:
  • nextBookId: A counter to assign unique IDs to books.
  • nextCustomerId: A counter to assign unique IDs to customers.
  • nextOrderId: A counter to assign unique IDs to orders.
  1. Methods:
  • addBook(): Allows adding a new book.
  • listBooks(): Lists all books.
  • addCustomer(): Allows adding a new customer.
  • listCustomers(): Lists all customers.
  • placeOrder(): Allows placing an order.
  • listOrders(): Lists all orders.