Question
I have to add the following to an existing class I made. I started making some changes, but i'm really stuck on it. Please help. The top code is the existing class, and the bottom code is the one i made some changes to, but i think i'm not doing it right. Here's what i have to add to my existing BankAccount class: A private integer variable called badTransCounter which counts the number of times insufficientFunds was true. Modify withdraw, deposit, and readBalance to immediately return 0 if the account isClosed. Think of this as no transactions being allowed on a closed account, and the remaining funds are dispersed. A method called isClosed which returns true if badTransCounter is greater than or equal to 5. //Here's my existing class public class BankAccount { private int dollars = 0; private boolean insufficientFunds = false; public BankAccount(int initializeDollars) { dollars = initializeDollars; } public int deposit(int d) { insufficientFunds = false; return dollars += d; } public int withdraw(int d) { if (d <= dollars) { return dollars -= d; } else { insufficientFunds = true; } return dollars; } public boolean insufficientFunds() { return insufficientFunds; } public int readBalance() { return dollars; } } //Here the same class but with a few changes, which i think i did them wrong. And i typed on the lines that i added code to. public class BankAccount2 { private int dollars = 0; private int badTransCounter = 0; //Added this private boolean insufficientFunds = false; private boolean = false; //Added this, but i'm getting an error. public BankAccount2(int initializeDollars) { dollars = initializeDollars; } public int deposit(int d) { insufficientFunds = false; return dollars += d; } public int withdraw(int d) { if (d <= dollars) { return dollars -= d; } else { insufficientFunds = true; badTransCounter++; //Added this if(badTransCounter >= 5) //Added this { isClosed = true; //Added this, but i'm getting an error } } return dollars; } public boolean insufficientFunds() { return insufficientFunds; } public boolean isClosed() //Added this { return isClosed; } public int readBalance() { return dollars; } }
Explanation / Answer
Modified BankAccount.java: public class BankAccount { private int dollars = 0; private boolean insufficientFunds = false; private int badTransCounter = 0; public BankAccount(int initializeDollars) { dollars = initializeDollars; } public int deposit(int d) { if(isClosed()) return 0; insufficientFunds = false; return dollars += d; } public int withdraw(int d) { if(isClosed()) return 0; if (d =5) return true; else return false; } }