Advertisement
Guest User

Untitled

a guest
Mar 12th, 2014
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.28 KB | None | 0 0
  1. object Birthday extends App {
  2.  
  3.   val recipe = List("Flour", "Sugar", "Baking Powder", "Eggs", "Vanilla")
  4.   val dryStuff = recipe.slice(0, 3)
  5.   val wetStuff = recipe.slice(3, 5)
  6.  
  7.   class Cake() {
  8.  
  9.     var mixingBowl = ""
  10.  
  11.     def mix(stuffToMix: List[String]) = {
  12.       val mixTwo = (one: String, two: String) => (one + " and " + two)
  13.       if (!mixingBowl.isEmpty()) mixingBowl += " and "
  14.       mixingBowl += "(" + stuffToMix.reduceLeft(mixTwo) + ")"
  15.       println("Mixing bowl contains: " + mixingBowl)
  16.     }
  17.  
  18.     def bake() {
  19.       var ingredientsPresent = true
  20.       for (ing <- recipe) {
  21.         if (!mixingBowl.contains(ing)) {
  22.           ingredientsPresent = false
  23.         }
  24.       }
  25.       if (ingredientsPresent) {
  26.         println("Ingredients correct!")
  27.         Thread.sleep(500)
  28.         print("Baking")
  29.         for (i <- (0 to 4)) {
  30.         Thread.sleep(900)
  31.         print(".")
  32.         }
  33.         println("Cake complete! Happy Birthday!")
  34.        
  35.       } else {
  36.         println("Required ingredients not detected")
  37.       }
  38.     }
  39.   }
  40.  
  41.   val bDayCake = new Cake
  42.   println("Recipe: ")
  43.   recipe.foreach(str => println("\t" + str))
  44.   Thread.sleep(1000)
  45.   bDayCake.mix(dryStuff)
  46.   Thread.sleep(1000)
  47.   bDayCake.mix(wetStuff)
  48.   Thread.sleep(1000)
  49.   bDayCake.bake()
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement