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
- Case Classes:
Book: Represents a book withid,title,author, andprice.Customer: Represents a customer withid,name, andemail.Order: Represents an order withid,customerId, andbookId.
- Lists:
books: A mutable list to hold books.customers: A mutable list to hold customers.orders: A mutable list to hold orders.
- 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.
- 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.