mqrnKNOetoNOOB

Untitled

Jul 24th, 2025
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 37.15 KB | None | 0 0
  1. // BloodUnit.java
  2. package BloodRelated;
  3.  
  4. import java.time.LocalDate;
  5. import java.io.Serializable;
  6.  
  7. public class BloodUnit implements Serializable{
  8.     private String donorName;
  9.     private String donorNID;
  10.     private String bloodGroup;
  11.     private LocalDate donationDate;
  12.     private LocalDate expiryDate;
  13.  
  14.     public BloodUnit(String donorName, String donorNID, String bloodGroup, LocalDate donationDate, LocalDate expiryDate) {
  15.         this.donorName = donorName;
  16.         this.donorNID = donorNID;
  17.         this.bloodGroup = bloodGroup;
  18.         this.donationDate = donationDate;
  19.         this.expiryDate = expiryDate;
  20.     }
  21.  
  22.     public String getDonorName() { return donorName; }
  23.     public String getDonorNID() { return donorNID; }
  24.     public String getBloodGroup() { return bloodGroup; }
  25.     public LocalDate getDonationDate() { return donationDate; }
  26.     public LocalDate getExpiryDate() { return expiryDate; }
  27.     public String getName() {
  28.         return donorName;
  29.     }
  30.  
  31.     public void showInfo() {
  32.         System.out.println("Donor: " + donorName + ", NID: " + donorNID + ", Group: " + bloodGroup +
  33.                 ", Donated: " + donationDate + ", Expires: " + expiryDate);
  34.     }
  35.  
  36.     public boolean isExpired() {
  37.         return expiryDate.isBefore(LocalDate.now());
  38.     }
  39.  
  40.     @Override
  41.     public String toString() {
  42.         return donorName + " (" + bloodGroup + "), Expires: " + expiryDate;
  43.     }
  44. }
  45.  
  46. // BloodUnitList.java
  47. package BloodRelated;
  48.  
  49. import Message.StockAlertMessage;
  50.  
  51. import java.io.*;
  52. import java.time.LocalDate;
  53. import java.util.ArrayList;
  54.  
  55. public class BloodUnitList {
  56.  
  57.     private ArrayList<BloodUnit> Apos = new ArrayList<>();
  58.     private ArrayList<BloodUnit> Aneg = new ArrayList<>();
  59.     private ArrayList<BloodUnit> Bpos = new ArrayList<>();
  60.     private ArrayList<BloodUnit> Bneg = new ArrayList<>();
  61.     private ArrayList<BloodUnit> ABpos = new ArrayList<>();
  62.     private ArrayList<BloodUnit> ABneg = new ArrayList<>();
  63.     private ArrayList<BloodUnit> Opos = new ArrayList<>();
  64.     private ArrayList<BloodUnit> Oneg = new ArrayList<>();
  65.  
  66.     public ArrayList<BloodUnit> fetchBlood(String group) {
  67.         group = group.toUpperCase();
  68.         switch (group) {
  69.             case "A+": return Apos;
  70.             case "A-": return Aneg;
  71.             case "B+": return Bpos;
  72.             case "B-": return Bneg;
  73.             case "AB+": return ABpos;
  74.             case "AB-": return ABneg;
  75.             case "O+": return Opos;
  76.             case "O-": return Oneg;
  77.             default: return null;
  78.         }
  79.     }
  80.  
  81.     public void append(ArrayList<BloodUnit> list, BloodUnit unit) {
  82.         list.add(unit);
  83.         this.saveAllToFiles("C:\\Users\\win11\\OneDrive\\Documents\\BUET program\\project java\\javaProject\\data");
  84.     }
  85.  
  86.     public void leave(ArrayList<BloodUnit> list, int index) {
  87.         list.remove(index);
  88.         this.saveAllToFiles("C:\\Users\\win11\\OneDrive\\Documents\\BUET program\\project java\\javaProject\\data");
  89.     }
  90.  
  91.     public BloodUnit dequeue(ArrayList<BloodUnit> list) {
  92.         if (list == null || list.isEmpty()) {
  93.             return null;
  94.         }
  95.         BloodUnit bloodUnit = list.remove(0);
  96.         this.saveAllToFiles("C:\\Users\\win11\\OneDrive\\Documents\\BUET program\\project java\\javaProject\\data");
  97.         return bloodUnit;
  98.     }
  99.  
  100.     public void printAll() {
  101.         String[] groups = {"A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-"};
  102.         for (String group : groups) {
  103.             ArrayList<BloodUnit> list = fetchBlood(group);
  104.             System.out.println("Group " + group + ":");
  105.             for (BloodUnit b : list) {
  106.                 System.out.println("  " + b);
  107.             }
  108.         }
  109.     }
  110.  
  111.     public ArrayList<BloodUnit> getExpiredUnits() {
  112.         ArrayList<BloodUnit> expired = new ArrayList<>();
  113.         String[] groups = {"A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-"};
  114.         for (String group : groups) {
  115.             ArrayList<BloodUnit> list = fetchBlood(group);
  116.             for (BloodUnit b : list) {
  117.                 if (b.isExpired()) expired.add(b);
  118.             }
  119.         }
  120.         return expired;
  121.     }
  122.  
  123.     public void saveAllToFiles(String folderPath) {
  124.         String[] bloodGroups = {"A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-"};
  125.  
  126.         for (String group : bloodGroups) {
  127.             try {
  128.                 File file = new File(folderPath + "/blood_" + group.replace("+", "pos").replace("-", "neg") + ".csv");
  129.                 BufferedWriter bw = new BufferedWriter(new FileWriter(file));
  130.  
  131.                 for (BloodUnit unit : fetchBlood(group)) {
  132.                     String line = unit.getDonorName() + "," + unit.getDonorNID() + "," +
  133.                             unit.getBloodGroup() + "," + unit.getDonationDate() + "," + unit.getExpiryDate();
  134.                     bw.write(line);
  135.                     bw.newLine();
  136.                 }
  137.  
  138.                 bw.close();
  139.             } catch (IOException e) {
  140.                 System.err.println("Error writing to file for group: " + group);
  141.             }
  142.         }
  143.     }
  144.  
  145.     public void loadAllFromFiles(String folderPath) {
  146.         String[] bloodGroups = {"A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-"};
  147.  
  148.         for (String group : bloodGroups) {
  149.             String fileName = folderPath + "/blood_" + group.replace("+", "pos").replace("-", "neg") + ".csv";
  150.             ArrayList<BloodUnit> list = fetchBlood(group);
  151.             try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
  152.                 String line;
  153.                 while ((line = br.readLine()) != null) {
  154.                     String[] parts = line.split(",", -1);
  155.                     if (parts.length == 5) {
  156.                         String donor = parts[0];
  157.                         String nid = parts[1];
  158.                         String grp = parts[2];
  159.                         LocalDate donationDate = LocalDate.parse(parts[3]);
  160.                         LocalDate expiryDate = LocalDate.parse(parts[4]);
  161.                         BloodUnit unit = new BloodUnit(donor, nid, grp, donationDate, expiryDate);
  162.                         list.add(unit);
  163.                     }
  164.                 }
  165.             } catch (IOException e) {
  166.                 System.err.println("Error loading blood units from file: " + fileName);
  167.             }
  168.         }
  169.     }
  170. }
  171.  
  172. // Person.java
  173. package BloodRelated;
  174.  
  175. import java.io.Serializable;
  176.  
  177. public class Person implements Serializable {
  178.     private String name;
  179.     private String NID;
  180.     private String bloodGroup;
  181.  
  182.     public Person(String name, String nid, String bloodGroup) {
  183.         this.name = name;
  184.         this.NID = nid;
  185.         this.bloodGroup = bloodGroup;
  186.     }
  187.  
  188.     public String getName() {
  189.         return name;
  190.     }
  191.  
  192.     public String getNID() {
  193.         return NID;
  194.     }
  195.  
  196.     public String getBloodGroup() {
  197.         return bloodGroup;
  198.     }
  199. }
  200.  
  201. // ClientHandler.java
  202. package Client;
  203.  
  204. import Message.*;
  205.  
  206. import java.io.*;
  207. import java.net.Socket;
  208. import java.util.concurrent.ConcurrentHashMap;
  209.  
  210. public class ClientHandler implements Runnable {
  211.     private static final ConcurrentHashMap<String, ObjectOutputStream> clientMap = new ConcurrentHashMap<>();
  212.     private Socket socket;
  213.     private ObjectInputStream in;
  214.     private ObjectOutputStream out;
  215.     private String role;
  216.  
  217.     public ClientHandler(Socket socket) {
  218.         this.socket = socket;
  219.     }
  220.  
  221.     @Override
  222.     public void run() {
  223.         try {
  224.             out = new ObjectOutputStream(socket.getOutputStream());
  225.             in = new ObjectInputStream(socket.getInputStream());
  226.  
  227.             role = (String) in.readObject(); // read role like "Manager", "DonationOfficer"
  228.             clientMap.put(role, out);
  229.  
  230.             Object obj;
  231.             while ((obj = in.readObject()) != null) {
  232.                 if (obj instanceof BaseMessage) {
  233.                     BaseMessage msg = (BaseMessage) obj;
  234.                     String to = msg.getReceiverRole();
  235.  
  236.                     if (clientMap.containsKey(to)) {
  237.                         clientMap.get(to).writeObject(msg);
  238.                         clientMap.get(to).flush();
  239.                     } else {
  240.                         System.out.println("Receiver " + to + " is not connected.");
  241.                     }
  242.                 }
  243.             }
  244.  
  245.         } catch (Exception e) {
  246.             System.out.println("Connection closed for: " + role);
  247.         } finally {
  248.             try {
  249.                 if (in != null) in.close();
  250.                 if (out != null) out.close();
  251.                 if (socket != null) socket.close();
  252.             } catch (IOException ex) {
  253.                 ex.printStackTrace();
  254.             }
  255.         }
  256.     }
  257. }
  258.  
  259. // ClientMain.java
  260. package Client;
  261.  
  262. import Message.*;
  263. import StuffRelated.*;
  264.  
  265. import java.io.*;
  266. import java.net.Socket;
  267. import java.util.Scanner;
  268.  
  269. public class ClientMain {
  270.     public static void main(String[] args) {
  271.         try (Socket socket = new Socket("localhost", 12345);
  272.              ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
  273.              ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
  274.              Scanner sc = new Scanner(System.in)) {
  275.  
  276.             Stuff currentUser = null;
  277.             while (currentUser == null) {
  278.                 System.out.println("Select Role:");
  279.                 System.out.println("1. Manager");
  280.                 System.out.println("2. Registration Tester");
  281.                 System.out.println("3. Donation Officer");
  282.                 System.out.println("4. Preservation Officer");
  283.                 System.out.println("5. Delivery Tester");
  284.                 System.out.print("Enter role number: ");
  285.                 int role = Integer.parseInt(sc.nextLine());
  286.  
  287.                 System.out.print("Enter PIN: ");
  288.                 String enteredPin = sc.nextLine();
  289.  
  290.                 output.writeObject("LOGIN_REQUEST");
  291.                 output.writeObject(role);
  292.                 output.writeObject(enteredPin);
  293.  
  294.                 Object response = input.readObject();
  295.                 if (response instanceof Stuff) {
  296.                     currentUser = (Stuff) response;
  297.                     System.out.println("Login successful!");
  298.                 } else {
  299.                     System.out.println("Login failed. Try again.\n");
  300.                 }
  301.             }
  302.  
  303.             String x = currentUser.getRole();
  304.             ClientMenuHandler.handleMenu(x, currentUser, output);
  305.  
  306.         } catch (Exception e) {
  307.             System.out.println("Error in client: " + e.getMessage());
  308.             e.printStackTrace();
  309.         }
  310.     }
  311. }
  312.  
  313. // ClientMenuHandler.java
  314. package Client;
  315.  
  316. import StuffRelated.*;
  317. import java.io.ObjectOutputStream;
  318. import java.util.Scanner;
  319.  
  320. public class ClientMenuHandler {
  321.  
  322.     public static void handleMenu(String role, Stuff currentStuff, ObjectOutputStream out) {
  323.         switch (role) {
  324.             case "Manager" -> ManagerClient.run(currentStuff, out);
  325.             case "RegTester" -> RegTesterClient.run(currentStuff, out);
  326.             case "DonationOfficer" -> DonationOfficerClient.run(currentStuff, out);
  327.             case "PreservationOfficer" -> PreservationOfficerClient.run(currentStuff, out);
  328.             case "DeliveryTester" -> DeliveryTesterClient.run(currentStuff, out);
  329.             default -> System.out.println("Invalid role. Cannot start client menu.");
  330.         }
  331.     }
  332. }
  333.  
  334. // DeliveryTesterClient.java
  335. package Client;
  336.  
  337. import StuffRelated.Stuff;
  338.  
  339. import java.io.ObjectOutputStream;
  340. import java.util.Scanner;
  341.  
  342. public class DeliveryTesterClient {
  343.     public static void run(Stuff currentStuff, ObjectOutputStream out) {
  344.         Scanner sc = new Scanner(System.in);
  345.  
  346.         while (true) {
  347.             System.out.println("\nDelivery Tester Menu:");
  348.             System.out.println("1. View Profile");
  349.             System.out.println("2. Send CustomerRequest back and request unit");
  350.             System.out.println("3. If none, send null");
  351.             System.out.println("0. Logout");
  352.  
  353.             int choice = sc.nextInt(); sc.nextLine();
  354.  
  355.             switch (choice) {
  356.                 case 1:
  357.                     currentStuff.showInfo();
  358.                     break;
  359.                 case 2:
  360.                     break;
  361.                 case 3:
  362.                     break;
  363.                 case 0:
  364.                     System.out.println("Logging out...");
  365.                     return;
  366.                 default:
  367.                     System.out.println("Invalid choice.");
  368.             }
  369.         }
  370.     }
  371. }
  372.  
  373. // DonationOfficerClient.java
  374. package Client;
  375.  
  376. import StuffRelated.Stuff;
  377.  
  378. import java.io.ObjectOutputStream;
  379. import java.util.Scanner;
  380.  
  381. public class DonationOfficerClient {
  382.     public static void run(Stuff currentStuff, ObjectOutputStream out) {
  383.         Scanner sc = new Scanner(System.in);
  384.  
  385.         while (true) {
  386.             System.out.println("\nDonation Officer Menu:");
  387.             System.out.println("1. View Profile");
  388.             System.out.println("2. Forward donor info to Preservation Officer");
  389.             System.out.println("0. Logout");
  390.  
  391.             int choice = sc.nextInt(); sc.nextLine();
  392.  
  393.             switch (choice) {
  394.                 case 1:
  395.                     break;
  396.                 case 2:
  397.                     break;
  398.                 case 0:
  399.                     System.out.println("Logging out...");
  400.                     return;
  401.                 default:
  402.                     System.out.println("Invalid choice.");
  403.             }
  404.         }
  405.     }
  406. }
  407.  
  408. // ManagerClient.java
  409. package Client;
  410.  
  411. import StuffRelated.Stuff;
  412.  
  413. import java.io.ObjectOutputStream;
  414. import java.util.Scanner;
  415.  
  416. public class ManagerClient {
  417.     public static void run(Stuff currentStuff, ObjectOutputStream out) {
  418.         Scanner sc = new Scanner(System.in);
  419.  
  420.         while (true) {
  421.             System.out.println("\nManager Menu:");
  422.             System.out.println("1. Send blood request");
  423.             System.out.println("2. View all staff info");
  424.             System.out.println("3. View all kit quantities");
  425.             System.out.println("4. View blood unit details");
  426.             System.out.println("0. Logout");
  427.  
  428.             int choice = sc.nextInt(); sc.nextLine();
  429.  
  430.             switch (choice) {
  431.                 case 1:
  432.                     break;
  433.                 case 2:
  434.                     break;
  435.                 case 3:
  436.                     break;
  437.                 case 4:
  438.                     break;
  439.                 case 0:
  440.                     System.out.println("Logging out...");
  441.                     return;
  442.                 default:
  443.                     System.out.println("Invalid choice.");
  444.             }
  445.         }
  446.     }
  447. }
  448.  
  449. // PreservationOfficerClient.java
  450. package Client;
  451.  
  452. import StuffRelated.Stuff;
  453.  
  454. import java.io.ObjectOutputStream;
  455. import java.util.Scanner;
  456.  
  457. public class PreservationOfficerClient {
  458.     public static void run(Stuff currentStuff, ObjectOutputStream out) {
  459.         Scanner sc = new Scanner(System.in);
  460.  
  461.         while (true) {
  462.             System.out.println("\nPreservation Officer Menu:");
  463.             System.out.println("1. View Profile");
  464.             System.out.println("2. Enqueue blood unit from Donation Officer");
  465.             System.out.println("3. Dequeue and send to DeliveryTester");
  466.             System.out.println("4. Remove expired units");
  467.             System.out.println("0. Logout");
  468.  
  469.             int choice = sc.nextInt(); sc.nextLine();
  470.  
  471.             switch (choice) {
  472.                 case 1:
  473.                     break;
  474.                 case 2:
  475.                     break;
  476.                 case 3:
  477.                     break;
  478.                 case 4:
  479.                     break;
  480.                 case 0:
  481.                     System.out.println("Logging out...");
  482.                     return;
  483.                 default:
  484.                     System.out.println("Invalid choice.");
  485.             }
  486.         }
  487.     }
  488. }
  489.  
  490. // RegTesterClient.java
  491. package Client;
  492.  
  493. import StuffRelated.Stuff;
  494.  
  495. import java.io.ObjectOutputStream;
  496. import java.util.Scanner;
  497.  
  498. public class RegTesterClient {
  499.     public static void run(Stuff currentStuff, ObjectOutputStream out) {
  500.         Scanner sc = new Scanner(System.in);
  501.  
  502.         while (true) {
  503.             System.out.println("\nRegTester Menu:");
  504.             System.out.println("1. View Profile");
  505.             System.out.println("2. Input Donor Info");
  506.             System.out.println("0. Logout");
  507.  
  508.             int choice = sc.nextInt(); sc.nextLine();
  509.  
  510.             switch (choice) {
  511.                 case 1:
  512.                     break;
  513.                 case 2:
  514.                     break;
  515.                 case 0:
  516.                     System.out.println("Logging out...");
  517.                     return;
  518.                 default:
  519.                     System.out.println("Invalid choice.");
  520.             }
  521.         }
  522.     }
  523. }
  524.  
  525. // KitManagement.java
  526. package KitRelated;
  527.  
  528. import java.io.*;
  529. import Message.*;
  530.  
  531. public class KitManagement {
  532.     private int numberOfBag;
  533.     private int numberOfNeedle;
  534.     private int numberOfPipe;
  535.     private int numberOfTestingKit;
  536.     public KitManagement(int x, int y, int z, int w) {
  537.         numberOfBag = x;
  538.         numberOfNeedle = y;
  539.         numberOfPipe = z;
  540.         numberOfTestingKit = w;
  541.     }
  542.  
  543.     public void loadKitData() {
  544.         try (BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\win11\\OneDrive\\Documents\\BUET program\\project java\\javaProject\\data\\KitManagement.csv"))) {
  545.             String line = br.readLine();
  546.             String[] parts = line.strip().split(",");
  547.             int bag = Integer.parseInt(parts[0]);
  548.             int needle = Integer.parseInt(parts[1]);
  549.             int pipe = Integer.parseInt(parts[2]);
  550.             int testKit = Integer.parseInt(parts[3]);
  551.  
  552.             KitManagement kit = new KitManagement(bag, needle, pipe, testKit);
  553.             System.out.println("Kit data loaded successfully.");
  554.         } catch (Exception e) {
  555.             System.out.println("Error loading kit data: " + e.getMessage());
  556.         }
  557.     }
  558.  
  559.     public void setNumberOfBag(int numberOfBag) {
  560.         this.numberOfBag = numberOfBag;
  561.     }
  562.     public void setNumberOfNeedle(int numberOfNeedle) {
  563.         this.numberOfNeedle = numberOfNeedle;
  564.     }
  565.  
  566.     public void saveToFile() {
  567.         try (BufferedWriter writer = new BufferedWriter(new FileWriter("C:\\Users\\win11\\OneDrive\\Documents\\BUET program\\project java\\javaProject\\data\\KitManagement.csv"))) {
  568.             writer.write(numberOfBag + "," + numberOfNeedle + "," + numberOfPipe + "," + numberOfTestingKit);
  569.         } catch (IOException e) {
  570.             e.printStackTrace();
  571.         }
  572.     }
  573.  
  574.     public void setNumberOfPipe(int numberOfPipe) {
  575.         this.numberOfPipe = numberOfPipe;
  576.     }
  577.     public void setNumberOfTestingKit(int numberOfTestingKit) {
  578.         this.numberOfTestingKit = numberOfTestingKit;
  579.     }
  580.     public int getNumberOfBag() {
  581.         return numberOfBag;
  582.     }
  583.     public int getNumberOfNeedle() {
  584.         return numberOfNeedle;
  585.     }
  586.     public int getNumberOfPipe() {
  587.         return numberOfPipe;
  588.     }
  589.     public  int getNumberOfTestingKit(){return numberOfTestingKit;}
  590.  
  591.     public void showInfo() {
  592.         System.out.println(numberOfBag + "\t" + numberOfNeedle + "\t" + numberOfPipe);
  593.     }
  594.     public KitMessage Alarm(KitManagement x) {
  595.         KitMessage y = new KitMessage(x);
  596.         return y;
  597.     }
  598. }
  599.  
  600. // AddingRequestMessage.java
  601. package Message;
  602.  
  603. import BloodRelated.BloodUnit;
  604. import java.io.Serializable;
  605.  
  606. public class AddingRequestMessage implements BaseMessage {
  607.     private String from;
  608.     private String to;
  609.     private BloodUnit bloodUnit;
  610.     public AddingRequestMessage(String from, String to, BloodUnit bloodUnit) {
  611.         this.bloodUnit = bloodUnit;
  612.         this.from = from;
  613.         this.to = to;
  614.     }
  615.     public BloodUnit getBloodUnit() {
  616.         return bloodUnit;
  617.     }
  618.     public String getFrom() {
  619.         return from;
  620.     }
  621.     public String getTo() {
  622.         return to;
  623.     }
  624.  
  625.     @Override
  626.     public String getSenderRole() {
  627.         return from;
  628.     }
  629.  
  630.     @Override
  631.     public String getReceiverRole() {
  632.         return to;
  633.     }
  634. }
  635.  
  636. // BaseMessage.java
  637. package Message;
  638.  
  639. import java.io.Serializable;
  640.  
  641. public interface BaseMessage extends Serializable {
  642.     String getSenderRole();
  643.     String getReceiverRole();
  644. }
  645.  
  646. // CustomerRequestMessage.java
  647. package Message;
  648.  
  649. public class CustomerRequestMessage implements BaseMessage {
  650.     private String from;
  651.     private String to;
  652.     private String bloodGroup;
  653.     public CustomerRequestMessage(String from, String to, String bloodGroup) {
  654.         this.from = from;
  655.         this.to = to;
  656.         this.bloodGroup = bloodGroup;
  657.     }
  658.     public String getFrom() {
  659.         return from;
  660.     }
  661.     public String getTo() {
  662.         return to;
  663.     }
  664.     public String getBloodGroup() {
  665.         return bloodGroup;
  666.     }
  667.     @Override
  668.     public String getSenderRole() {
  669.         return from;
  670.     }
  671.  
  672.     @Override
  673.     public String getReceiverRole() {
  674.         return to;
  675.     }
  676. }
  677.  
  678. // KitMessage.java
  679. package Message;
  680.  
  681. import KitRelated.KitManagement;
  682.  
  683. public class KitMessage implements BaseMessage {
  684.     public String Message = "";
  685.     private static final String newLine = System.lineSeparator();
  686.     public KitMessage(KitManagement km) {
  687.         if(km.getNumberOfBag() == 0)
  688.             Message += "No bag left.";
  689.         if(km.getNumberOfNeedle() == 0) {
  690.             Message += newLine + "No needle left.";
  691.         }
  692.         if (km.getNumberOfPipe() == 0) {
  693.             Message += "No pipe left.";
  694.         }
  695.         if (km.getNumberOfTestingKit() == 1) {
  696.             Message += "No testing kit left.";
  697.         }
  698.     }
  699.  
  700.     @Override
  701.     public String getSenderRole() {
  702.         return "Server";
  703.     }
  704.  
  705.     @Override
  706.     public String getReceiverRole() {
  707.         return "Manager";
  708.     }
  709. }
  710.  
  711. // StockAlertMessage.java
  712. package Message;
  713.  
  714. public class StockAlertMessage implements BaseMessage {
  715.     private String message;
  716.  
  717.     public StockAlertMessage(String bloodGroup) {
  718.         this.message = "No unit available for blood group: " + bloodGroup;
  719.     }
  720.  
  721.     public String getMessage() {
  722.         return message;
  723.     }
  724.  
  725.     @Override
  726.     public String toString() {
  727.         return message;
  728.     }
  729.     public String getSenderRole() {
  730.         return "Server";
  731.     }
  732.  
  733.     @Override
  734.     public String getReceiverRole() {
  735.         return "Manager";
  736.     }
  737. }
  738.  
  739. // Server.java
  740. package Server;
  741.  
  742. import java.io.*;
  743. import java.net.*;
  744. import java.util.*;
  745. import Message.*;
  746. import StuffRelated.*;
  747. import BloodRelated.*;
  748. import KitRelated.*;
  749.  
  750. public class Server {
  751.     private static final int PORT = 12345;
  752.  
  753.     private static Map<String, ObjectOutputStream> clientOutputStreams = new HashMap<>();
  754.     private static StuffList staffList = new StuffList();
  755.     private static BloodUnitList bloodUnitList = new BloodUnitList();
  756.     private static KitManagement kit = new KitManagement(10, 10, 10, 10);
  757.  
  758.     public static void main(String[] args) throws IOException {
  759.         ServerSocket serverSocket = new ServerSocket(PORT);
  760.         System.out.println("Server started on port " + PORT);
  761.  
  762.         staffList.loadFromFile("data/stufflist.csv");
  763.         bloodUnitList.loadAllFromFiles("data");
  764.  
  765.         while (true) {
  766.             Socket clientSocket = serverSocket.accept();
  767.             System.out.println("Client connected: " + clientSocket);
  768.             new ClientHandler(clientSocket).start();
  769.         }
  770.     }
  771.  
  772.     static class ClientHandler extends Thread {
  773.         private Socket socket;
  774.         private ObjectOutputStream out;
  775.         private ObjectInputStream in;
  776.         private Stuff loggedInUser;
  777.  
  778.         public ClientHandler(Socket socket) {
  779.             this.socket = socket;
  780.         }
  781.  
  782.         public void run() {
  783.             try {
  784.                 out = new ObjectOutputStream(socket.getOutputStream());
  785.                 in = new ObjectInputStream(socket.getInputStream());
  786.  
  787.                 while (true) {
  788.                     Object obj = in.readObject();
  789.  
  790.                     if (obj instanceof String) {
  791.                         String role = (String) obj;
  792.                         out.writeObject("Enter PIN:");
  793.                         out.flush();
  794.  
  795.                         String pin = (String) in.readObject();
  796.                         Stuff user = staffList.getStuffByRole(role);
  797.                         if (user != null && pin.equals(user.getPin())) {
  798.                             user.login(pin);
  799.                             loggedInUser = user;
  800.                             clientOutputStreams.put(role, out);
  801.                             out.writeObject("Login successful as " + role);
  802.                         } else {
  803.                             out.writeObject("Login failed. Try again.");
  804.                         }
  805.                         out.flush();
  806.                     } else if (obj instanceof BaseMessage) {
  807.                         BaseMessage msg = (BaseMessage) obj;
  808.                         String target = msg.getReceiverRole();
  809.                         ObjectOutputStream targetStream = clientOutputStreams.get(target);
  810.                         if (targetStream != null) {
  811.                             targetStream.writeObject(msg);
  812.                             targetStream.flush();
  813.                         } else {
  814.                             System.out.println("Target role offline or not registered: " + target);
  815.                         }
  816.                     }
  817.                 }
  818.  
  819.             } catch (IOException | ClassNotFoundException e) {
  820.                 System.out.println("Client disconnected or error: " + e.getMessage());
  821.             } finally {
  822.                 try {
  823.                     socket.close();
  824.                 } catch (IOException e) {
  825.                     e.printStackTrace();
  826.                 }
  827.                 if (loggedInUser != null) {
  828.                     clientOutputStreams.remove(loggedInUser.getRole());
  829.                     loggedInUser.logout();
  830.                 }
  831.             }
  832.         }
  833.     }
  834. }
  835.  
  836. // DeliveryTester.java
  837. package StuffRelated;
  838.  
  839. import BloodRelated.BloodUnit;
  840. import KitRelated.KitManagement;
  841. import Message.AddingRequestMessage;
  842. import Message.CustomerRequestMessage;
  843.  
  844. public class DeliveryTester extends Stuff {
  845.  
  846.     public DeliveryTester(String name, String id, String contact, String pin) {
  847.         super(name, id, contact, pin);
  848.         setRole("Delivery Tester");
  849.     }
  850.  
  851.     public void profilePresenter() {
  852.         showInfo();
  853.     }
  854.  
  855.     public void kitInfoUpdater(int usedTestingKit, KitManagement x) {
  856.         x.setNumberOfTestingKit(x.getNumberOfTestingKit() - usedTestingKit);
  857.         x.saveToFile();
  858.     }
  859.  
  860.     public CustomerRequestMessage testBloodUnit(AddingRequestMessage bloodUnit, boolean passed) {
  861.         if (passed) {
  862.             System.out.println("Blood test passed for donor: " + bloodUnit.getBloodUnit().getBloodGroup());
  863.             return null;
  864.         } else {
  865.             return new CustomerRequestMessage("DeliveryTester", "PreservationOfficer", bloodUnit.getBloodUnit().getBloodGroup());
  866.         }
  867.     }
  868. }
  869.  
  870. // DonationOfficer.java
  871. package StuffRelated;
  872.  
  873. import BloodRelated.BloodUnit;
  874. import BloodRelated.BloodUnitList;
  875. import KitRelated.KitManagement;
  876. import Message.AddingRequestMessage;
  877.  
  878. import java.util.ArrayList;
  879.  
  880. public class DonationOfficer extends Stuff {
  881.  
  882.     public DonationOfficer(String name, String id, String contact, String pin) {
  883.         super(name, id, contact, pin);
  884.         setRole("Donation Officer");
  885.     }
  886.  
  887.     public void kitInfoUpdater(int usedPipe, int usedBag, KitManagement x) {
  888.         x.setNumberOfBag(x.getNumberOfBag() - usedBag);
  889.         x.setNumberOfPipe(x.getNumberOfPipe() - usedPipe);
  890.         x.saveToFile();
  891.     }
  892.  
  893.     public void profileShower() {
  894.         showInfo();
  895.     }
  896.  
  897.     public AddingRequestMessage anqueueApproval(boolean approved, BloodUnit bloodUnit) {
  898.         if(approved)
  899.             return new AddingRequestMessage("DonationOfficer", "DonationOfficer", bloodUnit);
  900.         return null;
  901.     }
  902. }
  903.  
  904. // Manager.java
  905. package StuffRelated;
  906.  
  907. import BloodRelated.BloodUnit;
  908. import BloodRelated.BloodUnitList;
  909. import KitRelated.KitManagement;
  910. import Message.CustomerRequestMessage;
  911.  
  912. import java.util.ArrayList;
  913. import java.util.List;
  914.  
  915. public class Manager extends Stuff {
  916.  
  917.     public Manager(String name, String id, String contact, String pin) {
  918.         super(name, id, contact, pin);
  919.         setRole("Manager");
  920.     }
  921.  
  922.     public void profilePresenter() {
  923.         showInfo();
  924.     }
  925.  
  926.     public void showList(StuffList x) {
  927.         x.printAll();
  928.     }
  929.  
  930.     public void receiveKit(KitManagement x, int a, int b, int c, int d) {
  931.         x.setNumberOfBag(x.getNumberOfBag() + a);
  932.         x.setNumberOfNeedle(x.getNumberOfNeedle() + b);
  933.         x.setNumberOfPipe(x.getNumberOfPipe() + c);
  934.         x.setNumberOfTestingKit(x.getNumberOfTestingKit() + d);
  935.     }
  936.  
  937.     public void showBloodInfo(BloodUnitList x) {
  938.         String[] groups = {"A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-"};
  939.         for (String group : groups) {
  940.             List<BloodUnit> list = x.fetchBlood(group);
  941.             for (BloodUnit unit : list) {
  942.                 unit.showInfo();
  943.             }
  944.         }
  945.     }
  946.  
  947.     public CustomerRequestMessage CustomerRequest(String bg) {
  948.         return new CustomerRequestMessage("Manager", "PreservationOfficer", bg);
  949.     }
  950. }
  951.  
  952. // PreservationOfficer.java
  953. package StuffRelated;
  954.  
  955. import BloodRelated.BloodUnit;
  956. import BloodRelated.BloodUnitList;
  957. import Message.AddingRequestMessage;
  958. import Message.CustomerRequestMessage;
  959.  
  960. import java.util.ArrayList;
  961. import java.util.List;
  962.  
  963. public class PreservationOfficer extends Stuff {
  964.  
  965.     public PreservationOfficer(String name, String id, String phone, String pin) {
  966.         super(name, id, phone, pin);
  967.     }
  968.  
  969.     public void checkExpiredUnits(BloodUnitList bloodList) {
  970.         List<BloodUnit> expired = bloodList.getExpiredUnits();
  971.         if (expired.isEmpty()) {
  972.             System.out.println(" No expired blood units.");
  973.         } else {
  974.             System.out.println("️ Expired blood units:");
  975.             for (BloodUnit unit : expired) {
  976.                 System.out.println(unit);
  977.             }
  978.         }
  979.     }
  980.     public void enqueueBloodUnit(AddingRequestMessage bloodUnit, BloodUnitList bloodUnitList) {
  981.         List<BloodUnit> groupList = bloodUnitList.fetchBlood(bloodUnit.getBloodUnit().getBloodGroup());
  982.         BloodUnit blood = bloodUnit.getBloodUnit();
  983.         if (groupList != null) {
  984.             groupList.add(blood);
  985.             System.out.println("Blood unit added for group " + blood.getBloodGroup());
  986.         } else {
  987.             System.out.println("Blood group list not found.");
  988.         }
  989.     }
  990.     public AddingRequestMessage dequeueBloodUnit(AddingRequestMessage bloodUnit, BloodUnitList bloodUnitList) {
  991.         ArrayList<BloodUnit> x = bloodUnitList.fetchBlood(bloodUnit.getBloodUnit().getBloodGroup());
  992.         BloodUnit y = bloodUnitList.dequeue(x);
  993.         return new AddingRequestMessage("PreservationOfficer","DeliveryTester",y);
  994.     }
  995.  
  996.     public void removeExpiredBloodUnits(BloodUnitList bloodUnitList) {
  997.         String[] groups = {"A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-"};
  998.         for (String group : groups) {
  999.             ArrayList<BloodUnit> list = bloodUnitList.fetchBlood(group);
  1000.             for (int i = 0; i < list.size(); i++) {
  1001.                 BloodUnit unit = list.get(i);
  1002.                 if (unit.isExpired()) {
  1003.                     list.remove(i);
  1004.                 }
  1005.                 else break;
  1006.             }
  1007.         }
  1008.         bloodUnitList.saveAllToFiles("C:\\Users\\win11\\OneDrive\\Documents\\BUET program\\project java\\javaProject\\data");
  1009.         System.out.println("All expired blood units removed successfully.");
  1010.     }
  1011.  
  1012.     public void removeBloodUnit(BloodUnitList bloodUnitList, String bloodGroup, int[] selectedIDX) {
  1013.         ArrayList<BloodUnit> units = bloodUnitList.fetchBlood(bloodGroup);
  1014.         for(int i=0; i<selectedIDX.length; i++) {
  1015.             bloodUnitList.leave(units, selectedIDX[i]);
  1016.             for(int j=i+1; j<selectedIDX.length; j++) {
  1017.                 if(selectedIDX[j] > selectedIDX[i]) {
  1018.                     selectedIDX[j]--;
  1019.                 }
  1020.             }
  1021.         }
  1022.     }
  1023. }
  1024.  
  1025. // RegTester.java
  1026. package StuffRelated;
  1027.  
  1028. import BloodRelated.BloodUnit;
  1029. import BloodRelated.BloodUnitList;
  1030. import BloodRelated.Person;
  1031. import KitRelated.KitManagement;
  1032. import Message.AddingRequestMessage;
  1033.  
  1034. import java.time.LocalDate;
  1035.  
  1036. public class RegTester extends Stuff {
  1037.     private Person donor;
  1038.  
  1039.     public RegTester(String name, String id, String contact, String pin) {
  1040.         super(name, id, contact, pin);
  1041.         donor = null;
  1042.         setRole("Registration Tester");
  1043.     }
  1044.  
  1045.     public Person registerDonor(String name, String id, String bg) {
  1046.         donor = new Person(name, id, bg);
  1047.         return donor;
  1048.     }
  1049.  
  1050.     public AddingRequestMessage checkHealth(boolean x, Person person) {
  1051.         if(x) {
  1052.             LocalDate today = LocalDate.now();
  1053.             LocalDate expiry = today.plusDays(14);
  1054.             BloodUnit q = new BloodUnit(person.getName(), person.getNID(), person.getBloodGroup(), today, expiry);
  1055.             return new AddingRequestMessage("RegTester","DonationOfficer",q);
  1056.         }
  1057.         return null;
  1058.     }
  1059.     public void kitInfoUpdater(int usedTestingKit, KitManagement x) {
  1060.         x.setNumberOfTestingKit(x.getNumberOfTestingKit() - usedTestingKit);
  1061.         x.saveToFile();
  1062.     }
  1063. }
  1064.  
  1065. // Stuff.java
  1066. package StuffRelated;
  1067.  
  1068. public class Stuff {
  1069.     private String name;
  1070.     private String id;
  1071.     private String phone;
  1072.     private String pin;
  1073.     private boolean isLoggedIn;
  1074.     private String role = "Undefined";
  1075.  
  1076.     public Stuff(String name, String id, String phone, String pin) {
  1077.         this.name = name;
  1078.         this.id = id;
  1079.         this.phone = phone;
  1080.         this.pin = pin;
  1081.         this.isLoggedIn = false;
  1082.     }
  1083.  
  1084.     public String getName() { return name; }
  1085.     public String getId() { return id; }
  1086.     public String getPhone() { return phone; }
  1087.     public String getPin() { return pin; }
  1088.     public boolean isLoggedIn() { return isLoggedIn; }
  1089.     public String getRole() { return role; }
  1090.  
  1091.     public void setRole(String role) {
  1092.         this.role = role;
  1093.     }
  1094.  
  1095.     public void login(String pin) {
  1096.         if(pin.equals(this.pin)) {
  1097.             this.isLoggedIn = true;
  1098.             System.out.println(name + " logged in.");
  1099.         }
  1100.     }
  1101.  
  1102.     public void logout() {
  1103.         this.isLoggedIn = false;
  1104.         System.out.println(name + " logged out.");
  1105.     }
  1106.  
  1107.     public void showInfo() {
  1108.         System.out.println("Name: " + name);
  1109.         System.out.println("ID: " + id);
  1110.         System.out.println("Phone: " + phone);
  1111.         System.out.println("Role: " + role);
  1112.         System.out.println("Login Status: " + (isLoggedIn ? "Logged In" : "Logged Out"));
  1113.     }
  1114.  
  1115.     @Override
  1116.     public String toString() {
  1117.         return name + " (" + id + "), Role: " + role + ", Status: " + (isLoggedIn ? "In" : "Out");
  1118.     }
  1119. }
  1120.  
  1121. // StuffList.java
  1122. package StuffRelated;
  1123.  
  1124. import java.io.*;
  1125. import java.util.ArrayList;
  1126. import java.util.List;
  1127.  
  1128. public class StuffList {
  1129.     private List<Stuff> stuff = new ArrayList<>();
  1130.  
  1131.     public void loadFromFile(String filename) {
  1132.         try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
  1133.             String line;
  1134.             int i = 0;
  1135.             while ((line = br.readLine()) != null && i < 5) {
  1136.                 String[] parts = line.split(",", -1);
  1137.                 if (parts.length >= 4) {
  1138.                     String name = parts[0];
  1139.                     String id = parts[1];
  1140.                     String phone = parts[2];
  1141.                     String pin = parts[3];
  1142.  
  1143.                     Stuff s = null;
  1144.                     if (i == 0) {
  1145.                         s = new Manager(name, id, phone, pin);
  1146.                     } else if (i == 1) {
  1147.                         s = new RegTester(name, id, phone, pin);
  1148.                     } else if (i == 2) {
  1149.                         s = new DonationOfficer(name, id, phone, pin);
  1150.                     } else if (i == 3) {
  1151.                         s = new PreservationOfficer(name, id, phone, pin);
  1152.                     } else if (i == 4) {
  1153.                         s = new DeliveryTester(name, id, phone, pin);
  1154.                     }
  1155.                     if (s != null) stuff.add(s);
  1156.                     i++;
  1157.                 }
  1158.             }
  1159.         } catch (IOException e) {
  1160.             System.err.println("Error loading staff from file: " + e.getMessage());
  1161.         }
  1162.     }
  1163.  
  1164.     public Stuff getById(String id) {
  1165.         for (Stuff s : stuff) {
  1166.             if (s.getId().equals(id)) return s;
  1167.         }
  1168.         return null;
  1169.     }
  1170.  
  1171.     public List<Stuff> getAll() {
  1172.         return stuff;
  1173.     }
  1174.  
  1175.     public void printAll() {
  1176.         for (Stuff s : stuff) {
  1177.             System.out.println(s);
  1178.         }
  1179.     }
  1180.  
  1181.     public Stuff getStuffByRole(String role) {
  1182.         for(int i=0; i<stuff.size(); i++) {
  1183.             if(stuff.get(i).getRole() == role)
  1184.                 return stuff.get(i);
  1185.         }
  1186.         return null;
  1187.     }
  1188. }
Advertisement
Add Comment
Please, Sign In to add comment