roman_gemini

Kitchen

Jun 18th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.92 KB | None | 0 0
  1. package test
  2.  
  3. import scala.collection.mutable.ListBuffer
  4.  
  5.  
  6. class Product(name: String, calories: Int) {
  7.   def getName: String = name
  8.  
  9.   def getCalories: Int = calories
  10.  
  11.   override def toString: String = "[" + getName + ", " + getCalories + " calories]"
  12. }
  13.  
  14. class Shelf(size: Int) {
  15.   private val list: ListBuffer[Product] = ListBuffer[Product]()
  16.  
  17.   def add(product: Product): Unit = list.append(product)
  18.   def free: Int = size - list.map(_.getCalories).sum
  19.   def toList: List[Product] = list.toList
  20. }
  21.  
  22. class Kitchen(size: Int) {
  23.  
  24.   private var list: ListBuffer[Shelf] = ListBuffer[Shelf]()
  25.  
  26.   def add(product: Product): Unit = {
  27.     val shelf: Option[Shelf] = list.find(_.free >= product.getCalories)
  28.     if (shelf.nonEmpty) {
  29.       shelf.get.add(product)
  30.     } else {
  31.       val new_shelf = new Shelf(size)
  32.       new_shelf.add(product)
  33.       list.append(new_shelf)
  34.     }
  35.     list = list.sortBy(_.free)
  36.   }
  37.  
  38.   def addAll(product: Product*): Unit = product.foreach(this.add)
  39.  
  40.   def fetch: List[Product] = {
  41.     if (list.size == 0) {
  42.       throw new RuntimeException("No food. Sorry...")
  43.     }
  44.     list.remove(0).toList
  45.   }
  46.  
  47.   def empty(): Boolean = list.length == 0
  48.  
  49.   override def toString: String = "Food on a kitchen: " + list.flatMap(_.toList).mkString(", ")
  50. }
  51.  
  52. object Main extends App {
  53.  
  54.   val threshold = 100
  55.   val kitchen = new Kitchen(threshold)
  56.  
  57.   kitchen.addAll(
  58.     new Product("beer", 40), new Product("tea", 34),
  59.     new Product("Beer", 31), new Product("Chocolate", 45),
  60.     new Product("Wine", 11), new Product("Pizza", 55),
  61.     new Product("Sushi", 54), new Product("Tea", 10),
  62.     new Product("Cake", 35), new Product("Ice", 63),
  63.     new Product("Orange", 5), new Product("Snickers", 80),
  64.     new Product("Meat", 89), new Product("Apple", 42),
  65.     new Product("Orange", 5), new Product("Snickers", 80),
  66.     new Product("Meat", 89), new Product("Apple", 42),
  67.     new Product("Cherry", 38), new Product("Cheese", 29),
  68.     new Product("Wine", 11), new Product("Pizza", 55),
  69.     new Product("Sushi", 54), new Product("Tea", 10),
  70.     new Product("Cake", 35), new Product("Ice", 63),
  71.     new Product("Orange", 5), new Product("Snickers", 80),
  72.     new Product("Meat", 89), new Product("Apple", 42),
  73.     new Product("Orange", 5), new Product("Snickers", 80),
  74.     new Product("Meat", 89), new Product("Apple", 42),
  75.     new Product("Orange", 5), new Product("Snickers", 80),
  76.     new Product("Meat", 89), new Product("Apple", 42),
  77.     new Product("Cherry", 38), new Product("Cheese", 29),
  78.     new Product("Water", 1), new Product("Bread", 42)
  79.   )
  80.  
  81.   while (true) {
  82.     if (kitchen.empty()) {
  83.       println("No more food at the kitchen. Bye!")
  84.       System.exit(0)
  85.     }
  86.     println(kitchen)
  87.     val yn = readLine("Do you want to eat? (y/n)")
  88.     println()
  89.     if (yn eq "n") {
  90.       println("Bye!")
  91.       System.exit(0)
  92.     }
  93.  
  94.     val products = kitchen.fetch
  95.  
  96.     println("You have eaten: " + products)
  97.     println("Total calories: " + products.map(_.getCalories).sum)
  98.     println()
  99.  
  100.   }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment