I am creating a program for uni that is about registering cars. I have an object class for the cars and an array list to keep them all in however when the addCar I made gets used and I then try to print it it will print the last one I entered for how ever many cars there should be
I have tried lifting the code straight into the main body and its the same and I've tried various different small changes like the way I'm comparing etc.
Here is the AddCar function that I keep in the Car class file
public static ArrayList<Car> addCar(ArrayList<String> Makes, Integer CarCounter, ArrayList<Car> Cars) {
Integer Select;
boolean Equal = false;
for (int i = 0; i < Makes.size(); i++) {
System.out.println(i + ": " + Makes.get(i));
}
Scanner choice = new Scanner(System.in);
{
System.out.println("Enter Numeric Choice: ");
while (!choice.hasNextInt()) {
System.out.println("Error Please Enter Numeric Choice Again: ");
choice.next();
}
Select = choice.nextInt();
}
if (Select >= Makes.size()) {
System.out.println("No records exist (Number entered too large)");
} else {
for (int i = 0; i < Makes.size(); i++) {
if (Equal = Makes.get(i).equals(Makes.get(Select))) {
break;
}
}
if (Equal == true) {
Car newCar;
Make = Makes.get(Select);
Reg = Input.getString("What is the registration: ");
Model = Input.getString("What is the Model: ");
Colour = Input.getString("What is the Colour: ");
newCar = new Car();
newCar.setMake(Make);
newCar.setReg(Reg);
newCar.setModel(Model);
newCar.setColour(Colour);
Cars.add(CarCounter, newCar);
} else {
System.out.println("Make is unavailable Please Try Again");
}
}
return Cars;
}
Here is the line that declares the arraylist in the main body of the main file
ArrayList<Car> Cars = new ArrayList<Car>();
Heres the line that calls the function for one of the cases in my switch case menu
Cars = Car.addCar(Makes, CarCounter, Cars);
If for example I set the first car as fiat,L4QWS,Punto,Silver
and the second car as Ferrari,4RE33,LaFerrari,Red
, then it should be:
Car 1:
Make: fiat
Registration: L4QWS
Model: Punto
Colour: Silver
Car 2:
Make: Ferrari
Registration: 4RE33
Model: LaFerrari
Colour: Red
However, is actually comes out as:
Car 1:
Make: Ferrari
Registration: 4RE33
Model: LaFerrari
Colour: Red
Car 2:
Make: Ferrari
Registration: 4RE33
Model: LaFerrari
Colour: Red