Created
June 1, 2013 01:19
-
-
Save bhameyie/5688946 to your computer and use it in GitHub Desktop.
Coffeeshop with Scala
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 akka.actor.{ActorSystem, Props, Actor, Inbox, ActorRef} | |
import scala.concurrent.duration._ | |
case class NoSoupForYou | |
case class OpenShop | |
case class CloseShop | |
case class CappucinoRequest | |
case class TeaRequest | |
case class Check(amount: Int) | |
class CoffeeShop(barista: ActorRef) extends Actor{ | |
var isOpen=true | |
def receive = { | |
case OpenShop => | |
isOpen = true | |
println("The coffehouse is open") | |
case CloseShop => | |
isOpen = false | |
println("Coffeeshop closing") | |
case TeaRequest => | |
if (isOpen) { | |
println("Sending tea request to the kitchen") | |
barista ! TeaRequest | |
sender ! Check(5) | |
} | |
else { | |
println("No tea for you") | |
sender ! NoSoupForYou | |
} | |
case CappucinoRequest => | |
if (isOpen) { | |
println("Sending cappucino request to the kitchen") | |
barista ! CappucinoRequest | |
sender ! Check(5) | |
} | |
else{ | |
println("No capuccino for you") | |
sender ! NoSoupForYou | |
} | |
} | |
} | |
class Barista extends Actor{ | |
def receive = { | |
case CappucinoRequest => println("Making a Cappucino for the customer") | |
case TeaRequest => println("Making Tea for the customer") | |
} | |
} | |
case class OrderTea | |
case class OrderCoffee | |
class Customer(house: ActorRef) extends Actor{ | |
def receive = { | |
case Check(amount) => println(s"Gotta pay my $amount dollar bill") | |
case OrderTea => | |
println("I want tea, and I want it now!!") | |
house ! TeaRequest | |
case OrderCoffee => | |
println("I want coffee, and I want it now!!") | |
house ! CappucinoRequest | |
case NoSoupForYou => println("Nooooooooo!!!!!!!") | |
} | |
} | |
object HelloAkkaScala extends App { | |
val system = ActorSystem("coffee") | |
val barista = system.actorOf(Props[Barista], "Barista") | |
val house = system.actorOf(Props(new CoffeeShop(barista)), "house") | |
val customer = system.actorOf(Props(new Customer(house)), "customer") | |
house ! OpenShop | |
customer ! OrderCoffee | |
customer ! OrderTea | |
customer ! OrderCoffee | |
customer ! OrderTea | |
house ! CloseShop | |
system.shutdown() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment