Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object HelloWorld {
- def main(args: Array[String]) {
- println("Hello, world!")
- val euro = new Euro(10,20) with Ordered[Euro]
- val euro2 = new Euro(5,1) with Ordered[Euro]
- println("Euro dodaj" ,euro+euro2)
- println("Euro odejmij", euro2-euro)
- println("Euro: ",euro)
- println("Euro 2x: ",euro*2)
- println(euro < euro2)
- println(euro >= euro2)
- }
- abstract class Currency(val symbol:String) {
- override def toString(): String = symbol
- }
- class Euro(val euro:Int, val cents:Int = 0) extends Currency("EUR ") with Ordered[Euro] {
- val inCents = euro*100+cents
- def +(x:Euro):Euro = {
- val noweEuro = x.euro+euro
- val noweCenty = x.cents+cents
- return new Euro(noweEuro,noweCenty)
- }
- def -(x:Euro):Euro = {
- val noweEuro = x.euro-euro
- val noweCenty = x.cents-cents
- return new Euro(noweEuro,noweCenty)
- }
- def *(x:Int):Euro = {
- var noweEuro = x*euro
- var noweCenty = x*cents
- if(noweCenty>99) {
- noweEuro = noweEuro + noweCenty/100
- noweCenty = noweCenty - noweCenty/noweEuro
- }
- return new Euro(noweEuro,noweCenty)
- }
- println("Euro",euro)
- println("Centy",cents)
- println("InCents",inCents)
- override def toString(): String = {
- if(cents>0) {
- return super.toString+euro+"."+cents
- }
- else {
- return super.toString+euro+".--"
- }
- }
- def compare(other: Euro) = {
- this.euro - other.euro
- this.cents - other.cents
- }
- }
- object Euro {
- def fromCents(inCents: Int):Euro = { return new Euro(0,inCents)
- }
- }
- trait Ordered[A] {
- def compare(that: A) : Int
- def < (that: A) : Boolean = (this compare that) < 0
- def > (that: A) : Boolean = (this compare that) > 0
- def <= (that: A) : Boolean = (this compare that) <= 0
- def >= (that: A) : Boolean = (this compare that) >= 0
- }
- case class EmailAddress(address: String)
- case class Person(name: String, age: Int, emailAddresses: List[EmailAddress])
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement