I'm pretty new to java and could not find a question answering my problem particulary.
I initialized an arraylist as instance variable and in one method I edit this arraylist which also works, but I'm having trouble storing this value in the instance variable and then reading it in a different method because then it appears empty again. I tried using getter and setter but that didn't really work even though I suppose that that is part of the solution...
Here some relevant snippets of my code:
public class CarManager {
ArrayList<Car> carList = new ArrayList<>();
String carId ="";
String driver= "";
int sizeCategory= 0;
Car myCar= new Car (carId,driver,sizeCategory);`
public void addCar () {
CarManager myCarManager= new CarManager();
Car car1 = new Car(carId,driver,sizeCategory);
carId= car1.getCarId(carId);
driver= car1.getDriver (driver);
sizeCategory= car1.getSizeCategory (sizeCategory);
System.out.println("You entered the following data:");
System.out.println("\ncar ID: "+ carId);
System.out.println("driver's name:" +driver);
System.out.println("size category: "+sizeCategory);
System.out.println("\nIf you are okay with this, press 0 to save the data and return to the CARS MENU");
System.out.println("\nIf you made a mistake, press 1 to enter the data again");
Scanner afterAddCar= new Scanner(System.in);
String choice1;
choice1= afterAddCar.next().trim();
if ("1".equals (choice1)) {
myCarManager.addCar();
}
if ("0".equals (choice1)) {
Car returnedCar= new Car (carId,driver,sizeCategory);
carList.add(returnedCar);
String list = carList.toString();
System.out.println(list);
myCarManager.setCarList(carList);
ArrayList<Car> afterAdd= carList;
myCarManager.handleCars();
}
}
public ArrayList<Car> getCarList() {
return carList;
}
public void setCarList(ArrayList<Car> carList) {
this.carList = carList;
}
public void listCars () {
String list = carList.toString();
System.out.println(list);
}
I have a second class called Car which I'll also post here for better understanding:
public class Car {
private String carId;
private String driver;
private int sizeCategory; // value from 1-3 indicating the size of the car
public Car () {}
public Car (String carId,String driver,int sizeCategory){
this.carId= carId;
this.driver= driver;
this.sizeCategory= sizeCategory;
}
public String getCarId(String carId) {
Scanner keyboard = new Scanner(System.in);
System.out.println("\nFirst enter the car ID: " + carId);
while (carId.length() < 6) {
System.out.println("Please enter an ID that has at least 6 characters");
carId = keyboard.next();
}
return carId;
}
public void setCarId(String carId) {
this.carId = carId;
}
public String getDriver(String driver) {
Scanner keyboard = new Scanner(System.in);
System.out.println("\nNext enter the driver's name:" + driver);
driver= driver.trim();
while (driver.length() < 2) {
System.out.println("Please enter a name that has at least 2 characters");
driver = keyboard.next();
driver= driver.trim();
}
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public int getSizeCategory(int sizeCategory) {
Scanner keyboard = new Scanner(System.in);
String input="";
System.out.println("\nNow the size category: ");
while (sizeCategory <1 || sizeCategory >3) {
System.out.println("The size category has to be between one and three");
input = keyboard.next().trim();
try {
sizeCategory = Integer.parseInt(input);
} catch (Exception e) {
sizeCategory = 0;
}
}
return sizeCategory;
}
public void setSizeCategory(int sizeCategory) {
this.sizeCategory = sizeCategory;
}
Now I know that there is probably more than one thing that could be improved here, but I'm mostly interested in solving the problem with the listing of the updated arraylist.
Thanks a lot in advance!