-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hello.kt
119 lines (96 loc) · 2.4 KB
/
Hello.kt
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import java.util.Scanner
class Calculator {
companion object {
fun add(a : Int , b : Int) =
println(a + b)
}
}
// Singleton Class for single instance of a class
class Database private constructor() {
companion object {
private var inst : Database ?= null
fun getInstance() : Database?{
if(inst==null){
inst = Database();
}
return inst
}
}
}
object Data{
init{
println("Database Created...!")
}
fun add() = println("Hello")
}
// Lazy Initialization
class User(var name : String ,var age : Int) : Any()
{
init{
println("user : $name is created")
}
}
/*
main()=>
val user = User("daniel" , 20)
val user2 by lazy {
User("david" , 2)
}
user2.name
*/
// Enum Classes
enum class Direction(var value : Int){
north(1) , south(2), east(3), west(4);
fun printDetail() = println("Value : $value")
}
// println(Direction.north.value)
// Direction.west.printDetail()
// ListView
class ListView(val it : Array<String>) {
inner class ListViewItems{
fun displayItem(pos : Int) = println(it[pos])
}
}
// val listView = ListView(arrayOf("Daniel" , "David" ))
// val it = listView.ListViewItems()
// it.displayItem(0);
// Bank Account
class Account(var name : String )
{
private var Balance : Double = 0.0;
private var Transactions = mutableListOf<String>()
fun deposit(amount : Double){
Balance += amount;
println("Deposited Amount $amount")
println("Balance Amount $Balance")
updateTransaction("Deposit" , ""+amount)
}
fun withdraw(amount : Double){
if(Balance-amount < 0)
{
println("Your Balance is low")
return
}
Balance -= amount;
println("Withdraw Amount $amount")
println("Balance Amount $Balance")
updateTransaction("Withdraw" , ""+amount)
}
private fun updateTransaction(operation : String , amount : String){
Transactions.add("$operation : $amount")
}
fun history(){
println("--------Transaction History-------")
for(t in Transactions)
println(t)
}
init{
println("Welcome $name")
}
}
// var acc = Account("daniel")
// acc.withdraw(200.0)
// acc.deposit(900.0)
// acc.deposit(200.0)
// acc.withdraw(300.0)
// acc.history()