// ======================== // CMPU-181, Spring 2013 // Code from class // March 6, 2013 // ======================== // NOTE: To use this code, save it to a file named BankAcct.java, // Then open it in drjava. class BankAcct { // =============================== // CLASS-ORIENTED DATA (fields) // =============================== static String BankName = "CMPU-181 Bank of Vassar"; // =============================== // CLASS-ORIENTED FUNCTION // =============================== static void cofunc() { System.out.println("Hello from our bank!"); } // =============================== // OBJECT-ORIENTED DATA (fields) // =============================== // private data is not accessible to the "outside world" private double balance; public String owner; private String password; // ---------------------------- // CONSTRUCTORS // ---------------------------- BankAcct() { System.out.println("Not doing any initialization!"); } BankAcct(String name, String pwd, double initBal) { balance = initBal; owner = name; password = pwd; } // =========================================== // OBJECT-ORIENTED METHODS // =========================================== // INPUTS: THIS, hidden input // OUTPUT: A string representation of the object public String acctString() { // hidden parameter: this <--- the object associated // with this function ca return "Bank Account: name = " + this.owner + ", password = secret!, balance = secret!"; } // getBalance // -------------------------------------- // INPUT: inputPwd, a String // OUTPUT: current balance if the password is correct public double getBalance(String inputPwd) { if (this.password.equals(inputPwd)) return this.balance; else System.out.println("Hey! Password incorrect!"); return 0; } // makeWithdrawal // --------------------------------------- // INPUTS: inputPwd, a String // wdAmt, withdrawal amount // OUTPUT: withdrawal // SIDE EFFECT: printing out information about // the transaction public double makeWithdrawal(String inputPwd, double wdAmt){ if ((this.password.equals(inputPwd)) && (wdAmt > 0) && (wdAmt <= this.balance)) { System.out.println("Withdrawal authorized!"); this.balance = this.balance - wdAmt; System.out.println("New balance = " + this.balance); return wdAmt; } else { System.out.println("Withdrawal not authorized!"); return 0; } } }