Q1. Can the following code be further improved?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class CashBalance { def balance = 0.0; def initBalance(def initBalance) {this.balance = initBalance } def addCredits(def credits) {this.balance = balance + credits } def subtractDebits(def credits) {this.balance = balance - credits } } def cashBal = new CashBalance(); cashBal.initBalance(50000.00); cashBal.addCredits(10000.00); cashBal.subtractDebits(20000.00); println "Current balance = ${cashBal.balance}" //40000.00 is the ouput |
A1. Yes, it can be made a bit more elegant with the builder design pattern. For example, we can return the “CashBalance” object from each mathod by adding the keyword “this”…