(Modified Account Class) Modify class Account (Fig. 3.13) to provide a method ca
ID: 3902389 • Letter: #
Question
(Modified Account Class) Modify class Account (Fig. 3.13) to provide a method called debit that withdraws money from an Account. Ensure that the debit 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 "Debit amount exceeded account balance." Modify class AccountTest (Fig. 3.14) to test method debit. Using Java programming
Explanation / Answer
public class Account { private int acctNum; private double balance; public Account() { this.acctNum = 0; this.balance = 0; } public Account(int acctNum, double balance) { this.acctNum = acctNum; this.balance = balance; } public int getAcctNum() { return acctNum; } public void setAcctNum(int acctNum) { this.acctNum = acctNum; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public void debit(double amount) { if(amount > balance) { System.out.println("Amount to be debited is more than the availablr balance."); } else { balance = balance - amount; } } @Override public String toString() { return String.format("Account #%d has a balance of $%.2f.", acctNum, balance); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.