Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.42 KB | None | 0 0
  1. package o1.auctionhouse
  2.  
  3. import scala.collection.mutable.Buffer
  4.  
  5.  
  6. /** The class `AuctionHouse` represents electronic auction houses. It provides methods for
  7.   * adding auctions and producing statistics about the items being sold, among other things.
  8.   *
  9.   * This version of class `AuctionHouse` can only handle English-style auctions (of type
  10.   * `EnglishAuction`). Other ways of selling items are not supported.
  11.   *
  12.   * @param name  the name of the auction house */
  13. class AuctionHouse(val name: String) {
  14.  
  15.   private val items = Buffer[EnglishAuction]()  // container for the items being sold
  16.  
  17.  
  18.   /** Adds the given auction to the auction house. */
  19.   def addItem(item: EnglishAuction): Unit = {
  20.     this.items += item
  21.   }
  22.  
  23.  
  24.   /** Removes the given auction from the auction house, assuming it was there. */
  25.   def removeItem(item: EnglishAuction): Unit = {
  26.     this.items -= item
  27.   }
  28.  
  29.  
  30.   /** Produces a textual representation of this auction house. */
  31.   override def toString = if (this.items.isEmpty) this.name else this.name + ":\n" + this.items.mkString("\n")
  32.  
  33.  
  34.   /** Records one day as having passed. This is equivalent to calling
  35.     * `auction.advanceOneDay()` for each of the auctions in this auction house.
  36.     * @see [[EnglishAuction.advanceOneDay]] */
  37.   def nextDay() = {
  38.     for (current <- this.items) {
  39.       current.advanceOneDay()
  40.     }
  41.   }
  42.  
  43.  
  44.   /** Returns the current total price of all the items that have been put up for sale in this
  45.     * auction house. The total includes the prices of all auctions, be they open or closed. */
  46.   def totalPrice = {
  47.     var totalSoFar = 0
  48.     for (current <- this.items) {
  49.       totalSoFar += current.price
  50.     }
  51.     totalSoFar
  52.   }
  53.  
  54.  
  55.   /** Returns the current average price of all the items that have been put up for sale in this
  56.     * auction house. The average is computed from the prices of all auctions, be they open or closed. */
  57.   def averagePrice = this.totalPrice.toDouble / this.items.size
  58.  
  59.  
  60.   /** Returns the number of auctions in this auction house that are currently open. */
  61.   def numberOfOpenItems = {
  62.     var openCount = 0
  63.     for (current <- this.items) {
  64.       if (current.isOpen) {
  65.         openCount += 1
  66.       }
  67.     }
  68.     openCount
  69.   }
  70.  
  71.  
  72.   /** Returns the priciest item in the auction house, that is, the item whose current price is
  73.     * the highest. Both open and closed items are considered. The item is returned in an `Option`
  74.     * wrapper; if there are no auctions at all, `None` is returned. */
  75.   def priciest = {
  76.     if (this.items.isEmpty) {
  77.       None
  78.     } else {
  79.       var priciestSoFar = this.items(0)
  80.       for (current <- this.items) {  
  81.         if (current.price > priciestSoFar.price) {
  82.           priciestSoFar = current
  83.         }
  84.       }
  85.       Some(priciestSoFar)
  86.     }
  87.   }
  88.    
  89.  
  90.   /** Returns a collection that contains the purchases of a single buyer. This means all the
  91.     * (open or closed) items that have either already been bought by the given person, or that
  92.     * have the person as the highest bidder.  
  93.     * @param buyer  the name of the buyer whose purchases should be returned
  94.     * @return a new buffer containing the auctions */
  95.   def purchasesOf(buyer: String) = {
  96.     val purchases = Buffer[EnglishAuction]()
  97.     for (current <- this.items) {
  98.       if (current.buyer == Some(buyer)) {
  99.         purchases += current
  100.       }
  101.     }
  102.     purchases
  103.   }
  104.  
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement