I am writing a program in which I have to create a method that finds a CustomerAccount object by the registration number associated with it. I also need to use a custom exception for when the registration number can't be found in the ArrayList of CustomerAccount objects.
My method throws the exception when trying to search for an invalid registration number, but does not print anything when searching for a valid registration number.
I want the method to output all the attributes associated with the correct customer account.
Here's the method I have so far:
public CustomerAccount findCustomer(String regNum){
CustomerAccount correctAccount = new CustomerAccount("","",null,0);
for (int i = 0; i < this.customerAccounts.size(); i++) {
if (regNum.equals(customerAccounts.get(i).getTypeOfVehicle()
.getRegNum())) {
correctAccount = customerAccounts.get(i);
return correctAccount;
} else {
try {
throw new CustomerNotFoundException("Customer Not Found");
} catch (CustomerNotFoundException e) {
e.printStackTrace();
}
}
}
return correctAccount;
}
I'm a new programmer so I apologise if the answer is obvious but after quite a while scrolling through forums I still can't figure it out!