must be in java In this problem, you are going to enhance the BankAccount class
ID: 3738011 • Letter: M
Question
must be in java
In this problem, you are going to enhance the BankAccount class of Chapter 3, page 94 and 95. You should already have this coded. We are going to enhance it by making the deposit and withdraw methods more realistic. You could not withdraw any amount of money if your balance was less than or equal to 0. You also could not deposit a negative amount.
Create a new class named EnhancedBankAccount.
If you have it copy BankAccount to EnhancedBankAcount. If you no longer have the BankAccount class, copy the BankAccount class form page 94 and 95.
In the deposit method, check to see if the deposit is negative. If it is, then print “You cannot make a negative deposit” and do not make the deposit. If it is not, make the deposit.
In the withdraw method, check to see if the balance – the amount to withdraw is less than 0. If it is then print “You do not have enough money to make the withdrawal”. If it is not, make the withdrawal.
Create a EnhancedBankAccountTester by either copying the prior one or entering the code from the text on page 101. Make sure the reference to BankAccount is changed to EnhancedBankAccount.
Enter the code as it appears on page 101.
Make a negative deposit.
Print the balance of harryChecking account.
Make a withdrawal greater than the balance.
Print the balance of harryChecking account.
Explanation / Answer
Since you have not provided the existing code of BankAccount class, I am providing a sample version of deposit and withdraw without any EnhancedBankAccountTester class since its code is purely dependent on the existing implementation of BankAccount class.
CODE
======================
public void deposit(double amount) {
if(amount < 0) {
System.out.println("You cannot make a negative deposit...");
return;
}
balance += amount;
System.out.println(amount + " deposited successfully!!");
}
public void withdraw(double amount) {
if(balance < amount) {
System.out.println("You do not have enough money to make the withdrawal...");
return;
}
balance -= amount;
System.out.println(amount + " withdrawn successfully!!");
}
*****NOTE: Just include these methods in your EnhancedBankAccount class and create the EnhancedBankAccountTester class.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.