Last active
August 29, 2015 14:01
-
-
Save stanpalatnik/1af68123b8518e7ccdf9 to your computer and use it in GitHub Desktop.
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 DbWrapper._ | |
import scala.util.{Failure, Success, Try} | |
class Auctioneer { | |
/** | |
* | |
* @param item | |
* @return Boolean Whether or not the auction was successfully started | |
*/ | |
def startAuction(item: Item): Boolean = { | |
using( new KeyValConn() ) { | |
connection => | |
//check item status. If it's closed, then throw an exception | |
//process | |
true | |
} | |
} | |
/** | |
* | |
* @param item [[Item]] | |
* @return Boolean Whether or not the auction was successful | |
*/ | |
def closeAuction(item: Item): Boolean = { | |
using( new KeyValConn() ) { | |
connection => | |
//process | |
} | |
item.currentPrice > item.reservedPrice | |
} | |
/** | |
* | |
* @param name | |
* @return a [[ItemInfo]] | |
*/ | |
def itemInfo(name: String) = Option[ItemInfo] { | |
using( new KeyValConn() ) { | |
connection => | |
//process | |
//return example | |
ItemInfo( | |
auctionStatus = AuctionStatus.NotStarted | |
) | |
} | |
} | |
} | |
case class Participant(id: Int, name: String) | |
object Participant { | |
/** | |
* | |
* @param bid | |
* @return True if bid was successful | |
*/ | |
def submitBid(bid: ItemBid): Boolean = { | |
//submit the bid to the db | |
using( new KeyValConn() ) { | |
connection => | |
//process | |
true | |
} | |
} | |
/** | |
* | |
* @param name | |
* @return a new [[ItemInfo]] | |
*/ | |
def itemInfo(name: String) = Option[ItemInfo] { | |
using( new KeyValConn() ) { | |
connection => | |
//process | |
//return example | |
ItemInfo( | |
auctionStatus = AuctionStatus.NotStarted | |
) | |
} | |
} | |
} | |
case class Item(name: String, currentPrice: Float, reservedPrice: Float) | |
case class ItemBid(item: Item, bidPrice: Float) { | |
require(bidPrice > item.currentPrice, "The bid price must be higher than the current price.") | |
} | |
case class ItemInfo(auctionStatus: AuctionStatus.AuctionStatus, isSold: Boolean = false, info: Option[SoldItem] = None) | |
case class SoldItem(priceSold: Float, soldTo: Participant) | |
object AuctionStatus extends Enumeration { | |
type AuctionStatus = Value | |
val NotStarted = Value("Not Started") | |
val Open = Value("Open") | |
val Closed = Value("Closed") | |
} | |
//Database utils | |
class KeyValConn { | |
def close() = {} | |
} | |
/** | |
* DB wrapper | |
*/ | |
object DbWrapper { | |
def using[Closeable <: { def close() }, B]( closeable: Closeable )( predicate: Closeable => B ): B = { | |
val result = Try(predicate(closeable)) | |
Try(closeable.close()) | |
result match { | |
case Success(res) => res | |
case Failure(ex) => throw ex | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment