-2

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!

1 Answers1

0

Your problem seems to be that you are creating a new CarManager everytime, and using recursive to go inside a new CarManager that you created in your addCar() method. it looks to me that you only need one CarManager and all the Car instances get added to your ArrayList<Car> that is a field in your Car Manager.

i assumed this because your Car class have fields :

private String carId;
private String driver;
private int sizeCategory;

and the same fields are being repeated in your CarManager for no use basically.

I suggest to use a while loop to check for user input, such as :

public void addCar(Scanner scanner) { // passing on the scanner
   while (true) {
        // ask the user for inputs
        String carId = scanner.nextLine();
        String driver = scanner.nextLine();
        int sizeCatagory = Integer.parseInt(scanner.nextLine());
         // ask user for confirmation
        if (scanner.nextLine().equals("1") {
            continue; 
        } else {
            carList.add(new Car(carId,driver,sizeCategory));
            break;
        }
    }

}
Emad Ali
  • 107
  • 7
  • Thank you very much for your answer! How do I now continue? With the break the program would end, but I need to be able to list the car later on and also update/ edit the arraylist. I tried CarManager.handleCars() and then to list the arraylist but that doesn't work. – mbg 2310 Nov 27 '21 at 22:31
  • The `break` does not exit the method, it only exit the`while loop`. the `loop` is only there to see if the user satisfied with the `inputs` or want to repeat them again. after `break` exits the loop, the method still continues to other functions you put after the loop. if we would use `return` in the other hand instead of `break` now that would quit the whole method. – Emad Ali Nov 27 '21 at 22:35
  • Thank you again for your time and sorry for my unprecise language.. I didn't mean that break would end the whole program but rather that i need something after that break. My question would be better formulated as: How do I access the just created Car of the arraylist from another method (listCars) in this class without creating a new CarManager – mbg 2310 Nov 27 '21 at 23:10
  • You do need to create a `CarManager`, but only once in your main, this `CarManager` will have all the cars you added to him, since he is holding the list of the cars you added. `CarManager` have 2 methods, one that will print all the cars, and one that will `return` to you a `list` of all the cars. `CarManager manager = new CarManager();` `manager.addCar(scanner);` repeat till you add all the cars you want, pass your scanner to the method. `manager.listCar();` to print all the cars that the manager have or `manager.getCarList();` if you want the list to do something with it. – Emad Ali Nov 27 '21 at 23:17
  • to clarify, you can create multiple `CarManagers` if you would like to, `CarManager secondManager = new CarManager();` for example that will have a list of different set of cars that is separated from the first manager. – Emad Ali Nov 27 '21 at 23:21