Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // BloodUnit.java
- package BloodRelated;
- import java.time.LocalDate;
- import java.io.Serializable;
- public class BloodUnit implements Serializable{
- private String donorName;
- private String donorNID;
- private String bloodGroup;
- private LocalDate donationDate;
- private LocalDate expiryDate;
- public BloodUnit(String donorName, String donorNID, String bloodGroup, LocalDate donationDate, LocalDate expiryDate) {
- this.donorName = donorName;
- this.donorNID = donorNID;
- this.bloodGroup = bloodGroup;
- this.donationDate = donationDate;
- this.expiryDate = expiryDate;
- }
- public String getDonorName() { return donorName; }
- public String getDonorNID() { return donorNID; }
- public String getBloodGroup() { return bloodGroup; }
- public LocalDate getDonationDate() { return donationDate; }
- public LocalDate getExpiryDate() { return expiryDate; }
- public String getName() {
- return donorName;
- }
- public void showInfo() {
- System.out.println("Donor: " + donorName + ", NID: " + donorNID + ", Group: " + bloodGroup +
- ", Donated: " + donationDate + ", Expires: " + expiryDate);
- }
- public boolean isExpired() {
- return expiryDate.isBefore(LocalDate.now());
- }
- @Override
- public String toString() {
- return donorName + " (" + bloodGroup + "), Expires: " + expiryDate;
- }
- }
- // BloodUnitList.java
- package BloodRelated;
- import Message.StockAlertMessage;
- import java.io.*;
- import java.time.LocalDate;
- import java.util.ArrayList;
- public class BloodUnitList {
- private ArrayList<BloodUnit> Apos = new ArrayList<>();
- private ArrayList<BloodUnit> Aneg = new ArrayList<>();
- private ArrayList<BloodUnit> Bpos = new ArrayList<>();
- private ArrayList<BloodUnit> Bneg = new ArrayList<>();
- private ArrayList<BloodUnit> ABpos = new ArrayList<>();
- private ArrayList<BloodUnit> ABneg = new ArrayList<>();
- private ArrayList<BloodUnit> Opos = new ArrayList<>();
- private ArrayList<BloodUnit> Oneg = new ArrayList<>();
- public ArrayList<BloodUnit> fetchBlood(String group) {
- group = group.toUpperCase();
- switch (group) {
- case "A+": return Apos;
- case "A-": return Aneg;
- case "B+": return Bpos;
- case "B-": return Bneg;
- case "AB+": return ABpos;
- case "AB-": return ABneg;
- case "O+": return Opos;
- case "O-": return Oneg;
- default: return null;
- }
- }
- public void append(ArrayList<BloodUnit> list, BloodUnit unit) {
- list.add(unit);
- this.saveAllToFiles("C:\\Users\\win11\\OneDrive\\Documents\\BUET program\\project java\\javaProject\\data");
- }
- public void leave(ArrayList<BloodUnit> list, int index) {
- list.remove(index);
- this.saveAllToFiles("C:\\Users\\win11\\OneDrive\\Documents\\BUET program\\project java\\javaProject\\data");
- }
- public BloodUnit dequeue(ArrayList<BloodUnit> list) {
- if (list == null || list.isEmpty()) {
- return null;
- }
- BloodUnit bloodUnit = list.remove(0);
- this.saveAllToFiles("C:\\Users\\win11\\OneDrive\\Documents\\BUET program\\project java\\javaProject\\data");
- return bloodUnit;
- }
- public void printAll() {
- String[] groups = {"A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-"};
- for (String group : groups) {
- ArrayList<BloodUnit> list = fetchBlood(group);
- System.out.println("Group " + group + ":");
- for (BloodUnit b : list) {
- System.out.println(" " + b);
- }
- }
- }
- public ArrayList<BloodUnit> getExpiredUnits() {
- ArrayList<BloodUnit> expired = new ArrayList<>();
- String[] groups = {"A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-"};
- for (String group : groups) {
- ArrayList<BloodUnit> list = fetchBlood(group);
- for (BloodUnit b : list) {
- if (b.isExpired()) expired.add(b);
- }
- }
- return expired;
- }
- public void saveAllToFiles(String folderPath) {
- String[] bloodGroups = {"A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-"};
- for (String group : bloodGroups) {
- try {
- File file = new File(folderPath + "/blood_" + group.replace("+", "pos").replace("-", "neg") + ".csv");
- BufferedWriter bw = new BufferedWriter(new FileWriter(file));
- for (BloodUnit unit : fetchBlood(group)) {
- String line = unit.getDonorName() + "," + unit.getDonorNID() + "," +
- unit.getBloodGroup() + "," + unit.getDonationDate() + "," + unit.getExpiryDate();
- bw.write(line);
- bw.newLine();
- }
- bw.close();
- } catch (IOException e) {
- System.err.println("Error writing to file for group: " + group);
- }
- }
- }
- public void loadAllFromFiles(String folderPath) {
- String[] bloodGroups = {"A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-"};
- for (String group : bloodGroups) {
- String fileName = folderPath + "/blood_" + group.replace("+", "pos").replace("-", "neg") + ".csv";
- ArrayList<BloodUnit> list = fetchBlood(group);
- try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
- String line;
- while ((line = br.readLine()) != null) {
- String[] parts = line.split(",", -1);
- if (parts.length == 5) {
- String donor = parts[0];
- String nid = parts[1];
- String grp = parts[2];
- LocalDate donationDate = LocalDate.parse(parts[3]);
- LocalDate expiryDate = LocalDate.parse(parts[4]);
- BloodUnit unit = new BloodUnit(donor, nid, grp, donationDate, expiryDate);
- list.add(unit);
- }
- }
- } catch (IOException e) {
- System.err.println("Error loading blood units from file: " + fileName);
- }
- }
- }
- }
- // Person.java
- package BloodRelated;
- import java.io.Serializable;
- public class Person implements Serializable {
- private String name;
- private String NID;
- private String bloodGroup;
- public Person(String name, String nid, String bloodGroup) {
- this.name = name;
- this.NID = nid;
- this.bloodGroup = bloodGroup;
- }
- public String getName() {
- return name;
- }
- public String getNID() {
- return NID;
- }
- public String getBloodGroup() {
- return bloodGroup;
- }
- }
- // ClientHandler.java
- package Client;
- import Message.*;
- import java.io.*;
- import java.net.Socket;
- import java.util.concurrent.ConcurrentHashMap;
- public class ClientHandler implements Runnable {
- private static final ConcurrentHashMap<String, ObjectOutputStream> clientMap = new ConcurrentHashMap<>();
- private Socket socket;
- private ObjectInputStream in;
- private ObjectOutputStream out;
- private String role;
- public ClientHandler(Socket socket) {
- this.socket = socket;
- }
- @Override
- public void run() {
- try {
- out = new ObjectOutputStream(socket.getOutputStream());
- in = new ObjectInputStream(socket.getInputStream());
- role = (String) in.readObject(); // read role like "Manager", "DonationOfficer"
- clientMap.put(role, out);
- Object obj;
- while ((obj = in.readObject()) != null) {
- if (obj instanceof BaseMessage) {
- BaseMessage msg = (BaseMessage) obj;
- String to = msg.getReceiverRole();
- if (clientMap.containsKey(to)) {
- clientMap.get(to).writeObject(msg);
- clientMap.get(to).flush();
- } else {
- System.out.println("Receiver " + to + " is not connected.");
- }
- }
- }
- } catch (Exception e) {
- System.out.println("Connection closed for: " + role);
- } finally {
- try {
- if (in != null) in.close();
- if (out != null) out.close();
- if (socket != null) socket.close();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
- }
- // ClientMain.java
- package Client;
- import Message.*;
- import StuffRelated.*;
- import java.io.*;
- import java.net.Socket;
- import java.util.Scanner;
- public class ClientMain {
- public static void main(String[] args) {
- try (Socket socket = new Socket("localhost", 12345);
- ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
- ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
- Scanner sc = new Scanner(System.in)) {
- Stuff currentUser = null;
- while (currentUser == null) {
- System.out.println("Select Role:");
- System.out.println("1. Manager");
- System.out.println("2. Registration Tester");
- System.out.println("3. Donation Officer");
- System.out.println("4. Preservation Officer");
- System.out.println("5. Delivery Tester");
- System.out.print("Enter role number: ");
- int role = Integer.parseInt(sc.nextLine());
- System.out.print("Enter PIN: ");
- String enteredPin = sc.nextLine();
- output.writeObject("LOGIN_REQUEST");
- output.writeObject(role);
- output.writeObject(enteredPin);
- Object response = input.readObject();
- if (response instanceof Stuff) {
- currentUser = (Stuff) response;
- System.out.println("Login successful!");
- } else {
- System.out.println("Login failed. Try again.\n");
- }
- }
- String x = currentUser.getRole();
- ClientMenuHandler.handleMenu(x, currentUser, output);
- } catch (Exception e) {
- System.out.println("Error in client: " + e.getMessage());
- e.printStackTrace();
- }
- }
- }
- // ClientMenuHandler.java
- package Client;
- import StuffRelated.*;
- import java.io.ObjectOutputStream;
- import java.util.Scanner;
- public class ClientMenuHandler {
- public static void handleMenu(String role, Stuff currentStuff, ObjectOutputStream out) {
- switch (role) {
- case "Manager" -> ManagerClient.run(currentStuff, out);
- case "RegTester" -> RegTesterClient.run(currentStuff, out);
- case "DonationOfficer" -> DonationOfficerClient.run(currentStuff, out);
- case "PreservationOfficer" -> PreservationOfficerClient.run(currentStuff, out);
- case "DeliveryTester" -> DeliveryTesterClient.run(currentStuff, out);
- default -> System.out.println("Invalid role. Cannot start client menu.");
- }
- }
- }
- // DeliveryTesterClient.java
- package Client;
- import StuffRelated.Stuff;
- import java.io.ObjectOutputStream;
- import java.util.Scanner;
- public class DeliveryTesterClient {
- public static void run(Stuff currentStuff, ObjectOutputStream out) {
- Scanner sc = new Scanner(System.in);
- while (true) {
- System.out.println("\nDelivery Tester Menu:");
- System.out.println("1. View Profile");
- System.out.println("2. Send CustomerRequest back and request unit");
- System.out.println("3. If none, send null");
- System.out.println("0. Logout");
- int choice = sc.nextInt(); sc.nextLine();
- switch (choice) {
- case 1:
- currentStuff.showInfo();
- break;
- case 2:
- break;
- case 3:
- break;
- case 0:
- System.out.println("Logging out...");
- return;
- default:
- System.out.println("Invalid choice.");
- }
- }
- }
- }
- // DonationOfficerClient.java
- package Client;
- import StuffRelated.Stuff;
- import java.io.ObjectOutputStream;
- import java.util.Scanner;
- public class DonationOfficerClient {
- public static void run(Stuff currentStuff, ObjectOutputStream out) {
- Scanner sc = new Scanner(System.in);
- while (true) {
- System.out.println("\nDonation Officer Menu:");
- System.out.println("1. View Profile");
- System.out.println("2. Forward donor info to Preservation Officer");
- System.out.println("0. Logout");
- int choice = sc.nextInt(); sc.nextLine();
- switch (choice) {
- case 1:
- break;
- case 2:
- break;
- case 0:
- System.out.println("Logging out...");
- return;
- default:
- System.out.println("Invalid choice.");
- }
- }
- }
- }
- // ManagerClient.java
- package Client;
- import StuffRelated.Stuff;
- import java.io.ObjectOutputStream;
- import java.util.Scanner;
- public class ManagerClient {
- public static void run(Stuff currentStuff, ObjectOutputStream out) {
- Scanner sc = new Scanner(System.in);
- while (true) {
- System.out.println("\nManager Menu:");
- System.out.println("1. Send blood request");
- System.out.println("2. View all staff info");
- System.out.println("3. View all kit quantities");
- System.out.println("4. View blood unit details");
- System.out.println("0. Logout");
- int choice = sc.nextInt(); sc.nextLine();
- switch (choice) {
- case 1:
- break;
- case 2:
- break;
- case 3:
- break;
- case 4:
- break;
- case 0:
- System.out.println("Logging out...");
- return;
- default:
- System.out.println("Invalid choice.");
- }
- }
- }
- }
- // PreservationOfficerClient.java
- package Client;
- import StuffRelated.Stuff;
- import java.io.ObjectOutputStream;
- import java.util.Scanner;
- public class PreservationOfficerClient {
- public static void run(Stuff currentStuff, ObjectOutputStream out) {
- Scanner sc = new Scanner(System.in);
- while (true) {
- System.out.println("\nPreservation Officer Menu:");
- System.out.println("1. View Profile");
- System.out.println("2. Enqueue blood unit from Donation Officer");
- System.out.println("3. Dequeue and send to DeliveryTester");
- System.out.println("4. Remove expired units");
- System.out.println("0. Logout");
- int choice = sc.nextInt(); sc.nextLine();
- switch (choice) {
- case 1:
- break;
- case 2:
- break;
- case 3:
- break;
- case 4:
- break;
- case 0:
- System.out.println("Logging out...");
- return;
- default:
- System.out.println("Invalid choice.");
- }
- }
- }
- }
- // RegTesterClient.java
- package Client;
- import StuffRelated.Stuff;
- import java.io.ObjectOutputStream;
- import java.util.Scanner;
- public class RegTesterClient {
- public static void run(Stuff currentStuff, ObjectOutputStream out) {
- Scanner sc = new Scanner(System.in);
- while (true) {
- System.out.println("\nRegTester Menu:");
- System.out.println("1. View Profile");
- System.out.println("2. Input Donor Info");
- System.out.println("0. Logout");
- int choice = sc.nextInt(); sc.nextLine();
- switch (choice) {
- case 1:
- break;
- case 2:
- break;
- case 0:
- System.out.println("Logging out...");
- return;
- default:
- System.out.println("Invalid choice.");
- }
- }
- }
- }
- // KitManagement.java
- package KitRelated;
- import java.io.*;
- import Message.*;
- public class KitManagement {
- private int numberOfBag;
- private int numberOfNeedle;
- private int numberOfPipe;
- private int numberOfTestingKit;
- public KitManagement(int x, int y, int z, int w) {
- numberOfBag = x;
- numberOfNeedle = y;
- numberOfPipe = z;
- numberOfTestingKit = w;
- }
- public void loadKitData() {
- try (BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\win11\\OneDrive\\Documents\\BUET program\\project java\\javaProject\\data\\KitManagement.csv"))) {
- String line = br.readLine();
- String[] parts = line.strip().split(",");
- int bag = Integer.parseInt(parts[0]);
- int needle = Integer.parseInt(parts[1]);
- int pipe = Integer.parseInt(parts[2]);
- int testKit = Integer.parseInt(parts[3]);
- KitManagement kit = new KitManagement(bag, needle, pipe, testKit);
- System.out.println("Kit data loaded successfully.");
- } catch (Exception e) {
- System.out.println("Error loading kit data: " + e.getMessage());
- }
- }
- public void setNumberOfBag(int numberOfBag) {
- this.numberOfBag = numberOfBag;
- }
- public void setNumberOfNeedle(int numberOfNeedle) {
- this.numberOfNeedle = numberOfNeedle;
- }
- public void saveToFile() {
- try (BufferedWriter writer = new BufferedWriter(new FileWriter("C:\\Users\\win11\\OneDrive\\Documents\\BUET program\\project java\\javaProject\\data\\KitManagement.csv"))) {
- writer.write(numberOfBag + "," + numberOfNeedle + "," + numberOfPipe + "," + numberOfTestingKit);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void setNumberOfPipe(int numberOfPipe) {
- this.numberOfPipe = numberOfPipe;
- }
- public void setNumberOfTestingKit(int numberOfTestingKit) {
- this.numberOfTestingKit = numberOfTestingKit;
- }
- public int getNumberOfBag() {
- return numberOfBag;
- }
- public int getNumberOfNeedle() {
- return numberOfNeedle;
- }
- public int getNumberOfPipe() {
- return numberOfPipe;
- }
- public int getNumberOfTestingKit(){return numberOfTestingKit;}
- public void showInfo() {
- System.out.println(numberOfBag + "\t" + numberOfNeedle + "\t" + numberOfPipe);
- }
- public KitMessage Alarm(KitManagement x) {
- KitMessage y = new KitMessage(x);
- return y;
- }
- }
- // AddingRequestMessage.java
- package Message;
- import BloodRelated.BloodUnit;
- import java.io.Serializable;
- public class AddingRequestMessage implements BaseMessage {
- private String from;
- private String to;
- private BloodUnit bloodUnit;
- public AddingRequestMessage(String from, String to, BloodUnit bloodUnit) {
- this.bloodUnit = bloodUnit;
- this.from = from;
- this.to = to;
- }
- public BloodUnit getBloodUnit() {
- return bloodUnit;
- }
- public String getFrom() {
- return from;
- }
- public String getTo() {
- return to;
- }
- @Override
- public String getSenderRole() {
- return from;
- }
- @Override
- public String getReceiverRole() {
- return to;
- }
- }
- // BaseMessage.java
- package Message;
- import java.io.Serializable;
- public interface BaseMessage extends Serializable {
- String getSenderRole();
- String getReceiverRole();
- }
- // CustomerRequestMessage.java
- package Message;
- public class CustomerRequestMessage implements BaseMessage {
- private String from;
- private String to;
- private String bloodGroup;
- public CustomerRequestMessage(String from, String to, String bloodGroup) {
- this.from = from;
- this.to = to;
- this.bloodGroup = bloodGroup;
- }
- public String getFrom() {
- return from;
- }
- public String getTo() {
- return to;
- }
- public String getBloodGroup() {
- return bloodGroup;
- }
- @Override
- public String getSenderRole() {
- return from;
- }
- @Override
- public String getReceiverRole() {
- return to;
- }
- }
- // KitMessage.java
- package Message;
- import KitRelated.KitManagement;
- public class KitMessage implements BaseMessage {
- public String Message = "";
- private static final String newLine = System.lineSeparator();
- public KitMessage(KitManagement km) {
- if(km.getNumberOfBag() == 0)
- Message += "No bag left.";
- if(km.getNumberOfNeedle() == 0) {
- Message += newLine + "No needle left.";
- }
- if (km.getNumberOfPipe() == 0) {
- Message += "No pipe left.";
- }
- if (km.getNumberOfTestingKit() == 1) {
- Message += "No testing kit left.";
- }
- }
- @Override
- public String getSenderRole() {
- return "Server";
- }
- @Override
- public String getReceiverRole() {
- return "Manager";
- }
- }
- // StockAlertMessage.java
- package Message;
- public class StockAlertMessage implements BaseMessage {
- private String message;
- public StockAlertMessage(String bloodGroup) {
- this.message = "No unit available for blood group: " + bloodGroup;
- }
- public String getMessage() {
- return message;
- }
- @Override
- public String toString() {
- return message;
- }
- public String getSenderRole() {
- return "Server";
- }
- @Override
- public String getReceiverRole() {
- return "Manager";
- }
- }
- // Server.java
- package Server;
- import java.io.*;
- import java.net.*;
- import java.util.*;
- import Message.*;
- import StuffRelated.*;
- import BloodRelated.*;
- import KitRelated.*;
- public class Server {
- private static final int PORT = 12345;
- private static Map<String, ObjectOutputStream> clientOutputStreams = new HashMap<>();
- private static StuffList staffList = new StuffList();
- private static BloodUnitList bloodUnitList = new BloodUnitList();
- private static KitManagement kit = new KitManagement(10, 10, 10, 10);
- public static void main(String[] args) throws IOException {
- ServerSocket serverSocket = new ServerSocket(PORT);
- System.out.println("Server started on port " + PORT);
- staffList.loadFromFile("data/stufflist.csv");
- bloodUnitList.loadAllFromFiles("data");
- while (true) {
- Socket clientSocket = serverSocket.accept();
- System.out.println("Client connected: " + clientSocket);
- new ClientHandler(clientSocket).start();
- }
- }
- static class ClientHandler extends Thread {
- private Socket socket;
- private ObjectOutputStream out;
- private ObjectInputStream in;
- private Stuff loggedInUser;
- public ClientHandler(Socket socket) {
- this.socket = socket;
- }
- public void run() {
- try {
- out = new ObjectOutputStream(socket.getOutputStream());
- in = new ObjectInputStream(socket.getInputStream());
- while (true) {
- Object obj = in.readObject();
- if (obj instanceof String) {
- String role = (String) obj;
- out.writeObject("Enter PIN:");
- out.flush();
- String pin = (String) in.readObject();
- Stuff user = staffList.getStuffByRole(role);
- if (user != null && pin.equals(user.getPin())) {
- user.login(pin);
- loggedInUser = user;
- clientOutputStreams.put(role, out);
- out.writeObject("Login successful as " + role);
- } else {
- out.writeObject("Login failed. Try again.");
- }
- out.flush();
- } else if (obj instanceof BaseMessage) {
- BaseMessage msg = (BaseMessage) obj;
- String target = msg.getReceiverRole();
- ObjectOutputStream targetStream = clientOutputStreams.get(target);
- if (targetStream != null) {
- targetStream.writeObject(msg);
- targetStream.flush();
- } else {
- System.out.println("Target role offline or not registered: " + target);
- }
- }
- }
- } catch (IOException | ClassNotFoundException e) {
- System.out.println("Client disconnected or error: " + e.getMessage());
- } finally {
- try {
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (loggedInUser != null) {
- clientOutputStreams.remove(loggedInUser.getRole());
- loggedInUser.logout();
- }
- }
- }
- }
- }
- // DeliveryTester.java
- package StuffRelated;
- import BloodRelated.BloodUnit;
- import KitRelated.KitManagement;
- import Message.AddingRequestMessage;
- import Message.CustomerRequestMessage;
- public class DeliveryTester extends Stuff {
- public DeliveryTester(String name, String id, String contact, String pin) {
- super(name, id, contact, pin);
- setRole("Delivery Tester");
- }
- public void profilePresenter() {
- showInfo();
- }
- public void kitInfoUpdater(int usedTestingKit, KitManagement x) {
- x.setNumberOfTestingKit(x.getNumberOfTestingKit() - usedTestingKit);
- x.saveToFile();
- }
- public CustomerRequestMessage testBloodUnit(AddingRequestMessage bloodUnit, boolean passed) {
- if (passed) {
- System.out.println("Blood test passed for donor: " + bloodUnit.getBloodUnit().getBloodGroup());
- return null;
- } else {
- return new CustomerRequestMessage("DeliveryTester", "PreservationOfficer", bloodUnit.getBloodUnit().getBloodGroup());
- }
- }
- }
- // DonationOfficer.java
- package StuffRelated;
- import BloodRelated.BloodUnit;
- import BloodRelated.BloodUnitList;
- import KitRelated.KitManagement;
- import Message.AddingRequestMessage;
- import java.util.ArrayList;
- public class DonationOfficer extends Stuff {
- public DonationOfficer(String name, String id, String contact, String pin) {
- super(name, id, contact, pin);
- setRole("Donation Officer");
- }
- public void kitInfoUpdater(int usedPipe, int usedBag, KitManagement x) {
- x.setNumberOfBag(x.getNumberOfBag() - usedBag);
- x.setNumberOfPipe(x.getNumberOfPipe() - usedPipe);
- x.saveToFile();
- }
- public void profileShower() {
- showInfo();
- }
- public AddingRequestMessage anqueueApproval(boolean approved, BloodUnit bloodUnit) {
- if(approved)
- return new AddingRequestMessage("DonationOfficer", "DonationOfficer", bloodUnit);
- return null;
- }
- }
- // Manager.java
- package StuffRelated;
- import BloodRelated.BloodUnit;
- import BloodRelated.BloodUnitList;
- import KitRelated.KitManagement;
- import Message.CustomerRequestMessage;
- import java.util.ArrayList;
- import java.util.List;
- public class Manager extends Stuff {
- public Manager(String name, String id, String contact, String pin) {
- super(name, id, contact, pin);
- setRole("Manager");
- }
- public void profilePresenter() {
- showInfo();
- }
- public void showList(StuffList x) {
- x.printAll();
- }
- public void receiveKit(KitManagement x, int a, int b, int c, int d) {
- x.setNumberOfBag(x.getNumberOfBag() + a);
- x.setNumberOfNeedle(x.getNumberOfNeedle() + b);
- x.setNumberOfPipe(x.getNumberOfPipe() + c);
- x.setNumberOfTestingKit(x.getNumberOfTestingKit() + d);
- }
- public void showBloodInfo(BloodUnitList x) {
- String[] groups = {"A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-"};
- for (String group : groups) {
- List<BloodUnit> list = x.fetchBlood(group);
- for (BloodUnit unit : list) {
- unit.showInfo();
- }
- }
- }
- public CustomerRequestMessage CustomerRequest(String bg) {
- return new CustomerRequestMessage("Manager", "PreservationOfficer", bg);
- }
- }
- // PreservationOfficer.java
- package StuffRelated;
- import BloodRelated.BloodUnit;
- import BloodRelated.BloodUnitList;
- import Message.AddingRequestMessage;
- import Message.CustomerRequestMessage;
- import java.util.ArrayList;
- import java.util.List;
- public class PreservationOfficer extends Stuff {
- public PreservationOfficer(String name, String id, String phone, String pin) {
- super(name, id, phone, pin);
- }
- public void checkExpiredUnits(BloodUnitList bloodList) {
- List<BloodUnit> expired = bloodList.getExpiredUnits();
- if (expired.isEmpty()) {
- System.out.println(" No expired blood units.");
- } else {
- System.out.println("️ Expired blood units:");
- for (BloodUnit unit : expired) {
- System.out.println(unit);
- }
- }
- }
- public void enqueueBloodUnit(AddingRequestMessage bloodUnit, BloodUnitList bloodUnitList) {
- List<BloodUnit> groupList = bloodUnitList.fetchBlood(bloodUnit.getBloodUnit().getBloodGroup());
- BloodUnit blood = bloodUnit.getBloodUnit();
- if (groupList != null) {
- groupList.add(blood);
- System.out.println("Blood unit added for group " + blood.getBloodGroup());
- } else {
- System.out.println("Blood group list not found.");
- }
- }
- public AddingRequestMessage dequeueBloodUnit(AddingRequestMessage bloodUnit, BloodUnitList bloodUnitList) {
- ArrayList<BloodUnit> x = bloodUnitList.fetchBlood(bloodUnit.getBloodUnit().getBloodGroup());
- BloodUnit y = bloodUnitList.dequeue(x);
- return new AddingRequestMessage("PreservationOfficer","DeliveryTester",y);
- }
- public void removeExpiredBloodUnits(BloodUnitList bloodUnitList) {
- String[] groups = {"A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-"};
- for (String group : groups) {
- ArrayList<BloodUnit> list = bloodUnitList.fetchBlood(group);
- for (int i = 0; i < list.size(); i++) {
- BloodUnit unit = list.get(i);
- if (unit.isExpired()) {
- list.remove(i);
- }
- else break;
- }
- }
- bloodUnitList.saveAllToFiles("C:\\Users\\win11\\OneDrive\\Documents\\BUET program\\project java\\javaProject\\data");
- System.out.println("All expired blood units removed successfully.");
- }
- public void removeBloodUnit(BloodUnitList bloodUnitList, String bloodGroup, int[] selectedIDX) {
- ArrayList<BloodUnit> units = bloodUnitList.fetchBlood(bloodGroup);
- for(int i=0; i<selectedIDX.length; i++) {
- bloodUnitList.leave(units, selectedIDX[i]);
- for(int j=i+1; j<selectedIDX.length; j++) {
- if(selectedIDX[j] > selectedIDX[i]) {
- selectedIDX[j]--;
- }
- }
- }
- }
- }
- // RegTester.java
- package StuffRelated;
- import BloodRelated.BloodUnit;
- import BloodRelated.BloodUnitList;
- import BloodRelated.Person;
- import KitRelated.KitManagement;
- import Message.AddingRequestMessage;
- import java.time.LocalDate;
- public class RegTester extends Stuff {
- private Person donor;
- public RegTester(String name, String id, String contact, String pin) {
- super(name, id, contact, pin);
- donor = null;
- setRole("Registration Tester");
- }
- public Person registerDonor(String name, String id, String bg) {
- donor = new Person(name, id, bg);
- return donor;
- }
- public AddingRequestMessage checkHealth(boolean x, Person person) {
- if(x) {
- LocalDate today = LocalDate.now();
- LocalDate expiry = today.plusDays(14);
- BloodUnit q = new BloodUnit(person.getName(), person.getNID(), person.getBloodGroup(), today, expiry);
- return new AddingRequestMessage("RegTester","DonationOfficer",q);
- }
- return null;
- }
- public void kitInfoUpdater(int usedTestingKit, KitManagement x) {
- x.setNumberOfTestingKit(x.getNumberOfTestingKit() - usedTestingKit);
- x.saveToFile();
- }
- }
- // Stuff.java
- package StuffRelated;
- public class Stuff {
- private String name;
- private String id;
- private String phone;
- private String pin;
- private boolean isLoggedIn;
- private String role = "Undefined";
- public Stuff(String name, String id, String phone, String pin) {
- this.name = name;
- this.id = id;
- this.phone = phone;
- this.pin = pin;
- this.isLoggedIn = false;
- }
- public String getName() { return name; }
- public String getId() { return id; }
- public String getPhone() { return phone; }
- public String getPin() { return pin; }
- public boolean isLoggedIn() { return isLoggedIn; }
- public String getRole() { return role; }
- public void setRole(String role) {
- this.role = role;
- }
- public void login(String pin) {
- if(pin.equals(this.pin)) {
- this.isLoggedIn = true;
- System.out.println(name + " logged in.");
- }
- }
- public void logout() {
- this.isLoggedIn = false;
- System.out.println(name + " logged out.");
- }
- public void showInfo() {
- System.out.println("Name: " + name);
- System.out.println("ID: " + id);
- System.out.println("Phone: " + phone);
- System.out.println("Role: " + role);
- System.out.println("Login Status: " + (isLoggedIn ? "Logged In" : "Logged Out"));
- }
- @Override
- public String toString() {
- return name + " (" + id + "), Role: " + role + ", Status: " + (isLoggedIn ? "In" : "Out");
- }
- }
- // StuffList.java
- package StuffRelated;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.List;
- public class StuffList {
- private List<Stuff> stuff = new ArrayList<>();
- public void loadFromFile(String filename) {
- try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
- String line;
- int i = 0;
- while ((line = br.readLine()) != null && i < 5) {
- String[] parts = line.split(",", -1);
- if (parts.length >= 4) {
- String name = parts[0];
- String id = parts[1];
- String phone = parts[2];
- String pin = parts[3];
- Stuff s = null;
- if (i == 0) {
- s = new Manager(name, id, phone, pin);
- } else if (i == 1) {
- s = new RegTester(name, id, phone, pin);
- } else if (i == 2) {
- s = new DonationOfficer(name, id, phone, pin);
- } else if (i == 3) {
- s = new PreservationOfficer(name, id, phone, pin);
- } else if (i == 4) {
- s = new DeliveryTester(name, id, phone, pin);
- }
- if (s != null) stuff.add(s);
- i++;
- }
- }
- } catch (IOException e) {
- System.err.println("Error loading staff from file: " + e.getMessage());
- }
- }
- public Stuff getById(String id) {
- for (Stuff s : stuff) {
- if (s.getId().equals(id)) return s;
- }
- return null;
- }
- public List<Stuff> getAll() {
- return stuff;
- }
- public void printAll() {
- for (Stuff s : stuff) {
- System.out.println(s);
- }
- }
- public Stuff getStuffByRole(String role) {
- for(int i=0; i<stuff.size(); i++) {
- if(stuff.get(i).getRole() == role)
- return stuff.get(i);
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment