0

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).

Tom
  • 16,842
  • 17
  • 45
  • 54
Abdelillah
  • 79
  • 1
  • 14
  • BTW *public int indexOf(Object o) Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.* how have you implemented `equals` – Scary Wombat Jun 20 '19 at 00:23
  • My expectation is that they *haven't* overwritten `equals`. However, this means that `indexOf` does a `==` check, which is more strict – EDToaster Jun 20 '19 at 00:33
  • @JClassic Agree with what you are saying, but I was banking on a badly implemented `equals` – Scary Wombat Jun 20 '19 at 00:57
  • You showed us a lot of code we don't need, while failing to post the code we do need to see. As the Javadoc for [`ArrayList::indexOf`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/ArrayList.html#indexOf(java.lang.Object)) explains, your override of `Object::equals` via [`Objects.equals`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Objects.html#compare(T,T,java.util.Comparator)) the mechanism by which the list searches for your object. Your `equals` method is likely flawed, but you don't show it so we don't know for certain. – Basil Bourque Jun 20 '19 at 03:42
  • Always **search Stack Overflow** thoroughly before posting. Comparing objects for equality has been covered countless times already. – Basil Bourque Jun 20 '19 at 03:43

1 Answers1

0

I' found a mistake in my code so the problem was solved. In a query method I wrote this.branchesArrayList.get(0); instead of using position as a parameter.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Abdelillah
  • 79
  • 1
  • 14