VendingMachine

o1.soda.VendingMachine
class VendingMachine(var bottlePrice: Int, var bottleCount: Int)

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:

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

scala> machine.insertMoney(300)

scala> "got change: " + machine.sellBottle()
val res0: String = got change: 50

scala> machine
val res1: o1.soda.VendingMachine = earned 2.5 euros, inserted 0 cents, 9 bottles left

scala> machine.insertMoney(50)

scala> machine.insertMoney(300)

scala> machine.sellBottle()
val res2: Int = 100

scala> machine
val 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.

Parameters

bottleCount

the initial number of bottles in the machine

bottlePrice

the price of a single bottle

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def addBottles(newBottles: Int): Unit

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

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

Parameters

newBottles

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

Attributes

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.

Attributes

Returns

the amount of money previously earned by the machine through sales

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

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

Attributes

def insertMoney(amount: Int): Unit

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

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

Attributes

Determines whether the machine is out of bottles.

Determines whether the machine is out of bottles.

Attributes

def sellBottle(): Int

(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.

(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.

Attributes

Returns

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

override 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.

Attributes

Definition Classes
Any

Concrete fields