Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using Scala perform the followig: Create a hierarchy of classes to represent ite

ID: 3607740 • Letter: U

Question

Using Scala perform the followig: Create a hierarchy of classes to represent items to be found in a store or a shopping cart. The top of this hierarchy consists of the class StoreItem, which implements the characteristics that are common to both its sub-classes: UnitItem and WeightItem.

For this application, we would like to model the items that are sold by the unit and those that are sold by weight. In particular, the number of units must be represented with an integer, whereas the weight is represented with the type double.

All the items must have a unique serial number, a description and a method that calculates the total price for this item.

In your .scala file for this assignment, you must include the following: class StoreItem, object StoreItem, class UnitItem, class WeightItem, and object Test.

StoreItem

Create an abstract class called StoreItem with the following characteristics.

Fields

a val called serialNumber to store the item's serial number.

a val called description of type String to store the item's description.

Constructor

There will be one constructor (the primary constructor), which will take a parameter called description of type String. In the body of the constructor (i.e., in your class definition) the value of the object's serial number is set to the last object's serial number plus one (+1). Implement this functionality using a companion object.

Methods

an abstract method getPrice(), whose implementation depends on the type of item and will thus be overridden in StoreItem's subclasses.

UnitItem

Create a class, called UnitItem, that inherits the characteristics of its superclass StoreItem and has the following ones as well.

Fields

a val of type int to represent the number of units of this item.

a val of type double that represents the price per unit.

Constructor

The constructor of the class has three arguments: the description, the number of units and the price per unit, which are used to initialize the fields of the class.

Methods

a method getPrice() that calculates the price as the product of the number of units and the unit price. This method will return a double.

WeightItem

Create a class, called WeightItem, that inherits the characteristics of its superclass StoreItem and has the following ones as well.

Fields

a val of type double to represent the weight of this item.

a val of type double to represent the price per unit of weight.

Constructor

The constructor of the class has three arguments: the description, the weight and the price per unit of weight, which are used to initialize the fields of the class.

Methods

a method getPrice() that calculates the price as the product of the number of units and the unit price. This method will return a double.

Explanation / Answer

// Create a class ShoppingCart

class ShoppingCart{

// Create a companion object : an object with the same name as the class

object ShoppingCart{

// declare a serialNum that can be incremented later to supply a unique serial number

var serialNum : Int = 1000

}

// Our base class

abstract class StoreItem (

val description: String) {

// Serial number can be generated by ciopying from companion object

val serialNumber = ShoppingCart.serialNum;

ShoppingCart.serialNum = ShoppingCart.serialNum + 1

  

// abstract method

def getPrice (): Double

}

  

// Subclass of StoreItem

class UnitItem (

override val description: String,

val noOfUnits: Int,

val pricePerUnit: Double

) extends StoreItem(description) {

// price that is product of noOfUnits and pricePerUnit

val price : Double = noOfUnits * pricePerUnit

  

// override getPrice method and return price

override def getPrice():Double = price

  

}

// Another sub class of StoreItem

class WeightItem (

override val description: String,

val weight: Double,

val pricePerUnit: Double

) extends StoreItem(description){

val price : Double = weight * pricePerUnit

// override getPrice method and return price

override def getPrice():Double = price

}

  

}

// Test object

object Test {

// Create outer class instance

val sCart = new ShoppingCart()

// now create inner class instance UnitItem with some example

val unitItem = new sCart.UnitItem("Banana",12,4)

val weightItem = new sCart.WeightItem("Carrot",1.4,5)

  

  

def main() {

print("bananas serialNumber " + unitItem.serialNumber);

print(" price of 12 bananas " + unitItem.getPrice());

print(" carrots serialNumber " + weightItem.serialNumber);

print(" price of 12 carrots " + weightItem.getPrice());

}

}

Test.main()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote