Answer to Question #239243 in Java | JSP | JSF for Princess

Question #239243

Q.1.8


The user must have the ability to search for a product. The user will select menu item two (2), which will prompt the user to enter a product code. If a valid product is found in the application, then display the product details to the user. If no valid product is found, display an error message to the user that the product cannot be located.


Sample Product Search Screenshot


Please enter the product code to search: 235


PRODUCT SEARCH RESULTS


PRODUCT CODE: A55


PRODUCT NAME: EliteBook


PRODUCT WARRANTY: laptop


PRODUCT CATEGORY: 2 years

PRODUCT PRICE: 15000


PRODUCT STOCK LEVELS: 3


PRODUCT SUPPLIER: IT_4_Africa


Enter (1) to launch menu or any other key to exit


Sample Invalid Product Screenshot


Please enter the product code to search: 1

The product cannot be located.

Invalid Product Enter (1) to launch menu or any other key to exit


1
Expert's answer
2021-09-20T05:48:29-0400
public class Product {
    private String code;
    private String name;
    private String warranty;
    private String category;
    private double price;
    private int stockLevel;
    private String supplier;

    public Product(String code, String name, String warranty, String category, double price,
                   int stockLevel, String supplier) {
        this.code = code;
        this.name = name;
        this.warranty = warranty;
        this.category = category;
        this.price = price;
        this.stockLevel = stockLevel;
        this.supplier = supplier;
    }

    public String getCode() {
        return code;
    }

    @Override
    public String toString() {
        return "PRODUCT CODE: " + code + "\nPRODUCT NAME: " + name +
                "\nPRODUCT WARRANTY: " + warranty + "\nPRODUCT CATEGORY: " + category +
                "\nPRODUCT PRICE: " + price + "\nPRODUCT STOCK LEVELS: " + stockLevel +
                "\nPRODUCT SUPPLIER: " + supplier;
    }
}


import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        ArrayList<Product> products = new ArrayList<>();
        products.add(new Product("A55", "EliteBook", "2 years",
                "laptop", 15000, 3, "IT_4_Africa"));
        products.add(new Product("BX15", "ViewSonic", "1 year",
                "monitor", 10000, 4, "IT_4_Africa"));
        products.add(new Product("GT35", "Precision", "6 months",
                "mouse", 8000, 10, "IT_4_Africa"));
        Scanner in = new Scanner(System.in);
        Product tmp;
        String choice;
        while (true) {
            tmp = null;
            System.out.println("2. Find product");
            choice = in.nextLine();
            if (choice.equals("2")) {
                System.out.print("Please enter the product code to search: ");
                String code = in.nextLine();
                for (Product product : products) {
                    if (product.getCode().equals(code)) {
                        tmp = product;
                        break;
                    }
                }
                if (tmp != null) {
                    System.out.println(tmp);
                } else {
                    System.out.println("The product cannot be located.");
                }
                System.out.println("Enter (1) to launch menu or any other key to exit");
                choice = in.nextLine();
                if (!choice.equals("1")) {
                    break;
                }
            }
        }

    }
}

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