How do I display the updated element information when there is more than one element in the arrayList?

cleatus

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();
        }
    }
banzhe

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to stop click event from creating more than one element?

How to use jQuery :not selector for more than one element at a time

How do I pass in more than one parameter to the onChange action for the select element in Ember2.3

how can I get more than one element from json using jsonpath?

How to have more than one element change behaviour when an element is clicked

Kotlin reduce how to apply operation on more than one element

How do I call more than one function for each case when using a "when" statement in Kotlin?

How to achieve grid row with more than one element vertically?

How do I append more than one child element to a parent element Javascript

How to return more than one element list?

How can I toggle more than one element on a single click in jQuery?

How to get more than one of this element on one page (w/ codepen)

Priority when more than one element use the same class

How do I find the middle element of an ArrayList?

How to apply javascript to more than one element in html document?

how do I change just one character in a string when there more than one of that character?

How to add more than one element to a custom Menu in TinyMCE 4 and display its content on the editor canvas when clicked?

How can I order by more than one element with an angular ng-repeat?

shared element transaction not working with recyclerview when more than one element

how do I fill na values when I unstack more than one level at a time

How do I check if an element occurs more than twice in an array, subject to a string pattern?

pdfmake how to add more than one element in a column

How to add more than one attributes to an element dynamically in vuejs

In beautifulsoup4, when scraping a website purely based off of an element and the text within, how do you return more than one result?

How can I print more than one line of words/phrases in a Multiline PySimpleGUI element?

how to select more than one element with almost the same xpath?

(C++) How do I display an error when more than one "." is used in a calcualtor?

How do you hide an element when another element has a left position of more than 0?

How to move or copy more than one div element into another div?