Please review my code for the problem statement below. Many Thanks.
You job is to create a simple banking application.
There should be a Bank class:
- It should have an
ArrayList
ofBranche
s- Each Branch should have an
ArrayList
ofCustomer
s- The Customer class should have an
ArrayList
ofDouble
s (transactions)Customer:
- Name, and the
ArrayList
ofDouble
s.Branch:
- Need to be able to add a new customer and initial transaction amount.
- Also needs to add additional transactions for that customer/branch
Bank:
- Add a new branch
- Add a customer to that branch with initial transaction
- Add a transaction for an existing customer for that branch
Show a list of customers for a particular branch and optionally a list of their transactions Demonstration autoboxing and unboxing in your code.
import java.lang.Double;
import java.util.ArrayList;
public class Customer{
private String name;
private ArrayList<Double> transactions = new ArrayList<>();
public Customer(String name,double initialTransaction){
this.name = name;
this.transactions.add(Double.valueOf(initialTransaction));
}
public String getCustomerName(){
return this.name;
}
public Double getInitialTransaction(){
return this.transactions.get(0);
}
public void addTransaction(double transaction){
this.transactions.add(Double.valueOf(transaction));
}
public String getCustomerDetails(){
return getCustomerName()+"\t\t\t"+transactions;
}
}
import java.lang.Double;
import java.util.ArrayList;
public class Branch{
private String name;
private ArrayList<Customer> customers;
public Branch(String name){
this.name = name;
this.customers = new ArrayList<>();
}
public String getBranchName(){
return this.name;
}
public void addCustomer(Customer customer){
if(customer!=null){
if(!searchCustomer(customer)){
this.customers.add(customer);
//System.out.println("Customer with name "+customer.getCustomerName()+" added to branch "+getBranchName());
}else{
//System.out.println("Customer with name "+customer.getCustomerName()+" already present in branch "+getBranchName());
}
}else{
//System.out.println("Customer with null values entered!!!");
}
}
public void addTransaction(Customer customer,double transaction){
if(customer!=null || transaction<=0.0){
if(searchCustomer(customer)){
this.customers.get(this.customers.indexOf(customer)).addTransaction(transaction);
//System.out.println("Transaction of "+transaction+" added in account of Customer with name "+customer.getCustomerName());
}else{
//System.out.println("Customer with name "+customer.getCustomerName()+" not present in branch "+getBranchName()+". Please add the customer.");
}
}else{
//System.out.println("Customer with null values or no transaction entered!!!");
}
}
public void printCustomerList(){
System.out.println("Customer Name \t\t Transactions");
for(Customer cx:customers){
System.out.println(cx.getCustomerDetails());
}
}
public boolean searchCustomer(Customer customer){
for(int i=0;i<this.customers.size();i++){
if(this.customers.get(i).equals(customer)){
return true;
}
}
return false;
}
}
import java.lang.Double;
import java.util.ArrayList;
public class Bank{
private String name;
private ArrayList<Branch> branches;
public Bank(String name){
this.name = name;
this.branches = new ArrayList<>();
}
public String getBankName(){
return this.name;
}
public void addBranch(Branch branch,Customer customer){
if(branch!=null){
if(!searchBranch(branch)){
branch.addCustomer(customer);
this.branches.add(branch);
System.out.println("Customer with name "+customer.getCustomerName()+" with an initialTransaction of "+customer.getInitialTransaction().doubleValue()+" is added to Branch with name "+branch.getBranchName()+" of bank "+getBankName());
}else{
System.out.println("Branch with name "+branch.getBranchName()+" already present");
}
}else{
System.out.println("Branch with null values entered!!!");
}
}
public void addTransaction(Branch branch, Customer customer,double transaction){
if(branch!=null || customer!=null){
if(searchBranch(branch) && branch.searchCustomer(customer)){
branch.addTransaction(customer,transaction);
System.out.println("Transaction of "+transaction+" added in account of Customer with name "+customer.getCustomerName()+" under branch "+branch.getBranchName()+" of bank "+getBankName());
}else{
System.out.println("Customer with name "+customer.getCustomerName()+" not present in branch "+branch.getBranchName()+". Please add the customer.");
}
}else{
System.out.println("Branch or Customer with null values entered!!!");
}
}
public void printCustomersOfBranch(Branch branch){
if(branch!=null){
if(searchBranch(branch)){
branch.printCustomerList();
}
}else{
System.out.println("Branch with null values entered!!!");
}
}
private boolean searchBranch(Branch branch){
for(int i=0;i<branches.size();i++){
if(this.branches.get(i).equals(branch)){
return true;
}
}
return false;
}
}
public class BankTest{
public static void main(String[] args){
Bank pnb = new Bank("Punjab National Bank");
Branch peachtree = new Branch("Peach Tree");
Branch rodeodrive = new Branch("Rodeo Drive");
Branch goodearth = new Branch("Good Earth");
Customer harsh = new Customer("Harsh",0.5);
Customer nidhi = new Customer("Nidhi",600.75);
Customer yuv = new Customer("Yuv",1785.95);
pnb.addBranch(peachtree,harsh);
pnb.addBranch(rodeodrive,nidhi);
pnb.addBranch(goodearth,yuv);
pnb.printCustomersOfBranch(peachtree);
pnb.printCustomersOfBranch(rodeodrive);
pnb.printCustomersOfBranch(goodearth);
pnb.addTransaction(peachtree,harsh,500.60);
pnb.addTransaction(rodeodrive,nidhi,150000.70);
pnb.addTransaction(goodearth,yuv,121798.12);
pnb.printCustomersOfBranch(peachtree);
pnb.printCustomersOfBranch(rodeodrive);
pnb.printCustomersOfBranch(goodearth);
}
}
java.lang.Double
is unnecessary, all ofjava.lang.*
is imported by default. \$\endgroup\$