I need to modify addAccount method so that it has another parameter of AccountTy
ID: 643516 • Letter: I
Question
I need to modify addAccount method so that it has another parameter of AccountType and then call createAccount on the customer passing the accountType as an argument
here is my code that I have so far....
public class Bank {
int customerCount;
Customer customers[];
public Bank() {
this.customerCount = 0;
this.customers = new Customer[5];
}
public TransactionResult addCustomer(String firstName, String lastName) {
if (this.customerCount < 5) {
this.customers[this.customerCount] = new Customer(firstName,
lastName);
this.customerCount++;
return new TransactionResult(true,
"New Customer added successfully");
} else
return new TransactionResult(false, "Maximum customers reached");
}
public TransactionResult addAccount(int customerIndex) {
if (customerIndex >= 0 && customerIndex < this.customerCount) {
return this.customers[customerIndex].createAccount();
} else
return new TransactionResult(false, "Invalid customer index");
}
public String customerList() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < this.customerCount; i++) {
sb.append(this.customers[i].details());
sb.append(System.lineSeparator());
}
if (this.customerCount == 0)
sb.append("No customers to List");
return new String(sb);
}
public String customerDetails(int index) {
if (index < 0 || index > this.customerCount) {
return "Invalid index";
} else {
StringBuilder sb = new StringBuilder(index);
sb.append(System.lineSeparator());
sb.append(this.customers[index].details());
return new String(sb);
}
}
public TransactionResult deposit(int customerIndex, int accountIndex,
double amount) {
if (customerIndex >= 0 && customerIndex <= this.customerCount) {
return this.customers[customerIndex].deposit(amount, accountIndex);
} else
return new TransactionResult(false, "Invalid Customer index");
}
public TransactionResult withdraw(int customerIndex, int accountIndex,
double amount) {
if (customerIndex >= 0 && customerIndex <= this.customerCount) {
return this.customers[customerIndex].withdraw(amount, accountIndex);
} else
return new TransactionResult(false, "Invalid Customer index");
}
public void applyInterest() {
for (int i = 0; i < this.customerCount; i++) {
this.customers[i].applyInterest();
}
}
Explanation / Answer
//AccountType.java
//Create an enumeration class AccountType
public enum AccountType
{
//set values for AccountType class
SAVINGS_ACCOUNT,CURRENT_ACCOUNT,BUSINESS_ACCOUNT;
}
----------------------------------------------------------------------------------------------------------------
//Customer.java
public class Customer
{
private String firstName;
private String lastName;
//instance variable AccountType
private AccountType accountType;
public Customer(String firstName, String lastName)
{
this.firstName=firstName;
this.lastName=lastName;
}
//The method createAccount accepts the AccountType value
//and add account type to customer class
public TransactionResult createAccount(AccountType accountType)
{
//check if the accountType is existed in AccountType
//enum class ,otherwise returns account creation is failed
if((accountType==AccountType.SAVINGS_ACCOUNT )||
(accountType==AccountType.CURRENT_ACCOUNT)||
(accountType==AccountType.BUSINESS_ACCOUNT ))
{
this.accountType=accountType;
return new TransactionResult(true, "Account added successfully");
}
else
return new TransactionResult(true, "Account creation failed");
}
public TransactionResult deposit(double amount, int accountIndex)
{
return null;
}
public TransactionResult withdraw(double amount, int accountIndex) {
// TODO Auto-generated method stub
return null;
}
public void applyInterest() {
// TODO Auto-generated method stub
}
//Returns the string representation of Customer class object
//with accountType enumeration value
public String details()
{
return "Name : "+firstName+" "+lastName+
" Account Type :"+accountType;
}
}
----------------------------------------------------------------------------------------------------------------
//TransactionResult.java
public class TransactionResult
{
//instance variables
private boolean value;
private String message;
//constructor
public TransactionResult(boolean value, String message)
{
this.value=value;
this.message=message;
}
//String representation of TransactionResult object
@Override
public String toString()
{
return message+" : "+value;
}
}
----------------------------------------------------------------------------------------------------------------
//Bank.java
public class Bank
{
private int customerCount;
private Customer customers[];
//Add AccountType to Bank class
private AccountType accountType;
public Bank()
{
this.customerCount = 0;
this.customers = new Customer[5];
}
public TransactionResult addCustomer(String firstName, String lastName)
{
if (this.customerCount < 5)
{
customers[customerCount] =
new Customer(firstName,lastName);
customerCount++;
return new TransactionResult(true,"New Customer added successfully");
}
else
return new TransactionResult(false, "Maximum customers reached");
}
//The method addAccount that accepts the customer index and AccountType
//type as input parameters and
//calls the createAccount with accountType as parameter
//on customer class with index customerIndex and creates a new
//account for account Type
public TransactionResult addAccount(int customerIndex, AccountType accountType)
{
if (customerIndex >= 0 && customerIndex < this.customerCount)
{
return customers[customerIndex].createAccount(accountType);
}
else
return new TransactionResult(false, "Invalid customer index");
}
public String customerList()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < customerCount; i++)
{
sb.append(customers[i].details());
sb.append(System.lineSeparator());
}
if (customerCount == 0)
sb.append("No customers to List");
return new String(sb);
}
public String customerDetails(int index)
{
if (index < 0 || index > customerCount)
{
return "Invalid index";
}
else
{
StringBuilder sb = new StringBuilder(index);
sb.append(System.lineSeparator());
sb.append(customers[index].details());
return new String(sb);
}
}
public TransactionResult deposit(int customerIndex, int accountIndex,
double amount)
{
if (customerIndex >= 0 && customerIndex <= this.customerCount)
{
return customers[customerIndex].deposit(amount, accountIndex);
} else
return new TransactionResult(false, "Invalid Customer index");
}
public TransactionResult withdraw(int customerIndex, int accountIndex,
double amount) {
if (customerIndex >= 0 && customerIndex <= this.customerCount)
{
return this.customers[customerIndex].withdraw(amount, accountIndex);
} else
return new TransactionResult(false, "Invalid Customer index");
}
public void applyInterest()
{
for (int i = 0; i < this.customerCount; i++)
{
this.customers[i].applyInterest();
}
}
}
----------------------------------------------------------------------------------------------------------------
//Tester class
/**
* Test java program that tests the Bank
* class calls the method addAccount
* with customer index and type of account
* AccountType as input parameters
* and display the customer details
* by calling the method customerDetails
* with customer index.
* */
//TestBank.java
public class TestBank
{
public static void main(String[] args)
{
Bank bank=new Bank();
System.out.println(bank.addCustomer("John", "Dietel"));
bank.addAccount(0, AccountType.SAVINGS_ACCOUNT);
System.out.println(bank.addCustomer("Samantha", "Brandi"));
bank.addAccount(1, AccountType.CURRENT_ACCOUNT);
System.out.println(bank.addCustomer("Shoe", "Ferrari"));
bank.addAccount(2, AccountType.BUSINESS_ACCOUNT);
System.out.println(bank.customerDetails(0));
System.out.println(bank.customerDetails(1));
System.out.println(bank.customerDetails(2));
}
}
------------------------------------------------------------------------------------------------------------------
Sample output:
New Customer added successfully : true
New Customer added successfully : true
New Customer added successfully : true
Name : John Dietel
Account Type :SAVINGS_ACCOUNT
Name : Samantha Brandi
Account Type :CURRENT_ACCOUNT
Name : Shoe Ferrari
Account Type :BUSINESS_ACCOUNT
Note : Some classes and methods were not provided in the question .Assumed to be such that to complete the program.
Hope this helps you