The error is that the customers has different ID, but in readCustomer object of ArrayList class the only ID that is saved is the ID of the last customer.
//reads customer data from register.txt file
//has 2 string as arguments
//returns a ArrayList object with all the customer with the same name
public ArrayList<Customer> readCustomer(String name,String surname){
Customer temp = new Customer();
//will hold all the customers with the same name and surname
ArrayList<Customer> readCustomer = new ArrayList<Customer>();
try{
FileInputStream fstream = new FileInputStream("register.txt");
BufferedReader fbr = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = fbr.readLine()) != null) {
String line[];
line = strLine.split(" ");
if(name.equals(line[1]) && surname.equals(line[2])){
temp.setCustomerID(Integer.parseInt(line[0]));
temp.setName(line[1]);
temp.setSurname(line[2]);
temp.setAddress(line[3]);
temp.setAge(Integer.parseInt(line[4]));
readCustomer.add(temp);
}
}
fbr.close();
}
catch(Exception ex){*emphasized text*
System.out.println(ex.getMessage());
}
return readCustomer;
}