Skip to content

Instantly share code, notes, and snippets.

@oc
Created October 4, 2009 14:09
Show Gist options
  • Save oc/201411 to your computer and use it in GitHub Desktop.
Save oc/201411 to your computer and use it in GitHub Desktop.
#!/bin/sh
exec scala "$0" "$@"
!#
import collection.mutable.ListBuffer
val random = new Random()
def sumPizzas(list:ListBuffer[Pizza]):Int = { list.foldLeft(0)(_ + _.totalPrice) }
object Orderer {
val maximumPrice = 1200
val hungerFactor = 0.4
val sourcreamPrice = 20
def apply(numberOfPeople:Int) {
val numberOfPizzas = (numberOfPeople * hungerFactor).round.toInt
val pizzas = pick(numberOfPizzas, maximumPrice)
println(String.format("%-60s %5s", "Antall pizzaer", pizzas.length.toString))
println("-" * 66)
println(pizzas.mkString("\n"))
println("-" * 66)
println(String.format("%-60s %5s", "TOTALSUM", sumPizzas(pizzas).toString))
println("=" * 66)
}
def pick(antall: Int, maximumPrice: Int) = {
val picks = new ListBuffer[Pizza]
for (i <- 0 to antall) {
val pizza = Menu.randomPizza
if (sumPizzas(picks) + pizza.price < maximumPrice) picks += pizza
}
picks
}
}
class Sourcream {
def price() = 20
override def toString() = " m/rømmedressing"
}
object Menu {
val menu = Pizza("Pappas pizza", 159) * 4 ++
Pizza("Texas", 149) * 3 ++
Pizza("Blue Hawaii", 149) * 7 ++
Pizza("Florida", 149) * 4 ++
Pizza("Buffalo", 149) * 4 ++
Pizza("Chicken", 149) * 4 ++
Pizza("New York", 149) * 0 ++
Pizza("Las Vegas", 149) * 6 ++
Pizza("Vegetarianer", 149) * 0 ++
Pizza("Philadelphia", 149) * 4 ++
Pizza("Hot Chicago", 149) * 7 ++
Pizza("Hot express", 149) * 5 ++
Pizza("Kebabpizza spesial", 169) * 3 ++
Pizza("Pepperoni, biff, skinke, bacon, løk", 159, false) * 9 ++
Pizza("Pepperoni, biff, skinke, bacon, tacokjøtt", 159, false) * 9 toArray
def randomPizza() = menu(random.nextInt(menu.length))
def pick(antall: Int, maximumPrice: Int) = {
val picks = new ListBuffer[Pizza]
for (i <- 0 to antall) {
val rnd = randomPizza
if (sumPizzas(picks) + rnd.price < maximumPrice) picks += rnd
}
picks
}
}
import java.util.Calendar
import Calendar.{getInstance => today}
object Pizza {
def apply(name:String, p:Int):Pizza = Pizza(name, p, true)
}
case class Pizza(name:String, price:Int, wp:Boolean) {
val wednesdayMadnessPrice = 119
def totalPrice = sourcream.map(_.price).getOrElse(0) +
(if(today.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY && wp) wednesdayMadnessPrice else price)
val sourcream = if(random.nextInt(100) <= 70) Some(new Sourcream()) else None
override def toString() = String.format("%-60s %5s", name + sourcream.map(_.toString).getOrElse(""), totalPrice.toString )
def * (w:Int) = for (i <- 0 to w) yield this
}
Orderer(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment