Answer to Question #234893 in Java | JSP | JSF for Momo

Question #234893
Question 5 (Marks: 40)
Design a Java application that will store three different vehicle details for a local car dealership.
The vehicle details must be stored in parallel arrays for the Vehicle Identification Number (VIN),
Vehicle Manufacturer and Vehicle Price. Use the following data as an example when populating
the arrays.
VIN Manufacturer Vehicle Price
112367 AUDI R 180 000
212367 BMW R 170 000
312367 VOLVO R 210 000
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 (0) to exit the application. Make use of JOptionPanes
for input and output. Create a loadArray() method that will store the vehicle details into the three
parallel arrays.
If the user enters a one (1) provide functionality for a vehicle search. Create a searchArray()
method and write the code to enable the user to search for a specific vehicle. If the VIN is found in
the array, display the vehicle details.
1
Expert's answer
2021-09-08T14:16:49-0400
import javax.swing.JOptionPane;


public class CarDealership {


	static String[] VIN = new String[3];
	static String[] manufacturer = new String[3];
	static float[] vehiclePrice = new float[3];


	/**
	 * load Array
	 */
	static void loadArray() {
		VIN[0] = "112367";
		VIN[1] = "212367";
		VIN[2] = "312367";


		manufacturer[0] = "AUDI";
		manufacturer[1] = "BMW";
		manufacturer[2] = "VOLVO";


		vehiclePrice[0] = 180000;
		vehiclePrice[1] = 170000;
		vehiclePrice[2] = 210000;
	}


	/**
	 * Main method
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		loadArray();
		int ch = -1;
		while (ch != 0) {
			ch = Integer.parseInt(JOptionPane.showInputDialog(null,
					"Enter (1) to search for vehicle details.\nEnter (2) to display all the vehicle details.\nEnter (0) to exit. "));
			if (ch == 1) {
				searchArray();
			}
			if (ch == 2) {
				String allVehicleDetails = "";
				for (int i = 0; i < 3; i++) {
					allVehicleDetails += "VIN: " + VIN[i] + "\nManufacturer: " + manufacturer[i] + "\nVehicle Price: R "
							+ vehiclePrice[i]+"\n";


				}
				JOptionPane.showMessageDialog(null, allVehicleDetails);
			}
		}
	}


	static void searchArray() {
		String vehicleVIN = JOptionPane.showInputDialog(null, "Enter in the Vehicle VIN");
		int index = -1;
		for (int i = 0; i < 3; i++) {
			if (VIN[i].compareToIgnoreCase(vehicleVIN) == 0) {
				index = i;
				break;
			}
		}
		if (index != -1) {
			JOptionPane.showMessageDialog(null, "VIN: " + VIN[index] + "\nManufacturer: " + manufacturer[index]
					+ "\nVehicle Price: R " + vehiclePrice[index]);
		} else {
			JOptionPane.showMessageDialog(null, "The vehicle you entered cannot be found.");
		}


	}


}

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