Created
December 27, 2008 20:58
-
-
Save bjartek/40312 to your computer and use it in GitHub Desktop.
Scala versjon av http://linux.hig.no/Prosjekter/PizzaScriptet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Calendar | |
import scala.io._ | |
import scala.collection.mutable.HashMap | |
abstract case class OrderLine(name: String, _price:Int, weight: Int){ | |
var ammount = 1 | |
def ++ { | |
ammount += 1 | |
} | |
def price = _price | |
override def toString = { | |
String.format("%1$2d x %2$-55s a %3$3d = %4$4d,- NOK", Array(ammount: | |
Integer, name, price: Integer, (price * ammount): Integer)) | |
} | |
} | |
case class Sauce extends OrderLine("Rømmedressing", 20, 1) | |
case class CustomPizza(cn: String, cw:Int, cp:Int) extends OrderLine(cn,cp,cw) | |
case class NormalPizza(n: String, w: Int, p:Int) extends OrderLine(n, p, w) { | |
override def price = if(isWednesday) 119 else _price; | |
def isWednesday = 4 == Calendar.DAY_OF_WEEK | |
} | |
class Order(maxPrice: Int){ | |
val items = new HashMap[String, OrderLine]; | |
var totalPrice = 0; | |
def add (item: OrderLine) { | |
if(item.price + totalPrice < maxPrice) { | |
totalPrice += item.price | |
if(items.contains(item.name)){ | |
items(item.name)++ | |
}else { | |
items += item.name -> item | |
} | |
} | |
} | |
override def toString = { | |
items.values.mkString("\n") + String.format("\n %1$-65s = %2$4d,- NOK", Array("Total pris:", totalPrice: Integer)) | |
} | |
} | |
object pizza { | |
def main(args: Array[String]) { | |
var people: Int = 0; | |
if(args.size == 0){ | |
println("How many peeps"); | |
people = readLine().toInt; | |
} else { | |
people = args(0).toInt; | |
} | |
val numPizzas = (people * hunger).toInt | |
val order = new Order(1200) | |
println("Max antall pizza: " + numPizzas) | |
for (i <- 1 to numPizzas ) { | |
if(addSauce) { | |
order add(new Sauce) | |
} | |
order add randomPizza | |
} | |
println(order); | |
} | |
val sourceProb = 0.7 | |
def addSauce() = rnd.nextDouble <= sourceProb | |
def randomPizza() = weightedList(rnd.nextInt(weightedList.size)) | |
val rnd = new Random() | |
val hunger = 0.4; | |
val menu = | |
NormalPizza("Pappas spesial", 4, 159) :: | |
NormalPizza("Texas", 3, 149) :: | |
NormalPizza("Blue Hawai", 7, 149) :: | |
NormalPizza("Floriad", 4, 149) :: | |
NormalPizza("Buffalo", 4, 149) :: | |
NormalPizza("Chicken", 4, 149) :: | |
NormalPizza("New York", 0, 149) :: | |
NormalPizza("Las Vegas", 6, 149) :: | |
NormalPizza("Vegetarianer", 0, 149) :: | |
NormalPizza("FILADELFIA", 4, 149) :: | |
NormalPizza("Hot Chicago", 7, 149) :: | |
NormalPizza("Hot Express", 5, 149) :: | |
NormalPizza("Kebab pizza spesial", 3, 169) :: | |
CustomPizza("Egenkomponert Pepperoni Biff Bacon Skinke løk", 9, 159) :: | |
CustomPizza("Egenkomponert Biff Pepperoni Bacon Skinke Tacokjøtt", 9, 159) :: Nil | |
def weightedList = for(p <- menu; i <- 1 to p.weight) yield { p } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment