Indus is an e-store, like Amazon. An item in the Indus
inventory, like a DVD, has a unique identity number and an associated product
description that contains a brief description of the product, such as the title
of the DVD, the price (in pennies), and the name of the supplier where more
items can be ordered::
Implement the Item and Description classes in Scala. Provide
companion objects with apply methods for each class.
Users should not be trusted to provide unique id numbers for
each item. Instead, these should be automatically generated. In Java we might
create a private static nextId variable that stores
the next available id number. The Item constructor could use this to initialize
id, then increment the variable:
this.id = Item.nextId++;
Scala doesn't have static variables. Instead, nextId can be declared in the Item companion object.
As in Java, every Scala class inherits useful methods from a
cosmic superclass (called AnyRef, more on this
later). For example, the inherited toString is
automatically called when an object appears in a context where a string is
expected. This method can be overridden in the Item and Description classes to
create more attractive strings. Warning: you must declare that you are
overriding a method:
override def ...
Create an application object called Indus containing an
inventory field of type ArrayBuffer[Item]. Stock the inventory with the following items:
Description |
Price |
Supplier |
# in stock |
The
Matrix DVD |
$15.50 |
DVD
World |
5 |
The
Terminator DVD |
$13.25 |
DVD
World |
3 |
Ironman
DVD |
$18.00 |
DVD
Planet |
2 |
Print the inventory.