⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 auction.scala

📁 JAVA 语言的函数式编程扩展
💻 SCALA
字号:
package examples.actorsimport java.util.Dateimport scala.actors._import scala.actors.Actor._/** A simple demonstrator program implementing an online auction service *  The example uses the actor abstraction defined in the API of *  package scala.actors. */trait AuctionMessagecase class Offer(bid: Int, client: Actor) extends AuctionMessage // make a bidcase class Inquire(client: Actor) extends AuctionMessage         // inquire statustrait AuctionReplycase class Status(asked: Int, expiration: Date)           // asked sum, expiration date  extends AuctionReplycase object BestOffer extends AuctionReply                // yours is the best offercase class BeatenOffer(maxBid: Int) extends AuctionReply  // offer beaten by maxBidcase class AuctionConcluded(seller: Actor, client: Actor) // auction concluded  extends AuctionReplycase object AuctionFailed extends AuctionReply            // failed with no bidscase object AuctionOver extends AuctionReply              // bidding is closedclass AuctionActor(seller: Actor, minBid: Int, closing: Date) extends Actor {  val timeToShutdown = 3000 // msec  val bidIncrement = 10  def act() {    var maxBid = minBid - bidIncrement    var maxBidder: Actor = null    loop {      reactWithin (closing.getTime() - new Date().getTime()) {        case Offer(bid, client) =>          if (bid >= maxBid + bidIncrement) {            if (maxBid >= minBid)              maxBidder ! BeatenOffer(bid)            maxBid = bid            maxBidder = client            client ! BestOffer          }          else            client ! BeatenOffer(maxBid)        case Inquire(client) =>          client ! Status(maxBid, closing)        case TIMEOUT =>          if (maxBid >= minBid) {            val reply = AuctionConcluded(seller, maxBidder)            maxBidder ! reply            seller ! reply          } else {            seller ! AuctionFailed          }          reactWithin(timeToShutdown) {            case Offer(_, client) => client ! AuctionOver            case TIMEOUT => exit()          }      }    }  }}object auction {  val random = new Random  val minBid = 100  val closing = new Date(new Date().getTime() + 4000)  val seller = Actor.actor { }  val auction = new AuctionActor(seller, minBid, closing)  def client(i: Int, increment: Int, top: Int) = new Actor {    val name = "Client " + i    def log(msg: String) = Console.println(name + ": " + msg)    var max: Int = _    var current: Int = 0    def act() {      log("started")      auction ! Inquire(this)      receive {        case Status(maxBid, _) =>          log("status(" + maxBid + ")")          max = maxBid      }      loop {        if (max >= top) {          log("too high for me")        }        else if (current < max) {          current = max + increment          Thread.sleep(1 + random.nextInt(1000))          auction ! Offer(current, this)        }        reactWithin(3000) {          case BestOffer =>            log("bestOffer(" + current + ")")          case BeatenOffer(maxBid) =>            log("beatenOffer(" + maxBid + ")")            max = maxBid          case AuctionConcluded(seller, maxBidder) =>            log("auctionConcluded"); exit()          case AuctionOver =>            log("auctionOver"); exit()          case TIMEOUT =>            exit()        }      }    }  }  def main(args: Array[String]) {    seller.start()    auction.start()    client(1, 20, 200).start()    client(2, 10, 300).start()  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -