// Define a trait for common characteristics of entities in the shop trait Identifiable…
Private research annotation
Sign in to add a private research label or note.
- User
// Define a trait for common characteristics of entities in the shop trait Identifiable { val id: Int }
trait Nameable { val name: String }
// Define a trait for behavior of entities that can be displayed trait Displayable { def display(): Unit }
// Define a trait for inventory management trait InventoryManagement { def checkStock(): Unit def updateStock(amount: Int): Unit }
// Product class with characteristics and behaviors case class Product(id: Int, name: String, price: Double, stock: Int) extends Identifiable with Nameable with InventoryManagement { override def checkStock(): Unit = ??? override def updateStock(amount: Int): Unit = ??? }
// User class with characteristics case class User(id: Int, name: String, email: String) extends Identifiable with Nameable with UserOperations { override def login(): Unit = ??? override def logout(): Unit = ??? }
// Define a trait for operations that can be performed by users trait UserOperations { def login(): Unit def logout(): Unit }
// Order class with characteristics and behaviors case class Order(id: Int, userId: Int, productIds: List[Int], totalAmount: Double) extends Identifiable { def calculateTotal(): Unit = ??? }
// Define a trait for managing orders trait OrderManagement { def placeOrder(order: Order): Unit def cancelOrder(orderId: Int): Unit def updateOrder(order: Order): Unit def viewOrder(orderId: Int): Order }
// ShoppingCart class to hold products case class ShoppingCart(products: List[Product]) { def addProduct(product: Product): Unit = ??? def removeProduct(productId: Int): Unit = ??? }
// Singleton object for the shop object StationeryShop extends Displayable with OrderManagement { var products: List[Product] = List() var users: List[User] = List() var orders: List[Order] = List()
override def display(): Unit = ??? override def placeOrder(order: Order): Unit = ??? override def cancelOrder(orderId: Int): Unit = ??? }
// Companion objects for each class can serve as factories and contain static methods object Product { def apply(id: Int, name: String, price: Double, stock: Int): Product = new Product(id, name, price, stock) }
object User { def apply(id: Int, name: String, email: String): User = new User(id, name, email) }
object Order { def apply(id: Int, userId: Int, productIds: List[Int], totalAmount: Double): Order = new Order(id, userId, productIds, totalAmount) }
- Assistant