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)
- println("zad5")
- 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"))))
- val process = new ProcessPerson(people)
- println("Normalna lista",people)
- process.pelnoletnie
- process.nazwiska
- process.nazwiskaPelnoletnie
- process.paraDzieciDorosli
- process.paraWiecejMail
- process.kowalski
- process.emailOdNazwiska("Sonic")
- process.osobOdEmail("wilczek1@gmail.com")
- process.pierwszeEmaile
- }
- 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])
- case class ProcessPerson(personsList: List[Person]) {
- def pelnoletnie() {
- val lista = personsList.filter(p => p.age >= 18)
- println("Pelnoletnie",lista)
- }
- def nazwiska() {
- val lista = personsList.map(p => p.name)
- println("Nazwiska",lista)
- }
- def nazwiskaPelnoletnie() {
- val lista = personsList.filter(p => p.age >= 18).map(p => p.name)
- println("NazwiskaPelnoletnie",lista)
- }
- def paraDzieciDorosli() {
- val dzieci = personsList.partition(p => p.age < 18)
- val dorosli = personsList.partition(p => p.age >= 18)
- println("Dzieci",dzieci)
- println("Dorosli",dorosli)
- }
- def paraWiecejMail() {
- val wiecej = personsList.partition(p => p.emailAddresses.size > 1)
- val mniej = personsList.partition(p => p.emailAddresses.size == 1)
- println("Wiecej niz 1",wiecej)
- println("Mniej == 1",mniej)
- }
- def kowalski() {
- val lista = personsList.filter(p => p.name == "Kowalski")
- println("Kowalscy",lista)
- }
- def emailOdNazwiska(x:String) {
- val lista = personsList.filter(p => p.name == x).map(p => p.emailAddresses)
- println("EmailOdNazwiska",lista)
- }
- def osobOdEmail(x:String) {
- val lista = personsList.filter(p => p.emailAddresses == x
- //{
- // val i = 0
- // val sizeList = p.emailAddresses.size-1
- // for(i <- 0 to sizeList) p.emailAddresses(i) == x
- // }
- ).map(p => {
- p.name
- p.age
- })
- println("OsobaOdEmail",lista)
- }
- def pierwszeEmaile() {
- val lista = personsList.filter(p => p.emailAddresses.size > 1).map(p => p.emailAddresses(0))
- println("Pierwsze emaile ",lista)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement