-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExec8.scala
90 lines (76 loc) · 2.52 KB
/
Exec8.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import collection.mutable.ArrayBuffer
// 1
class BankAccount(initialBalance: Double) {
private var balance = initialBalance
def deposit(amount: Double) = { balance += amount; balance }
def withdraw(amount: Double) = { balance -= amount; balance }
}
class CheckAccount(initialBalance: Double) extends BankAccount(initialBalance) {
override def deposit(amount: Double) = {super.deposit(amount - 1)}
override def withdraw(amount: Double) = {super.withdraw(amount + 1)}
}
// 2
class SavingAccount(initialBalance: Double) extends BankAccount(initialBalance) {
private val interestRate = 0.001
private var transactionCount = 3
def earnMonthlyInterest() {
super.deposit(super.deposit(0) * interestRate)
transactionCount = 3
}
override def deposit(amount: Double) = {
if (transactionCount > 0) {
transactionCount -= 1
super.deposit(amount)
} else {
super.deposit(amount - 1)
}
}
override def withdraw(amount: Double) = {
if (transactionCount > 0) {
transactionCount -= 1
super.withdraw(amount)
} else {
super.withdraw(amount + 1)
}
}
}
// 4
abstract class Item {
def price: Double
def description: String
}
class SimpleItem(val price: Double, val description: String) extends Item {}
class Bundle extends Item {
val items: ArrayBuffer[Item] = new ArrayBuffer[Item]
override def price = items.foldLeft(0.0)((p, item) => p + item.price)
override def description = {
val descs = items.foldLeft("")((desc, item) => desc + "\n" + item.description)
"A bundle which contains " + items.length + " Items. Including:[ \n" + descs + "\n]"
}
def addItem(item: Item) {items += item}
}
// 5
class Point1(val x: Int, val y: Int) {}
class LabeledPoint(val label: String, x: Int, y: Int) extends Point1(x, y) {}
// 7
class Square(x: Int, y: Int, width: Int)
extends java.awt.Rectangle(x, y, width, width) {
def this(width: Int) { this(0, 0, width) }
def this() { this(0, 0, 0) }
}
object Exec8 extends App {
val name = new StringBuilder("ipiszy")
name.synchronized({name ++= " is super!"})
def printAnyRef(ref: AnyRef) {println(ref.eq(Unit))}
printAnyRef(new String("ipiszy"))
val bundle = new Bundle()
bundle.addItem(new SimpleItem(23, "add"))
bundle.addItem(new SimpleItem(1000, "ipiszy"))
println(bundle.description)
val bundle2 = new Bundle()
bundle2.addItem(bundle)
bundle2.addItem(new SimpleItem(9999, "hahaha"))
println(bundle2.description)
val p = new LabeledPoint("Black Friday", 12, -8);
println(p.x, p.y, p.label)
}