Packages

c

o1.soda

VendingMachine

class VendingMachine extends AnyRef

The class VendingMachine can be used to simulate simple virtual soda vending machines. A vending machine sells bottles of a single type of soft drink at a certain price per bottle.

The methods of the class correspond to the actions of buyers and maintenance personnel on an actual vending machine. For instance, a purchase is made by first inserting some money possibly several times with each one adding to the previous insertion, and then selecting to buy a bottle. This interaction is captured by the methods insertMoney and buyBottle.

Some of the methods are illustrated in the following REPL session, in which the vending machine initially contains 10 bottles with a price of 250 cents (2.5 euros) each:

 INPUT: val machine = new VendingMachine(250, 10)
OUTPUT: machine: o1.soda.VendingMachine = earned 0.0 euros, inserted 0 cents, 10 bottles left

 INPUT: machine.insertMoney(300)

 INPUT: "got change: " + machine.sellBottle()
OUTPUT: res0: String = got change: 50

 INPUT: machine
OUTPUT: res1: o1.soda.VendingMachine = earned 2.5 euros, inserted 0 cents, 9 bottles left

 INPUT: machine.insertMoney(50)

 INPUT: machine.insertMoney(300)

 INPUT: machine.sellBottle()
OUTPUT: res2: Int = 100

 INPUT: machine
OUTPUT: res3: o1.soda.VendingMachine = earned 5.0 euros, inserted 0 cents, 8 bottles left

As the example illustrates, a vending machine object has a mutable state: it keeps track of how much money the user has inserted, for instance. The machine also tracks the number of bottles it contains, the current (modifiable) price per bottle, and the money it has "earned" by selling bottles.

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. VendingMachine
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Instance Constructors

  1. new VendingMachine(bottlePrice: Int, bottleCount: Int)

    bottlePrice

    the price of a single bottle

    bottleCount

    the initial number of bottles in the machine

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. def addBottles(newBottles: Int): Unit

    (A maintenance action:) Adds a number of bottles to the machine.

    (A maintenance action:) Adds a number of bottles to the machine. The virtual vending machine has no upper limit for bottles.

    newBottles

    the number of bottles to be added (a positive integer)

  5. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  6. var bottlePrice: Int
  7. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native() @HotSpotIntrinsicCandidate()
  8. def emptyCashbox(): Int

    (A maintenance action:) Removes all the earned cash from the machine.

    (A maintenance action:) Removes all the earned cash from the machine.

    returns

    the amount of money previously earned by the machine through sales

  9. def enoughMoneyInserted: Boolean

    Determines whether enough money has been inserted to purchase a bottle.

  10. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  11. def equals(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef → Any
  12. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  13. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  14. def insertMoney(amount: Int): Unit

    (A customer action:) Adds some money towards a forthcoming purchase.

  15. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  16. def isSoldOut: Boolean

    Determines whether the machine is out of bottles.

  17. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  18. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  19. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  20. def sellBottle(): Int

    (A customer action:) Sells a bottle of virtual soda.

    (A customer action:) Sells a bottle of virtual soda. This can only be done, however, if enough money has been inserted and there are bottles left in the machine. If the sale was successful, the method adds the earnings in the machine's cashbox, removes a single bottle, and sets up for the next transaction by resetting the amount of inserted cash to zero.

    returns

    the amount of money (0 or more) given to the buyer as change, or a negative value to signal an unsuccessful purchase

  21. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  22. def toString: String

    Produces a textual description of the vending machine's current state.

    Produces a textual description of the vending machine's current state.

    Definition Classes
    VendingMachine → AnyRef → Any
  23. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  24. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()
  25. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])

Deprecated Value Members

  1. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable]) @Deprecated @deprecated
    Deprecated

    (Since version ) see corresponding Javadoc for more information.

Inherited from AnyRef

Inherited from Any

Ungrouped