I learn Java, now I'm at ArrayList, I wrote some methods about banking, when I want to add a new customer at a specific branch, the position always return 0. This is the source code of the methods that I'm talking about:
The instances in Main class :
public static Scanner scanner = new Scanner(System.in); public static Bank newBank = new Bank("BMCE");
Main class (static method to add a new customer):
public static void addNewCostumer(){ System.out.println("Choose a branch :"); String branchChoosed = scanner.nextLine(); Branches branches = newBank.queryBranch(branchChoosed); if(branches == null){ System.out.println("There are a problem, or you are entered a wrong name of branch"); } else{ System.out.println("Enter the name costumer :"); String nameOfCostumer = scanner.nextLine(); System.out.println("Enter the transaction number :"); double transactions = scanner.nextDouble(); scanner.nextLine(); if(newBank.addNewCostumer(branches,nameOfCostumer,transactions)){ System.out.println("The costumer was created in branch name :"+branches.getNameOfBranch()); }else{ System.out.println("Sorry you dindn't create a costumer in "+branches.getNameOfBranch()+" Try again please :)"); } }
The instances in Bank class:
private String name; private ArrayList<Branches> branchesArrayList = new ArrayList<>();
Bank class:
public boolean addNewCostumer(Branches nameOfExistingBranch,String nameOfNewCostumer,double newTransaction){ int position = findBranch(nameOfExistingBranch); if(position<0){ System.out.println("There is not branch with this name"); return false; } else{ if(this.branchesArrayList.get(position).findCostumer(nameOfNewCostumer)>=0){ System.out.println("You have already an existing costumer with that name"); return false; } else{ this.branchesArrayList.get(position).addNewCostumer(nameOfNewCostumer,newTransaction); return true; } } }
The method that finds branches:
public int findBranch(Branches branches){ int position = this.branchesArrayList.indexOf(branches); if(position>=0){ return position; } else return -1; }
The inputs :
0-Mdiq
1-Casa
2-Rabat
When I add a new customer to Rabat or Casa Branch the customer always records in Mdiq (Position 0).