-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAbstractDemo1.scala
56 lines (46 loc) · 1.33 KB
/
AbstractDemo1.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
package UtilityScala
// create a class : Account that includes constructor, method
// https://www.youtube.com/watch?v=J_e9mpBV1SI&list=PLmOn9nNkQxJEqCNXBu5ozT_26xwvUbHyE&index=73
object AbstractDemo1 extends App {
// run
val account1 = new Account("A0001", 100, 123)
account1.query(123)
account1.withdraw(123, 20)
account1.query(789)
account1.withdraw(789, 20)
account1.withdraw(123, 1000)
}
class Account (InAccountNo:String, InBalance:Double, inPwd:Int){
// constructor
val accountNo:String = InAccountNo
var balance:Double = InBalance
var password:Int = inPwd
// method
def query(pwd: Int): Unit = {
if (pwd != this.inPwd){
// if password is wrong
println("password wrong !")
return
}
// if password is correct
println("login success !")
println(s"this accountNo = $accountNo has balance : $balance")
}
def withdraw(pwd: Int, money: Double): Unit = {
if (pwd != this.inPwd) {
// if password is wrong
println("password wrong !")
return
}
// if withdraw > balance
if (money > this.balance){
println("balance is not enough")
return
}
// if password is correct
this.balance = this.balance - money
println("withdraw success !")
println(s"this accountNo = $accountNo has balance : $balance")
}
// TODO : transaction
}