Created
July 18, 2012 13:51
-
-
Save anvie/3136336 to your computer and use it in GitHub Desktop.
Materi Scala Belajar bersama di Ansvia HQ pertemuan ke 4
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
package com.ansvia.hello | |
/** | |
* Copyright (C) 2011-2012 Ansvia Inc. | |
* User: robin | |
* Date: 7/18/12 | |
* Time: 7:40 PM | |
* | |
*/ | |
trait Motor { | |
var status:String = "unknown" | |
def run() { | |
this.status = "running" | |
} | |
def stop() { | |
this.status = "stop" | |
} | |
def getStatus = this.toString + ": " + this.status | |
override def toString:String = super.toString | |
} | |
trait Nos { | |
def startNos() | |
} | |
trait VelgRacing { | |
def merekVelg:String = "unknown" | |
} | |
class Yamaha extends Motor { | |
override def toString = "Yamaha" | |
} | |
class Honda extends Motor { | |
override def toString = "Honda" | |
} | |
class Mobil { | |
override def toString = "Mobil" | |
} | |
abstract class MotorBalap(velgTitle:String) | |
extends Motor with Nos with VelgRacing { | |
override def merekVelg = velgTitle | |
} | |
class MioBalap extends MotorBalap("Ruji") { | |
def startNos() { | |
this.status = "Running with NOS" | |
} | |
override def toString = "Yamaha Mio" | |
override def merekVelg = "Venom" | |
} | |
class Daftar[T] { | |
private var data = List[T]() | |
def add(o:T){ | |
data :+= o | |
} | |
def remove(o:T){ | |
data = data.filterNot(_ == o) | |
} | |
def printDaftar(){ | |
println("Daftar kita:") | |
data foreach { | |
motor => | |
println(" * " + motor) | |
} | |
} | |
} | |
object BengkelMotor { | |
def testRun(motor:Motor){ | |
println("testing " + motor) | |
motor.run() | |
motor match { | |
case motorBalap:MotorBalap if motor.getStatus == motor.toString + ": running" => | |
motorBalap.startNos() | |
//println(motorBalap.getStatus) | |
case _ => | |
} | |
println(motor.getStatus) | |
} | |
} | |
object Hello { | |
def main(args:Array[String]){ | |
val yamaha = new Yamaha | |
val honda = new Honda | |
val miobalap = new MioBalap | |
// | |
// BengkelMotor.testRun(yamaha) | |
// BengkelMotor.testRun(honda) | |
// BengkelMotor.testRun(miobalap) | |
val daftarMotor = new Daftar[Motor] | |
daftarMotor.add(yamaha) | |
daftarMotor.add(honda) | |
daftarMotor.add(miobalap) | |
daftarMotor.printDaftar() | |
daftarMotor.remove(honda) | |
daftarMotor.printDaftar() | |
val mobil = new Mobil | |
val daftarMobil = new Daftar[Mobil] | |
daftarMobil.add(mobil) | |
daftarMobil.printDaftar() | |
// | |
// val yamaha = new Yamaha | |
// println(yamaha) | |
// println(yamaha.getStatus) | |
// yamaha.run() | |
// println(yamaha.getStatus) | |
// yamaha.stop() | |
// println(yamaha.getStatus) | |
// | |
// val honda = new Honda | |
// println(honda) | |
// println(honda.getStatus) | |
// honda.run() | |
// println(honda.getStatus) | |
// honda.stop() | |
// println(honda.getStatus) | |
// | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment