Classes

Example: Inventory

A digital inventory system maintains a record of each item in stock.

An item has a description and cost. The cost has two parts: dollars (an integer) and cents (integer).

Description is read-only, but the cost should be read/write.

Negative dollar and cents amounts are not allowed. Cents should be less than 100.

Solution 1

package demos

class Item(val description: String, var dollars: Int, var cents: Int):
  if (dollars < 0) throw Exception("Invalid dollar amount")
  if (cents < 0 || 99 < cents) throw Exception("Invalid cents amount")
  override def toString =
    "Item: " + description + " cost: $" + dollars + "." + (if (cents < 10) "0" + cents else cents)

object Item extends App:
  def test1() =
    try
      val item1 = Item("brush", 8, 100)
    catch
      case e: Exception => println(e.getMessage)
    val item1 = Item("brush", 8, 99)
    // getters:
    println(item1.description)
    println(item1.dollars)
    println(item1.cents)
    println(item1.toString)

    item1.dollars = 9
    item1.cents = 0
    println(item1.toString)

    try
      item1.dollars = -1
      item1.cents = 2000
      println(item1.toString)
    catch
      case e: Exception => println(e.getMessage)

  test1()

/*
Invalid cents amount
brush
8
99
Item: brush cost: $8.99
Item: brush cost: $9.00
Item: brush cost: $-1.2000
*/

Java Translation

public class Item {
    private String _description;
    private Integer _dollars;
    private Integer _cents;
    public Item(String _description, Integer _dollars, Integer _cents) {
        if (_dollars < 0) throw new Exception("Invalid dollar amount")
        if (_cents < 0 || 99 < _cents) throw new Exception("Invalid cents amount")
        this._description = _description;
        this._dollars = _dollars;
        this._cents = _cents;
    }
    // auto-generated getters
    public String description() { return _description; }
    public Integer dollars() { return _dollars; }
    public Integer cents() { return _cents; }
    // auto-generated setters
    public void dollars_=(Integer amt) { _dollars = amt; }
    public void cents_=(Integer amt) { _cents = amt; }
    // etc.
}

Solution 2

class Item(val description: String, private var _dollars: Int, private var _cents: Int):
  if (_dollars < 0) throw Exception("Invalid dollar amount")
  if (_cents < 0 || 99 < _cents) throw Exception("Invalid cents amount")
  def dollars = _dollars
  def cents = _cents
  def dollars_=(amt: Int) =
    if (amt < 0) throw Exception("Invalid dollar amount")
    _dollars = amt
  def cents_=(amt: Int) =
    if (amt < 0 || 99 < amt) throw Exception("Invalid cents amount")
    _cents = amt
  override def toString =
    "Item: " + description + " cost: $" + dollars + "." + (if (cents < 10) "0" + cents else cents)

object Item extends App:
  def test1() =
    try
      val item1 = Item("brush", 8, 100)
    catch
      case e: Exception => println(e.getMessage)
    val item1 = Item("brush", 8, 99)
    // getters:
    println(item1.description)
    println(item1.dollars)
    println(item1.cents)
    println(item1.toString)

    item1.dollars = 9
    item1.cents = 0
    println(item1.toString)

    try
      item1.dollars = -1
      item1.cents = 2000
      println(item1.toString)
    catch
      case e: Exception => println(e.getMessage)

  test1()
 
 /*
 Invalid cents amount
 brush
 8
 99
 Item: brush cost: $8.99
 Item: brush cost: $9.00
Invalid dollar amount
*/