Packages

p

o1

soda

package soda

Type Members

  1. class VendingMachine extends AnyRef

    The class VendingMachine can be used to simulate simple virtual soda vending machines.

    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.

Ungrouped