Advertisement
Sylv3rWolf

Untitled

Dec 11th, 2018
789
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. object HelloWorld {
  2. def main(args: Array[String]) {
  3. println("Hello, world!")
  4. val euro = new Euro(10,20) with Ordered[Euro]
  5. val euro2 = new Euro(5,1) with Ordered[Euro]
  6. println("Euro dodaj" ,euro+euro2)
  7. println("Euro odejmij", euro2-euro)
  8. println("Euro: ",euro)
  9. println("Euro 2x: ",euro*2)
  10. println(euro < euro2)
  11. println(euro >= euro2)
  12. println("zad5")
  13. val people = List(Person("Kowalski",20,List(EmailAddress("wilczek1@gmail.com"))),Person("Pimpim",18,List(EmailAddress("wilczek2@gmail.com"))),Person("Uzumaki",16,List(EmailAddress("wilczek3@gmail.com"))),Person("Uchiha",15,List(EmailAddress("wilczek4@gmail.com"))),Person("Obito",22,List(EmailAddress("wilczek5@gmail.com"),EmailAddress("obito@gmail.com"))),Person("Sonic",23,List(EmailAddress("wilczek6@gmail.com"))),Person("Megaman",24,List(EmailAddress("wilczek7@gmail.com"),EmailAddress("megaman@gmail.com"))),Person("Boruto",14,List(EmailAddress("wilczek8@gmail.com"),EmailAddress("boruto@gmail.com"))))
  14. val process = new ProcessPerson(people)
  15. println("Normalna lista",people)
  16. process.pelnoletnie
  17. process.nazwiska
  18. process.nazwiskaPelnoletnie
  19. process.paraDzieciDorosli
  20. process.paraWiecejMail
  21. process.kowalski
  22. process.emailOdNazwiska("Sonic")
  23. process.osobOdEmail("wilczek1@gmail.com")
  24. process.pierwszeEmaile
  25.  
  26. }
  27.  
  28. abstract class Currency(val symbol:String) {
  29. override def toString(): String = symbol
  30. }
  31.  
  32. class Euro(val euro:Int, val cents:Int = 0) extends Currency("EUR ") with Ordered[Euro] {
  33. val inCents = euro*100+cents
  34. def +(x:Euro):Euro = {
  35. val noweEuro = x.euro+euro
  36. val noweCenty = x.cents+cents
  37. return new Euro(noweEuro,noweCenty)
  38. }
  39. def -(x:Euro):Euro = {
  40. val noweEuro = x.euro-euro
  41. val noweCenty = x.cents-cents
  42. return new Euro(noweEuro,noweCenty)
  43. }
  44. def *(x:Int):Euro = {
  45. var noweEuro = x*euro
  46. var noweCenty = x*cents
  47. if(noweCenty>99) {
  48. noweEuro = noweEuro + noweCenty/100
  49. noweCenty = noweCenty - noweCenty/noweEuro
  50. }
  51. return new Euro(noweEuro,noweCenty)
  52. }
  53. println("Euro",euro)
  54. println("Centy",cents)
  55. println("InCents",inCents)
  56.  
  57. override def toString(): String = {
  58. if(cents>0) {
  59. return super.toString+euro+"."+cents
  60. }
  61. else {
  62. return super.toString+euro+".--"
  63. }
  64. }
  65.  
  66. def compare(other: Euro) = {
  67. this.euro - other.euro
  68. this.cents - other.cents
  69. }
  70. }
  71.  
  72. object Euro {
  73. def fromCents(inCents: Int):Euro = { return new Euro(0,inCents)
  74. }
  75. }
  76.  
  77. trait Ordered[A] {
  78. def compare(that: A) : Int
  79. def < (that: A) : Boolean = (this compare that) < 0
  80. def > (that: A) : Boolean = (this compare that) > 0
  81. def <= (that: A) : Boolean = (this compare that) <= 0
  82. def >= (that: A) : Boolean = (this compare that) >= 0
  83.  
  84. }
  85.  
  86. case class EmailAddress(address: String)
  87. case class Person(name: String, age: Int, emailAddresses: List[EmailAddress])
  88.  
  89. case class ProcessPerson(personsList: List[Person]) {
  90. def pelnoletnie() {
  91. val lista = personsList.filter(p => p.age >= 18)
  92. println("Pelnoletnie",lista)
  93. }
  94. def nazwiska() {
  95. val lista = personsList.map(p => p.name)
  96. println("Nazwiska",lista)
  97. }
  98. def nazwiskaPelnoletnie() {
  99. val lista = personsList.filter(p => p.age >= 18).map(p => p.name)
  100. println("NazwiskaPelnoletnie",lista)
  101. }
  102. def paraDzieciDorosli() {
  103. val dzieci = personsList.partition(p => p.age < 18)
  104. val dorosli = personsList.partition(p => p.age >= 18)
  105. println("Dzieci",dzieci)
  106. println("Dorosli",dorosli)
  107. }
  108. def paraWiecejMail() {
  109. val wiecej = personsList.partition(p => p.emailAddresses.size > 1)
  110. val mniej = personsList.partition(p => p.emailAddresses.size == 1)
  111. println("Wiecej niz 1",wiecej)
  112. println("Mniej == 1",mniej)
  113. }
  114. def kowalski() {
  115. val lista = personsList.filter(p => p.name == "Kowalski")
  116. println("Kowalscy",lista)
  117. }
  118. def emailOdNazwiska(x:String) {
  119. val lista = personsList.filter(p => p.name == x).map(p => p.emailAddresses)
  120.  
  121. println("EmailOdNazwiska",lista)
  122. }
  123. def osobOdEmail(x:String) {
  124. val lista = personsList.filter(p => p.emailAddresses == x
  125. //{
  126. // val i = 0
  127. // val sizeList = p.emailAddresses.size-1
  128. // for(i <- 0 to sizeList) p.emailAddresses(i) == x
  129. // }
  130. ).map(p => {
  131. p.name
  132. p.age
  133. })
  134.  
  135. println("OsobaOdEmail",lista)
  136. }
  137. def pierwszeEmaile() {
  138. val lista = personsList.filter(p => p.emailAddresses.size > 1).map(p => p.emailAddresses(0))
  139. println("Pierwsze emaile ",lista)
  140. }
  141. }
  142.  
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement