Answer to Question #239831 in Java | JSP | JSF for sihle

Question #239831

Extreme IT Products is a local supplier that specializes in the sales of the latest Information Technology hardware devices. The business has recently opened an outlet in the town you reside and has hired the software development house you work for to design a Java application to manage their products.


1
Expert's answer
2021-09-20T23:53:12-0400

The answer provided works as management system for products in Extreme It products hardware shop. You can add new products, you can update products, you can delete products and also you can view a report of the products available. You can perform multiple operations until you decide to exit the program.



import java.util.ArrayList;
import java.util.Scanner;
// class product having attributes of any product in Extreme IT products
class Product {

    private String code;
    private String description;
    private double price;

    // the constructor
    public Product() {
        this.code = "";
        this.description = "";
        this.price = 0;
    }

    public Product(String code, String description, double price) {
        this.code = code;
        this.description = description;
        this.price = price;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Product code: " + this.code + "\n" + "Product description: " + this.description + "\n"
                + "Product price: " + this.price + "\n";
    }
}

public class Main {
    private static int getProductByCode(ArrayList<Product> products, String code) {

        for (int i = 0; i < products.size(); i++) {
            if (products.get(i).getCode().compareTo(code) == 0) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Product> products = new ArrayList<Product>();
        String code;
        String description;
        double price;
        int terminate_condition = 1;
        System.out.println("Extreme IT Products  Management application");
        while (terminate_condition == 1) {
            System.out.println("1. Add new product.");
            System.out.println("2. Update a product.");
            System.out.println("3. Delete a product.");
            System.out.println("4. Print report.");
            System.out.println("Enter the option for operation to perform:");
            int option = sc.nextInt();
            if (option == 1) {
                sc.nextLine();
                System.out.print("Enter product code: ");
                code = sc.nextLine();
                System.out.print("Enter product description: ");
                description = sc.nextLine();
                System.out.print("Enter product price: ");
                price = sc.nextDouble();
                Product newProduct = new Product(code, description, price);
                products.add(newProduct);
                System.out.println("\nProduct has been added.\n");
            }
            else if (option == 2)
            {
                sc.nextLine();
                System.out.print("Enter product code to edit: ");
                code = sc.nextLine();
                int index = getProductByCode(products, code);
                if (index != -1) {
                    System.out.print("Enter a new product description: ");
                    description = sc.nextLine();
                    System.out.print("Enter a new product price: ");
                    price = sc.nextDouble();
                    Product newProduct = new Product(code, description, price);
                    products.remove(index);
                    products.add(index, newProduct);
                    System.out.println("\nProduct has been updated.\n");
                }
                //IN the case where the Id is not in the array of products
                else {
                    System.out.println("\nWrong product ID.\n");
                }
                }
            else if (option == 3)
            {
                sc.nextLine();
                System.out.print("Enter product code to delete: ");
                code = sc.nextLine();
                int index = getProductByCode(products, code);
                if (index != -1) {
                    products.remove(index);
                    System.out.println("\nProduct has been deleted.\n");
                } else {
                    System.out.println("\nWrong product ID.\n");
                }
                }
            else if (option == 4)
            {
                System.out.println("Extreme IT products Report");
                int count = 1;
                for (int i = 0; i < products.size(); i++) {
                    System.out.print(count);
                    System.out.println(".\t"+products.get(i).toString() + "\n");
                    count = count+1;
                }
                }
            else {
                    System.out.println("Invalid option please try again");
                }
                System.out.print("1. To perform another operation or any other key to exit: ");
                int my_option = sc.nextInt();
                terminate_condition = my_option;
                
            }
        }
    }

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