|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.List; |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +public class MedicalStore { |
| 6 | + public static class Product { |
| 7 | + private int id; |
| 8 | + private String name; |
| 9 | + private double price; |
| 10 | + private int quantity; |
| 11 | + |
| 12 | + public Product(int id, String name, double price, int quantity) { |
| 13 | + this.id = id; |
| 14 | + this.name = name; |
| 15 | + this.price = price; |
| 16 | + this.quantity = quantity; |
| 17 | + } |
| 18 | + |
| 19 | + public int getId() { |
| 20 | + return id; |
| 21 | + } |
| 22 | + |
| 23 | + public String getName() { |
| 24 | + return name; |
| 25 | + } |
| 26 | + |
| 27 | + public double getPrice() { |
| 28 | + return price; |
| 29 | + } |
| 30 | + |
| 31 | + public int getQuantity() { |
| 32 | + return quantity; |
| 33 | + } |
| 34 | + |
| 35 | + public void setQuantity(int quantity) { |
| 36 | + this.quantity = quantity; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + private List<Product> products; |
| 41 | + |
| 42 | + public MedicalStore() { |
| 43 | + products = new ArrayList<>(); |
| 44 | + } |
| 45 | + |
| 46 | + public void addProduct(Product product) { |
| 47 | + products.add(product); |
| 48 | + } |
| 49 | + |
| 50 | + public void listProducts() { |
| 51 | + System.out.println("Products available:"); |
| 52 | + for (Product product : products) { |
| 53 | + System.out.println(product.getId() + ". " + product.getName() + " - $" + product.getPrice()); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public Product findProductById(int id) { |
| 58 | + for (Product product : products) { |
| 59 | + if (product.getId() == id) { |
| 60 | + return product; |
| 61 | + } |
| 62 | + } |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + public void sellProduct(int productId, int quantity) { |
| 67 | + Product product = findProductById(productId); |
| 68 | + if (product != null && product.getQuantity() >= quantity) { |
| 69 | + product.setQuantity(product.getQuantity() - quantity); |
| 70 | + System.out.println(quantity + " " + product.getName() + " sold for $" + (product.getPrice() * quantity)); |
| 71 | + } else { |
| 72 | + System.out.println("Product not available or insufficient quantity."); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public static void main(String[] args) { |
| 77 | + MedicalStore store = new MedicalStore(); |
| 78 | + store.addProduct(new Product(1, "Painkiller", 5.99, 50)); |
| 79 | + store.addProduct(new Product(2, "Bandages", 3.49, 100)); |
| 80 | + store.addProduct(new Product(3, "Vitamins", 8.99, 30); |
| 81 | + |
| 82 | + Scanner scanner = new Scanner(System.in); |
| 83 | + |
| 84 | + while (true) { |
| 85 | + System.out.println("\n1. List Products"); |
| 86 | + System.out.println("2. Sell Product"); |
| 87 | + System.out.println("3. Exit"); |
| 88 | + System.out.print("Select an option: "); |
| 89 | + |
| 90 | + int choice = scanner.nextInt(); |
| 91 | + |
| 92 | + switch (choice) { |
| 93 | + case 1: |
| 94 | + store.listProducts(); |
| 95 | + break; |
| 96 | + case 2: |
| 97 | + System.out.print("Enter product ID: "); |
| 98 | + int productId = scanner.nextInt(); |
| 99 | + System.out.print("Enter quantity: "); |
| 100 | + int quantity = scanner.nextInt(); |
| 101 | + store.sellProduct(productId, quantity); |
| 102 | + break; |
| 103 | + case 3: |
| 104 | + System.out.println("Exiting..."); |
| 105 | + System.exit(0); |
| 106 | + default: |
| 107 | + System.out.println("Invalid option. Please try again."); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments