Suppose that you are provided with a pre-written class BankAccount as shown belo
ID: 3543216 • Letter: S
Question
Suppose that you are provided with a pre-written class BankAccount as shown below. (The headings are shown, but not the method bodies, to save space.) Assume that the fields, constructor, and methods shown are already implemented. You may refer to them or use them in solving this problem if necessary.
Write an instance method named transfer that will be placed inside the BankAccount class to become a part of each BankAccountobject's behavior. The transfer method moves money from this bank account to another account. The method accepts two parameters: a second BankAccount to accept the money, and a real number for the amount of money to transfer.
There is a $5.00 fee for transferring money, so this much must be deducted from the current account's balance before any transfer.
The method should modify the two BankAccount objects such that "this" current object has its balance decreased by the given amount plus the $5 fee, and the other BankAccount object's balance is increased by the given amount. A transfer also counts as a transaction on both accounts.
If this account object does not have enough money to make the full transfer, transfer whatever money is left after the $5 fee is deducted. If this account has under $5 or the amount is 0 or less, no transfer should occur and neither account's state should be modified.
Explanation / Answer
public void transfer(BankAccount other, double amount) {
if (amount > 0.00) {
if (amount + 5.00 <= balance) {
withdraw(amount + 5.00);
other.deposit(amount);
} else if (balance > 5.00) {
other.deposit(balance - 5.00);
withdraw(balance);
}
}
}
public void transfer(BankAccount other, double amount) {
if (balance > 5 && amount > 0) {
balance -= 5;
if (balance < amount) {
amount = balance;
}
other.deposit(amount);
withdraw(amount);
}
}
public void transfer(BankAccount other, double amount) {
if (balance > 5 && amount > 0) {
transactions++;
other.transactions++;
balance -= 5;
if (balance >= amount) {
other.balance += amount;
balance -= amount;
} else {
other.balance += balance;
balance = 0.0;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.