Here is a description of the program. And my code is below the output. Someone please help me.
Part 1:
Write a class which represents a bank account. Call this class BankAccount. The class will have the following data and operations:
An integer variable called dollars which will keep track of the current balance.
A method called deposit which increases dollars by the given amount amount.
A method called withdraw which decreases dollars by the given amount amount. Ensure dollars can never become negative. If the programmer withdraws more than exists in the account, set dollars to zero.
Part 2:
Add the following features to the BankAccount class:
A boolean variable called insufficientFunds which becomes true if the user attempts to withdraw more than they have.
Modify withdraw to set insufficientFunds to true if the given withdrawel amount would cause a negative balance. If the transaction would cause a negative balance, leave dollars unchanged.
A method called insufficientFunds which returns the boolean insufficientFunds
Modify deposit to set insufficientFunds to false.
Part 3:
Add the following features to the 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.
I followed all of the directions, but when i submit the code my the output is incorrect:
CORRECTED OUTPUT:
Starting at: 5
Withdraw 20: 5
Insufficient funds. Balance unchanged.
Withdraw 15: 5
Insufficient funds. Balance unchanged.
Deposit 53: 58
Withdraw 300: 58
Insufficient funds. Balance unchanged.
Withdraw 250: 58
Insufficient funds. Balance unchanged.
Withdraw 5: 53
Withdraw 333: 53
Account has been closed.
Withdraw 777: 0
Account has been closed.
Withdraw 50: 0
Account has been closed.
Withdraw 40: 0
Account has been closed.
Withdraw 20: 0
Account has been closed.
MY INCORRECT OUTPUT
Starting at: 5
Withdraw 20: 5
Insufficient funds. Balance unchanged.
Withdraw 15: 5
Insufficient funds. Balance unchanged.
Deposit 53: 58
Withdraw 300: 58
Insufficient funds. Balance unchanged.
Withdraw 250: 58
Insufficient funds. Balance unchanged.
Withdraw 5: 53
Insufficient funds. Balance unchanged.
Withdraw 333: 53
Account has been closed.
Withdraw 777: 0
Account has been closed.
Withdraw 50: 0
Account has been closed.
Withdraw 40: 0
Account has been closed.
Withdraw 20: 0
Account has been closed.
Part 1:
Write a class which represents a bank account. Call this class BankAccount. The class will have the following data and operations:
An integer variable called dollars which will keep track of the current balance.
A method called deposit which increases dollars by the given amount amount.
A method called withdraw which decreases dollars by the given amount amount. Ensure dollars can never become negative. If the programmer withdraws more than exists in the account, set dollars to zero.
Part 2:
Add the following features to the BankAccount class:
A boolean variable called insufficientFunds which becomes true if the user attempts to withdraw more than they have.
Modify withdraw to set insufficientFunds to true if the given withdrawel amount would cause a negative balance. If the transaction would cause a negative balance, leave dollars unchanged.
A method called insufficientFunds which returns the boolean insufficientFunds
Modify deposit to set insufficientFunds to false.
Part 3:
Add the following features to the 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.
I followed all of the directions, but when i submit the code my the output is incorrect:
CORRECTED OUTPUT:
Starting at: 5
Withdraw 20: 5
Insufficient funds. Balance unchanged.
Withdraw 15: 5
Insufficient funds. Balance unchanged.
Deposit 53: 58
Withdraw 300: 58
Insufficient funds. Balance unchanged.
Withdraw 250: 58
Insufficient funds. Balance unchanged.
Withdraw 5: 53
Withdraw 333: 53
Account has been closed.
Withdraw 777: 0
Account has been closed.
Withdraw 50: 0
Account has been closed.
Withdraw 40: 0
Account has been closed.
Withdraw 20: 0
Account has been closed.
MY INCORRECT OUTPUT
Starting at: 5
Withdraw 20: 5
Insufficient funds. Balance unchanged.
Withdraw 15: 5
Insufficient funds. Balance unchanged.
Deposit 53: 58
Withdraw 300: 58
Insufficient funds. Balance unchanged.
Withdraw 250: 58
Insufficient funds. Balance unchanged.
Withdraw 5: 53
Insufficient funds. Balance unchanged.
Withdraw 333: 53
Account has been closed.
Withdraw 777: 0
Account has been closed.
Withdraw 50: 0
Account has been closed.
Withdraw 40: 0
Account has been closed.
Withdraw 20: 0
Account has been closed.
public class BankAccount { //Variable to keep track of the balance. Start it at zero dollars. private int dollars = 0; private boolean insufficientFunds = false; //to determine if the user attempts to withdraw more than they have. private int badTransCounter = 0; //To count the number of times insufficientFunds was true. 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; } else if (d <= dollars) { return dollars -= d; } else { badTransCounter++; insufficientFunds = true; } return dollars; } public boolean insufficientFunds() { return insufficientFunds; } public int readBalance() { if(isClosed()) { return 0; } return dollars; } public boolean isClosed() { if(badTransCounter >= 5) { return true; } else { return false; } } }