From b15203dcf3449c2e3a82a9af227cf344802dbfaa Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Mon, 28 Oct 2024 00:13:56 +0530 Subject: [PATCH] Add OOP/Java/Assignments/FISAC/Q1.java --- OOP/Java/Assignments/FISAC/Q1.java | 98 ++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 OOP/Java/Assignments/FISAC/Q1.java diff --git a/OOP/Java/Assignments/FISAC/Q1.java b/OOP/Java/Assignments/FISAC/Q1.java new file mode 100644 index 0000000..052a3ba --- /dev/null +++ b/OOP/Java/Assignments/FISAC/Q1.java @@ -0,0 +1,98 @@ +import java.util.*; + +class Warehouse { + String productId; + int quantity; + int unitCost; + + Warehouse(String productId, int quantity, int unitCost) { + this.productId = productId; + this.quantity = quantity; + this.unitCost = unitCost; + } +} + +class Order { + int orderedQuantity; + int orderId; + String date; + + Order(int orderedQuantity, int orderId, String date) { + this.orderedQuantity = orderedQuantity; + this.orderId = orderId; + this.date = date; + } +} + +class BookOrderThread extends Thread { + private Warehouse warehouse; + private Order order; + private static List orderList = new ArrayList<>(); + + BookOrderThread(Warehouse warehouse, Order order) { + this.warehouse = warehouse; + this.order = order; + } + + public void run() { + try { + checkAvailableQuantity(); + computeTotCost(); + orderList.add(order); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } + + private void checkAvailableQuantity() throws Exception { + if (warehouse.quantity == 0) { + throw new Exception("Order cannot be placed"); + } + if (order.orderedQuantity == 0) { + throw new Exception("Order quantity is invalid"); + } + if (order.orderedQuantity > warehouse.quantity) { + throw new Exception("Out of Stock for " + warehouse.productId); + } + } + + private void computeTotCost() { + warehouse.quantity -= order.orderedQuantity; + int totalCost = order.orderedQuantity * warehouse.unitCost; + System.out.println("OrderID: " + order.orderId + " Total Cost: " + totalCost); + } + + public static void displaySortedOrders() { + orderList.sort(Comparator.comparing(o -> o.date)); + for (Order o : orderList) { + System.out.println("OrderID: " + o.orderId + " Date: " + o.date); + } + } +} + +public class Main { + public static void main(String[] args) { + Warehouse warehouse = new Warehouse("P001", 50, 100); + Order o1 = new Order(10, 101, "2023-10-01"); + Order o2 = new Order(20, 102, "2023-10-03"); + Order o3 = new Order(25, 103, "2023-10-02"); + + Thread t1 = new BookOrderThread(warehouse, o1); + Thread t2 = new BookOrderThread(warehouse, o2); + Thread t3 = new BookOrderThread(warehouse, o3); + + t1.start(); + t2.start(); + t3.start(); + + try { + t1.join(); + t2.join(); + t3.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + BookOrderThread.displaySortedOrders(); + } +}