Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test
- import scala.collection.mutable.ListBuffer
- class Product(name: String, calories: Int) {
- def getName: String = name
- def getCalories: Int = calories
- override def toString: String = "[" + getName + ", " + getCalories + " calories]"
- }
- class Shelf(size: Int) {
- private val list: ListBuffer[Product] = ListBuffer[Product]()
- def add(product: Product): Unit = list.append(product)
- def free: Int = size - list.map(_.getCalories).sum
- def toList: List[Product] = list.toList
- }
- class Kitchen(size: Int) {
- private var list: ListBuffer[Shelf] = ListBuffer[Shelf]()
- def add(product: Product): Unit = {
- val shelf: Option[Shelf] = list.find(_.free >= product.getCalories)
- if (shelf.nonEmpty) {
- shelf.get.add(product)
- } else {
- val new_shelf = new Shelf(size)
- new_shelf.add(product)
- list.append(new_shelf)
- }
- list = list.sortBy(_.free)
- }
- def addAll(product: Product*): Unit = product.foreach(this.add)
- def fetch: List[Product] = {
- if (list.size == 0) {
- throw new RuntimeException("No food. Sorry...")
- }
- list.remove(0).toList
- }
- def empty(): Boolean = list.length == 0
- override def toString: String = "Food on a kitchen: " + list.flatMap(_.toList).mkString(", ")
- }
- object Main extends App {
- val threshold = 100
- val kitchen = new Kitchen(threshold)
- kitchen.addAll(
- new Product("beer", 40), new Product("tea", 34),
- new Product("Beer", 31), new Product("Chocolate", 45),
- new Product("Wine", 11), new Product("Pizza", 55),
- new Product("Sushi", 54), new Product("Tea", 10),
- new Product("Cake", 35), new Product("Ice", 63),
- new Product("Orange", 5), new Product("Snickers", 80),
- new Product("Meat", 89), new Product("Apple", 42),
- new Product("Orange", 5), new Product("Snickers", 80),
- new Product("Meat", 89), new Product("Apple", 42),
- new Product("Cherry", 38), new Product("Cheese", 29),
- new Product("Wine", 11), new Product("Pizza", 55),
- new Product("Sushi", 54), new Product("Tea", 10),
- new Product("Cake", 35), new Product("Ice", 63),
- new Product("Orange", 5), new Product("Snickers", 80),
- new Product("Meat", 89), new Product("Apple", 42),
- new Product("Orange", 5), new Product("Snickers", 80),
- new Product("Meat", 89), new Product("Apple", 42),
- new Product("Orange", 5), new Product("Snickers", 80),
- new Product("Meat", 89), new Product("Apple", 42),
- new Product("Cherry", 38), new Product("Cheese", 29),
- new Product("Water", 1), new Product("Bread", 42)
- )
- while (true) {
- if (kitchen.empty()) {
- println("No more food at the kitchen. Bye!")
- System.exit(0)
- }
- println(kitchen)
- val yn = readLine("Do you want to eat? (y/n)")
- println()
- if (yn eq "n") {
- println("Bye!")
- System.exit(0)
- }
- val products = kitchen.fetch
- println("You have eaten: " + products)
- println("Total calories: " + products.map(_.getCalories).sum)
- println()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment