I’m developing a application that will borrow a car to the user, user can choose from the fleet available. Unfortunately when registration number is typed in and name of the borrower is given, program assigns that person to the first entry in arrayList that has been added. I’m not sure how to get program to assign borrower to the item with given registration number. Can anyone point out the mistake I’m making?
Method that sets borrower:
public void askBorrower(){
boolean loop = false;
while(!loop){
String bName = Console.askString("Borrower's Name: ");
while(bName.isEmpty()){
System.out.println("Borrower's name cannot be empty!");
bName = Console.askString("Borrower's Name: ");
}
while(!bName.matches("[a-zA-Z]+")){
System.out.println("Letters only allowed!");
bName = Console.askString("Borrower's Name: ");
}
setBorrower(bName);
loop = true;
boolean newAvailability = false;
setAvailability(newAvailability);
System.out.println("You have successfully borrowed a car.");
}
}
In main class switch triggering borrowing process:
case 4:
System.out.println("Borrowing a car:");
System.out.println("");
sCar = new StaffCar();
sCar.askRegNo();
sCar = (StaffCar)fleet2.find(sCar.getRegNo());
if(sCar == null){
System.out.println("Car not in the fleet, please try again");
}
else if(sCar.availability == false){
System.out.println("Car is out on loan currently");
}
else{
sCar.askBorrower();
}
break;
and find method:
public StaffCar find(String regNo){
int index = regNo.indexOf(regNo);
if(index == -1){
return null;
}
else{
return fleet2.get(index);
}
}
I would very much appreciate any comments or suggestions.
regNo.indexOf(regNo)will always return 0. You are looking for a string inside itself.