Question: Modify class Account (Fig. 7.8) to provide a method called withdraw that withdraws money from an Account. Ensure that the withdrawal amount does not exceed
Modify class Account (Fig. 7.8) to provide a method called withdraw that withdraws money from an Account. Ensure that the withdrawal amount does not exceed the Account’s balance. If it does, the balance should be left unchanged and the method should print a message indicating "Withdrawal amount exceeded account balance." Modify class AccountTest (Fig. 7.9) to test method withdraw.
Fig. 7.8
Fig. 7.9
I // Fig. 7.8: Account.java 2 // Account class with a double instance variable balance and a constructor // and deposit method that perform validation. 3 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 public class Account { private String name; // instance variable private double balance; // instance variable 40 41 } // Account constructor that receives two parameters public Account (String name, double balance) { this.name = name; // assign name to instance variable name } // method that deposits (adds) only a valid amount to the balance public void deposit (double depositAmount) { if (depositAmount > 0.0) { // if the deposit Amount is valid balance + depositAmount; // add it to the balance } // validate that the balance is greater than 0.0; if it's not, // instance variable balance keeps its default initial value of 0.0 if (balance > 0.0) { // if the balance is valid this.balance = balance; // assign it to instance variable balance // method returns the account balance public double getBalance() { return balance; } } } // method that sets the name public void setName(String name) { this.name = name; } // method that returns the name public String getName() { return name;
Step by Step Solution
3.33 Rating (153 Votes )
There are 3 Steps involved in it
To fulfill the requirements of the given assignment you should add a withdraw method to the Account class that allows the withdrawal of funds provided the amount does not exceed the account balance He... View full answer
Get step-by-step solutions from verified subject matter experts
