Skip to content

Instantly share code, notes, and snippets.

@devsprint
Created April 27, 2020 11:09
Show Gist options
  • Save devsprint/b01694ee2be1fbe3b7dc242092afe369 to your computer and use it in GitHub Desktop.
Save devsprint/b01694ee2be1fbe3b7dc242092afe369 to your computer and use it in GitHub Desktop.
object portfolio {
/**
* EXERCISE 1
*
* Using only sealed traits and case classes, develop a model of a stock
* exchange. Ensure there exist values for NASDAQ and NYSE.
*/
sealed trait Exchange
object Exchange {
final case object NASDAQ extends Exchange
final case object NYSE extends Exchange
}
/**
* EXERCISE 2
*
* Using only sealed traits and case classes, develop a model of a currency
* type.
*/
sealed trait CurrencyType
object CurrencyType {
final case object USD extends CurrencyType
final case object EUR extends CurrencyType
}
/**
* EXERCISE 3
*
* Using only sealed traits and case classes, develop a model of a stock
* symbol. Ensure there exists a value for Apple's stock (APPL).
*/
sealed trait StockSymbol {
def currentValue: BigDecimal
def currency: CurrencyType
def exchange: Exchange
}
object StockSymbol {
final case class APPL(currentValue: BigDecimal, currency: CurrencyType, exchange: Exchange) extends StockSymbol
final case class T(currentValue: BigDecimal, currency: CurrencyType, exchange: Exchange) extends StockSymbol
}
/**
* EXERCISE 4
*
* Using only sealed traits and case classes, develop a model of a portfolio
* held by a user of the web application.
*/
final case class Portfolio(stocks: Map[StockSymbol, BigDecimal])
/**
* EXERCISE 5
*
* Using only sealed traits and case classes, develop a model of a user of
* the web application.
*/
final case class User(userId: String, name: String)
/**
* EXERCISE 6
*
* Using only sealed traits and case classes, develop a model of a trade type.
* Example trade types might include Buy and Sell.
*/
sealed trait TradeType
object TradeType {
final case object Buy extends TradeType
final case object Sell extends TradeType
}
/**
* EXERCISE 7
*
* Using only sealed traits and case classes, develop a model of a trade,
* which involves a particular trade type of a specific stock symbol at
* specific prices.
*/
final case class Trade(tradeType: TradeType, quantity: Int, symbol: StockSymbol, price: BigDecimal, currencyType: CurrencyType)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment