I am relatively new to scala and I was tasked with creating 10 classes for an online stationery website/store. I am unsure if I have used the different types of classes such as: abstract class, case class, traits, and inherited classes correctly. I am using scala 2, JDK 1.8, and scala version 2.12.19. The classes do not need implementation but needs to show the behaviour and characteristics of the classes.
Are these valid classes in scala2 and suitable to be used to describe an online stationery store? Are there better alternatives to the classes I have written down below? These are the classes I have written down below:
//Case class for holding Login data
case class Login(username: String, password: String)
//Class for user authentication when logging in
class Authentication (val user: User){
def authenticate(login: Login) : Boolean = {
user.login.password == login.password && user.login.username == login.username
}
}
//Abstract class used to represent a user of the system
abstract class User(val id: Int, val firstName: String, val lastName: String, val username: String, var email: String,
var phoneNumber: String, var address: String, var login: Login) {
//updateProfile() method to update user profile.
def updateProfile() : Unit
def saveProfile() : Unit
def deleteProfile() : Unit //deleteProfile() method to delete user profile.
//Common implementation to delete profile
}
//Class for Admin
class Admin(_id: Int, _firstName: String, _lastName: String, _username: String, _email: String, _phoneNumber: String, _address: String, _login: Login)
extends User(_id, _firstName, _lastName, _username, _email, _phoneNumber, _address, _login) {
override def updateProfile(): Unit = { //Admin-specific implementation to update profile. Such as allow Admin to update his own profile or update customer's profile
}
override def saveProfile(): Unit = {
}
override def deleteProfile(): Unit = { //Admin-specific implementation to delete profile. Such as administrative action to delete Customer's profile.
}
}
//Class for Customer
class Customer(_id: Int, _firstName: String, _lastName: String, _username: String, _email: String, _phoneNumber: String, _address: String, _login: Login) extends User(_id,
_firstName, _lastName, _username, _email, _phoneNumber, _address, _login) {
//Customer-specific methods.
override def updateProfile(): Unit = {
}
override def saveProfile(): Unit = {
}
override def deleteProfile(): Unit = {
}
}
trait Reusable {
}
//Abstract class used to represent a generic product found in the store
abstract class Product(val id: Int, val name: String, val sellerName: String, var price: Double, val category: String) {
def changePrice(): Unit
def displayProductDetails(): Unit
def discount(): Unit
}
//Class for Book
class Book(_id: Int, _name: String, _sellerName: String, _price: Double, _category: String,
val author: String, val publisher: String) extends Product(_id, _name, _sellerName, _price, _category) {
//Common implementation for Book category
override def displayProductDetails(): Unit = {
//Book specific implementation for displaying different books
}
override def discount(): Unit = {
//Book specific discount depending on what type of book and what type of discount being offered.
}
override def changePrice(): Unit = {
}
}
//Class for Writing Utensil
class Stationery(_id: Int, _name: String, _sellerName: String, _price: Double, _category: String)
extends Product(_id, _name, _sellerName, _price, _category) with Reusable {
//Common implementation for Writing Utensil category (Pen, pencil, etc.)
override def displayProductDetails(): Unit = {
}
override def discount(): Unit = {
}
override def changePrice(): Unit = {
}
}
//Case class for Review data
case class Review(productId: Product, userId: User, rating: Double, comment: String ) {
def postReview(): Unit = {
}
}
I have read through the classes but I am unsure whether I have correctly and accurately defined good classes and used good OOP concepts to make the code “maintainable”.
Wong Jun Teng is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.