Answer to Question #274377 in Java | JSP | JSF for doeboy

Question #274377

Design a java application that will store three different vehicle details for a local car dealership. The vihicle details must be stored in parallel arrays for the Vehicle identification Number (VIN), Vehicle Manufacturer and vehicle price.


Prompt the user with a menu system for searching or displaying the vehicles. The user must have the option to enter the numbers 1,2 or zero to exit the application. Make use of JOptionPanes for input and output. Create a loadArray () method that will store the vehicle details into thr three parrallel arrays.



If a user enters one provide functionality for a vehicle search. Create a searchArray () method and write code to enable the user to search for a specific vehicle. If the VIN is found in the array, display the Vehicle details.


If the VIN that the user searched for does not exist in the array, show a message to indicate that the vehicle is not found.



2 must display all vehicles

0 must exit




1
Expert's answer
2021-12-02T00:34:55-0500
import javax.swing.*;

public class Main {
    private int[] vins;
    private String[] manufactures;
    private double[] prices;

    public void loadArray() {
        vins = new int[]{0, 1, 2};
        manufactures = new String[]{"BMW", "Mercedes", "Bugatti"};
        prices = new double[]{10000, 15000, 50000};
    }

    public void searchArray(int vin) {
        for (int i = 0; i < vins.length; i++) {
            if (vin == vins[i]) {
                JOptionPane.showMessageDialog(null,
                        vins[i] + " " + manufactures[i] + " " + prices[i], "Success", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
        }
        JOptionPane.showMessageDialog(null,
                "The vehicle is not found.", "Fail", JOptionPane.INFORMATION_MESSAGE);
    }

    public void displayAll() {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < vins.length; i++) {
            builder.append(vins[i]).append(" ").append(manufactures[i]).append(" ").append(prices[i]).append('\n');
        }
        JOptionPane.showMessageDialog(null, builder, "Result", JOptionPane.INFORMATION_MESSAGE);
    }

    public static void main(String[] args) {
        Main main = new Main();
        main.loadArray();
        while (true) {
            String input = JOptionPane.showInputDialog(null,
                    "1. Search a vehicle by a VIN\n2. Display all vehicles\n0. Exit", "Make a choice", JOptionPane.INFORMATION_MESSAGE);

            switch (input) {
                case "1":
                    input = JOptionPane.showInputDialog(null,
                            "Enter the VIN", "Make a choice", JOptionPane.INFORMATION_MESSAGE);
                    main.searchArray(Integer.parseInt(input));
                    break;
                case "2":
                    main.displayAll();
                    break;
                case "0":
                    System.exit(0);
            }

        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS