BankAccount.scalapackage week52import akka.actor.Actorimport Akka.event.loggingreceiveobject bankaccount { case class deposit (amount: BIGINT) { require (amount > 0) } case class withdraw (Amount: bigint) { require (amount > 0) } case object done case object failed}class bankaccount Extends actor { import bankaccount._ var balance = bigint ( 0) def receive = loggingreceive { case deposit ( Amount) => balance += amount sender ! done case withdraw (amount) if amount <= balance => balance -= amount sender ! Done case _ => sender ! failed }}
wiretransfer.scalapackage week52import akka.actor.actorimport akka.actor.actorrefimport akka.event.loggingreceiveobject wiretransfer { case class transfer (from: actorref, to: actorref, amount: bigint) case object Done case object failed}class wiretransfer extends actor { import WireTransfer._ def receive = LoggingReceive { case transfer (From, to, amount) => from ! bankaccount.withdraw (amount) context.become (AwaitFrom (To), amount, sender)) } def awaitfrom (to: ActorRef, AMOUNT:&NBSP;BIGINT,&NBSP;CUSTOMER:&NBSP;ACTORREF): receive = loggingreceive { case bankacCount. Done => to ! bankaccount.deposit (amount) context.become (Awaitto (customer)) case bankaccount.failed => customer ! Failed Context.stop (self) } def awaitto (customer: actorref): Receive = LoggingReceive { case BankAccount.Done => customer ! done context.stop (self) }}
transfermain.scalapackage week52import akka.actor.actorimport akka.actor.propsimport akka.event.LoggingReceiveclass TransferMain extends Actor { val Accounta = context.actorof (props[bankaccount], "AccountA") val accountb = context.actorof (props[bankaccount], "ACCOUNTB") accounta ! bankaccount.deposit (+) def receive = LoggingReceive { case Bankaccount.done => transfer ( } def transfer) (AMOUNT:&NBSP;BIGINT): unit = { val transaction = context.actorof (Props[ wiretransfer], "Transfer") transaction ! wiretransfer.transfer (AccountA, accountb, amount) context.become (loggingreceive { case wiretransfEr. DONE&NBSP;=>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;PRINTLN ("Success") context.stop (self) case wiretransfer.failed => println ("Failed") context.stop (self) }) }}
Akka example of 51 bank transfers