blob: 8d5aea571a502a8b014fd87ccb3146061595ba12 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
// SPDX-License-Identifier: Apache-2.0
package examples
import scala.collection.mutable
/* Useful utilities for testing vending machines */
object VendingMachineUtils {
abstract class Coin(val name: String, val value: Int)
// US Coins
case object Penny extends Coin("penny", 1)
case object Nickel extends Coin("nickel", 5)
case object Dime extends Coin("dime", 10)
case object Quarter extends Coin("quarter", 25)
// Harry Potter Coins
case object Knut extends Coin("knut", Penny.value * 2) // Assuming 1 Knut is worth $0.02
case object Sickle extends Coin("sickle", Knut.value * 29)
case object Galleon extends Coin("galleon", Sickle.value * 17)
def getExpectedResults(inputs: Seq[Option[Coin]], sodaCost: Int): Seq[Boolean] = {
var value = 0
val outputs = mutable.ArrayBuffer.empty[Boolean]
for (input <- inputs) {
val incValue = input match {
case Some(coin) => coin.value
case None => 0
}
if (value >= sodaCost) {
outputs.append(true)
value = 0
} else {
outputs.append(false)
value += incValue
}
}
outputs.toSeq
}
}
|