Home > Software engineering >  How do I display the updated element information when there is more than one element in the arrayLis
How do I display the updated element information when there is more than one element in the arrayLis

Time:11-07

I have a program to update vehicle inventory. I call the updateVehicle()...it should loop through the arrayList of vehicles to look for a match based on what the user input. In the if statement, if a match is found, update the vehicle in the arrayList with what the user input, call the displayCurrentVehicleEntry() and display the updated details to console. The code works and will update the vehicle. However, if there is more than one vehicle in the arrayList, it will update it correctly, but not display the details of the updated vehicle (it displays the info for the last element in the arrayList). In the displayCurrentVehicleEntry() it will grab the last element and display the details, which works correctly for the addVehicle(). I'm not sure how to get that to work for the updateVehicle().

public void updateVehicle(String makeCurrent, String modelCurrent, String colorCurrent, int yearCurrent, int mileageCurrent,
            String makeUpdated, String modelUpdated, String colorUpdated, int yearUpdated, int mileageUpdated) {
        try {
            boolean found = false;
            for (int i = 0; i < listOfVehicles.size(); i  ) {
                AutoInv vehicle = listOfVehicles.get(i);
                if (vehicle.getMake().equalsIgnoreCase(makeCurrent) 
                        && vehicle.getModel().equalsIgnoreCase(modelCurrent)
                        && vehicle.getColor().equalsIgnoreCase(colorCurrent) 
                        && vehicle.getYear() == yearCurrent
                        && vehicle.getMileage() == mileageCurrent) {
                    vehicle.setMake(makeUpdated);
                    vehicle.setModel(modelUpdated);
                    vehicle.setColor(colorUpdated);
                    vehicle.setYear(yearUpdated);
                    vehicle.setMileage(mileageUpdated);
                    System.out.println("\nVehicle updated successfully!\n");
                    displayCurrentVehicleEntry(); //FIXME not working rethink
                    found = true;
                }
            }
                if (!found) {
                    System.out.println("\nVehicle not found in inventory!");
                }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("Failure");
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        }
public void displayCurrentVehicleEntry() {
        try {
            AutoInv vehicle = listOfVehicles.get(listOfVehicles.size() - 1);
            System.out.println("Make: "   vehicle.getMake().toUpperCase());
            System.out.println("Model: "   vehicle.getModel().toUpperCase());
            System.out.println("Color: "   vehicle.getColor().toUpperCase());
            System.out.println("Year: "   vehicle.getYear());
            System.out.println("Mileage: "   vehicle.getMileage());
            System.out.println("");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("Failure");
            System.out.println(e.getMessage());
            e.printStackTrace();
        }   
    }
public void addVehicle(AutoInv vehicle) throws Exception{
        try {
            if (listOfVehicles.add(vehicle)) {
                System.out.println("\nFollowing vehicle added successfully:\n");
                displayCurrentVehicleEntry();
            }
            else {
                throw new Exception("\nFailed to add vehicle.");
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            //          e.printStackTrace();
        }
    }

CodePudding user response:

Modify the displayCurrentVehicleEntry() method, add a parameter to displayCurrentVehicleEntry() like this displayCurrentVehicleEntry(int index), and change AutoInv vehicle = listOfVehicles.get(listOfVehicles.size() - 1); to AutoInv vehicle = listOfVehicles.get(index);

in addVehicle, you can use displayCurrentVehicleEntry(listOfVehicles.size() - 1); and in updateVehicle you can use displayCurrentVehicleEntry(i); to select the vehicle you want to print

  •  Tags:  
  • java
  • Related