// Bank accounts in Kotlin: basic classes and inheritance // open class means class can be inherited by another class open class account(n:String, initial:Double) // don't put val, var here as { // that makes them public public val name = n; // val can't be changed protected var balance = initial; // var can public open fun inquiry():Double // default public (not same as c#,java) { return balance; } open fun withdraw(amt:Double) // if not open, can't override (final) { if (amt0) balance -= amt; } open fun deposit(amt:Double) { if (amt>0) balance += amt; } }//open class account // subclasses open class savings(n:String, ib:Double) : account(n,ib) { var gift = "toaster" private set; // gift can only be changed internally override fun deposit(amt:Double) { if (amt>=10000) println("Congratulations "+name+", you won a "+gift); super.deposit(amt); // hmmm - no error of 10000 instead of 10000.0 here } }//savings /* This Bank lets you select your minimum balance threshold: whenever your balances dips below the minimum min, you will be charged a fee of $5000/min. So if your chosen min is $1000, then your will be charged $5 when a withdraw dips your balance below $1000. However, if your min is $5000, then you will be charged $1 if your balance goes below $5000. */ open class checking(n:String, ib:Double, val min:Double) : account(n,ib) { private fun chargefee() { balance -= 5000/min;} override fun withdraw(amt:Double) { super.withdraw(amt) if (balance) { // create accounts for the Imal brothers: val acct1:account = savings("F. Arman Imal",1000.0); val acct2 = checking("P. Artyan Imal",1500.0,1000.0); // type inferred acct1.withdraw(400.0) // Arman needs more feed acct2.deposit(300.0) // Artyan got a check from mom for textbooks acct1.deposit(10000.0) // Arman deposits hard earned savings acct2.withdraw(1000.0) // Artyan buys more party supplies println("balance for "+acct1.name+": "+acct1.inquiry()) println("balance for "+acct2.name+": "+acct2.inquiry()) }//main