Java codes in following methods 1-createCustomer, lookupCustomer, createAccount, lookupAccount, createTransaction (These methods are part of Class Bank)
Question:
Java codes in following methods
1-createCustomer, lookupCustomer, createAccount, lookupAccount, createTransaction (These methods are part of Class Bank)
2-addTransaction, printStatement (These methods are part of Class Account)
3-addAccount, printStatement (These methods are part of class Customer)
I have attached Class Bank, Account and Customer codes for reference.
/*Start of the Bank Class*/
import java.util.HashMap; import java.util.Map; import java.util.Date;
public class Bank {
private final Map customerMap; private final Map accountMap;
private static Bank bankInstance;
private Bank() { customerMap = new HashMap<>(); accountMap = new HashMap<>(); }
public static synchronized Bank getBankInstance() { if (bankInstance == null) { bankInstance = new Bank(); } return bankInstance; }
public synchronized Customer createCustomer(String customerName, String customerId, Date registrationDate) throws BankException {
/* Fill in the code and return the Customer. Exception if customerId already exists */
return null;
}
public synchronized Customer lookupCustomer(String customerId) throws BankException {
/* Fill in the code and return the Customer. Exception if customerId doesn't exist */
return null; }
public synchronized Account createAccount(String customerId, AccountType accountType, String accountId, Date openDate, int initialAmount) throws BankException {
/* Fill in the code and return either a CheckingAccount or a SavingsAccount. Exception for any other AccountType */
return null; }
public synchronized Account lookupAccount(String accountId) {
/* Fill in the code and return the Account. Exception if accountId doesn't exist */
return null; }
public synchronized void setJointOwner(String accountId, String primaryOwnerId, String jointOwnerId, Date jointOwnershipDate) {
Account account = this.lookupAccount(accountId); Customer primaryOwner = this.lookupCustomer(primaryOwnerId); Customer jointOwner = this.lookupCustomer(jointOwnerId);
if ((account != null) && (primaryOwner != null) && (jointOwner != null)) { if ( (account.getPrimaryOwner() == primaryOwner) && (account.getJointOwner() == null) ) { account.setJointOwner(jointOwner, jointOwnershipDate); } }
}
public synchronized void createTransaction(TransactionType transactionType, Date date, int amount, String customerId, String sourceAccountId, String destinationAccountId) {
/* Fill in the code - Based on the transactionType, invoke makeDeposit(...), or makeWithdrawal(...), or makeTransfer(...) - The destinationAccountId is only applicable if this is a Transfer request */ switch (transactionType) {
case Deposit -> {
break; } case Withdraw -> {
break; } case Transfer -> {
} } }
private synchronized void makeDeposit(Date date, int amount, String customerId, String accountId) { Customer customer = this.lookupCustomer(customerId); Account account = this.lookupAccount(accountId); new DepositTransaction(date, amount, customer, account); }
private synchronized void makeWithdrawal(Date date, int amount, String customerId, String accountId) { Customer customer = this.lookupCustomer(customerId); Account account = this.lookupAccount(accountId); if (account != null && ((account.getPrimaryOwner() == customer) || (account.getJointOwner() != null && account.getJointOwner() == customer)) ) { new WithdrawTransaction(date, amount, customer, account); } else new BankException("Customer is not owner or joint owner"); }
private synchronized void makeTransfer(Date date, int amount, String customerId, String fromAccountId, String toAccountId) { Customer customer = this.lookupCustomer(customerId); Account fromAccount = this.lookupAccount(fromAccountId); Account toAccount = this.lookupAccount(toAccountId); if (fromAccount != null && ((fromAccount.getPrimaryOwner() == customer) || (fromAccount.getJointOwner() != null && fromAccount.getJointOwner() == customer)) ){ new TransferTransaction(date, amount, customer, fromAccount, toAccount); } else new BankException("Customer is not owner or joint owner"); }
public synchronized void printStatement(String customerId, Date toDate) { Customer customer = this.lookupCustomer(customerId); if (customer != null) customer.printStatement(toDate); }
}
/*Start of the Bank Account*/
import java.util.Date; import java.util.List; import java.util.ArrayList;
public abstract class Account {
private final String accountId; private final Date openDate; private Date closeDate;
private final Customer primaryOwner; private Customer jointOwner;
private AccountStatus accountStatus;
private Date jointOwnershipDate;
private int currentBalance;
private final List transactionList;
public Account(Customer primaryOwner, String accountId, Date openDate) { this.accountId = accountId; this.openDate = openDate; this.accountStatus = AccountStatus.Open; this.currentBalance = 0; this.transactionList = new ArrayList<>(); this.primaryOwner = primaryOwner; primaryOwner.addAccount(this); }
public Customer getPrimaryOwner() { return this.primaryOwner; }
public String getAccountId() { return this.accountId; }
public Date getOpenDate() { return this.openDate; }
public AccountStatus getAccountStatus() { return this.accountStatus; }
public int getCurrentBalance() { return this.currentBalance; }
public void setJointOwner(Customer jointOwner, Date jointOwnershipDate) { this.jointOwner = jointOwner; this.jointOwnershipDate = jointOwnershipDate; jointOwner.addAccount(this); }
public Customer getJointOwner() { return this.jointOwner; }
public Date getJointOwnershipDate() { return this.jointOwnershipDate; }
protected synchronized void addTransaction(Transaction t) {
if (this.accountStatus == AccountStatus.Close) throw new BankException("Account " + this.getAccountId() + "closed... Transaction not allowed");
if (t instanceof DepositTransaction) {
/* Fill in the code - invoke the deposit method with the transaction amount, - add the transaction to the transactionList - set the ending balance of the transaction as the account's current balance - invoke the transaction's print method */
} else if (t instanceof WithdrawTransaction) {
/* Fill in the code - invoke the withdraw method with the transaction amount if it is less than or equal to the current balance, otherwise change the description of the transaction - add the transaction to the transactionList - set the ending balance of the transaction as the account's current balance - invoke the transaction's print method */
} else if (t instanceof TransferTransaction) {
TransferTransaction tr = (TransferTransaction) t;
if (tr.getToAccount().accountStatus == AccountStatus.Close) throw new BankException("Account " + this.getAccountId() + " closed... Transaction not allowed");
tr.setDescription("Transfer from " + this.getAccountId() + " to " + tr.getToAccount().getAccountId()); this.transactionList.add(tr); tr.getToAccount().transactionList.add(tr); tr.print(null); }
}
private synchronized void deposit(int amount) { this.currentBalance += amount; }
private synchronized void withdraw(int amount) { this.currentBalance -= amount; }
public synchronized void closeAccount(Date closeDate) { this.accountStatus = AccountStatus.Close; this.closeDate = closeDate; }
public void printStatement(Date toDate) {
System.out.println(" tTransactions for Account " + this.accountId + " Primary Owner: " + this.primaryOwner.getName() + " ");
/* Fill in the code to iterate over the transactionList and invoke the print method for each transaction */
}
}
/*Start of the Bank Customer*/
import java.text.DateFormat; import java.util.List; import java.util.ArrayList; import java.util.Date;
public class Customer { private final String name; private final String customerId; private final Date registrationDate; private final List accountList;
public Customer(String name, String customerId, Date registrationDate) { this.name = name; this.customerId = customerId; this.registrationDate = registrationDate; this.accountList = new ArrayList<>(); }
public String getName() { return this.name; }
public String getCustomerId() { return this.customerId; }
public Date getRegistrationDate() { return this.registrationDate; }
public void addAccount(Account account) {
/* Fill in the code to add the account to the customer's accountList */
}
public void printStatement(Date toDate) {
System.out.println(" BEGIN ACCOUNT STATEMENT - " + this.getName() + " - " + DateFormat.getDateInstance().format(toDate));
/* Fill in the code to iterate over the customer's accountList and invoke printStatement for each account */
System.out.println(" END ACCOUNT STATEMENT "); }
/** * @return the accountList */ public List getAccountList() { return accountList; }
}
Organic Chemistry structure and function
ISBN: 978-1429204941
6th edition
Authors: K. Peter C. Vollhardt, Neil E. Schore