98 lines
		
	
	
	
		
			2.7 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			98 lines
		
	
	
	
		
			2.7 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
| 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<Order> 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();
 | |
|     }
 | |
| }
 | 
