I have been working on this:
Design a class for a bank database. The database should support the following operations:
❍ deposit a certain amount into an account
❍ withdraw a certain amount from an account
❍ get the balance (i.e., the current amount) in an account
❍ transfer an amount from one account to another
The amount in the transactions is a value of type double. The accounts are identified by instances of the class Account that is in the package com.megabankcorp.records. The database class should be placed in a package called com.megabankcorp.system. The deposit, withdraw, and balance operations should not have any implementation, but allow subclasses to provide the implementation. The transfer operation should use the deposit and withdraw operations to implement the transfer. It should not be possible to alter this operation in any subclass, and only classes within the package com.megabankcorp.system should be allowed to use this operation. The deposit and withdraw operations should be accessible in all packages. The balance operation should only be accessible in subclasses and classes within the package com.megabankcorp.system.
And this is the implementation:
Bank.java
package com.megabankcorp.system;
import java.util.List;
import com.megabankcorp.records.Account;
public abstract class Bank {
public abstract void depositAmount(Account account, double amount);
public abstract void withdrawAmount(Account account, double amount);
protected abstract double currentBalance(Account account);
List<Account> accounts; //Can be more than one account.
boolean transferAmount(Bank bank, Account transferFrom, Account transferTo, double amount) {
boolean isTransferred = false;
if (transferFrom.getAccountNumber() == transferTo.getAccountNumber()) {
System.out.println("Can not transfer in your own account.");
} else if (transferFrom.getCurrentAmount() < amount) {
System.out.println("You have insufficient funds.");
} else {
bank.depositAmount(transferTo, amount);
bank.withdrawAmount(transferFrom, amount);
isTransferred = true;
}
return isTransferred;
}
}
ABC.java
package com.megabankcorp.system;
import java.util.ArrayList;
import com.megabankcorp.records.Account;
public class ABC extends Bank {
public ABC () {
this.accounts = new ArrayList<Account>();
}
@Override
public void depositAmount(Account account, double amount) {
account.setCurrentAmount(account.getCurrentAmount() - amount);
}
@Override
public void withdrawAmount(Account account, double amount) {
account.setCurrentAmount(account.getCurrentAmount() - amount);
}
@Override
protected double currentBalance(Account account) {
return account.getCurrentAmount();
}
}
Account.java
package com.megabankcorp.records;
public class Account {
private int accountNumber;
private double currentAmount;
public Account (int accountNumber) {
this.accountNumber = accountNumber;
}
public int getAccountNumber() {
return accountNumber;
}
public double getCurrentAmount() {
return currentAmount;
}
public void setCurrentAmount(double amount) {
this.currentAmount = amount;
}
}
Please suggest the best practice possible for this problem. Any suggestions and ideas are welcome.
double
, instead useBigDecimal
. \$\endgroup\$ – esote Jul 7 '18 at 2:01