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

Question #274375

Design an application that stores different types of products available at a local IT store and the corresponding product price. The product details must be stored in parallel arrays. Use the following data to populate the three (3) parrallel arrays in a loadArrau ()method.


Create a SearchArray () method and write the code to enable the user to search for a specific type of product and if the product ID is listed in the array, show the product details. If the product ID that the user searched for does not exist in the array, show a message to indicate that this product is not available.


Prompt the user to enter numbers 1,2 or any other key to exit the application.


The product ID , product name and the product price must be displayed in a message box if a valid product ID is entered by the user.


Implement user friendliness into the application by prompting the user if they would like to search for more products.



1
Expert's answer
2021-12-02T00:35:01-0500
import javax.swing.*;
import java.util.Scanner;

public class Main {
    private int[] ids;
    private String[] names;
    private double[] prices;

    public void loadArray() {
        ids = new int[]{0, 1, 2};
        names = new String[]{"Mouse", "Keyboard", "CPU"};
        prices = new double[]{10.5, 15.7, 50};
    }

    public void searchArray(int id) {
        for (int i = 0; i < ids.length; i++) {
            if (id == ids[i]) {
                JOptionPane.showMessageDialog(null,
                        ids[i] + " " + names[i] + " " + prices[i], "Success", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
        }
        JOptionPane.showMessageDialog(null,
                "This product is not available.", "Fail", JOptionPane.INFORMATION_MESSAGE);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Main main = new Main();
        main.loadArray();
        while (true) {
            System.out.print("Enter ID to search(-1 to exit): ");
            int input = in.nextInt();
            if (input == -1) {
                break;
            }
            main.searchArray(input);
        }
    }
}

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