BaymaxSky / core-java-world

0 stars 0 forks source link

Java 1. Inventory Management #6

Open BaymaxSky opened 1 year ago

BaymaxSky commented 1 year ago
package students.commodity;

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

class Product {
    private String name;
    private double price;
    private int quantity;

    public Product(String name, double price, int quantity) {
        this.name = name;
        this.price = price;
        this.quantity = quantity;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
}

public class InventoryManagement {
    private ArrayList<Product> products;

    public InventoryManagement() {
        products = new ArrayList<>();
    }

    public void addProduct(Product product) {
        products.add(product);
    }

    public void displayProducts() {
        System.out.println("Inventory:");
        for (Product product : products) {
            System.out.println(product.getName() + " - Price: $" + product.getPrice() + " - Quantity: " + product.getQuantity());
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        InventoryManagement inventory = new InventoryManagement();

        while (true) {
            System.out.println("\nOptions:");
            System.out.println("1. Add product");
            System.out.println("2. Display products");
            System.out.println("3. Exit");
            System.out.print("Enter your choice: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume the newline character after reading the integer

            switch (choice) {
                case 1:
                    System.out.print("Enter product name: ");
                    String name = scanner.nextLine();
                    System.out.print("Enter product price: ");
                    double price = scanner.nextDouble();
                    System.out.print("Enter product quantity: ");
                    int quantity = scanner.nextInt();
                    scanner.nextLine(); // Consume the newline character after reading the integer

                    Product product = new Product(name, price, quantity);
                    inventory.addProduct(product);
                    break;

                case 2:
                    inventory.displayProducts();
                    break;

                case 3:
                    System.out.println("Exiting...");
                    scanner.close();
                    System.exit(0);
                    break;

                default:
                    System.out.println("Invalid choice! Please try again.");
            }
        }
    }
}