Skip to content
Snippets Groups Projects
ReceiveThread.java 3.43 KiB
Newer Older
  • Learn to ignore specific revisions
  • package com.example.schedulerapp;
    
    import java.io.*;
    import java.net.*;
    
    
    public class ReceiveThread extends Thread {
    
        private BufferedReader reader;
    
    eyan_'s avatar
    eyan_ committed
        private final Model client;
    
    eyan_'s avatar
    eyan_ committed
        public ReceiveThread(Socket socket, Model client) {
    
    
            try {
                InputStream input = socket.getInputStream();
                reader = new BufferedReader(new InputStreamReader(input));
    
            } catch (IOException exception) {
                System.out.println("Error getting input stream: " + exception.getMessage());
                exception.printStackTrace();
    
            }
        }
    
        public void run() {
            while(true) {
                try {
    
                    //System.out.println("ReceiveThread.Run.Hello"); why does this run twice.
    
                    String response = reader.readLine();
    
                    String[] args = response.split("/");
                    switch (args[0]) {
    
                        case "allEmployees" -> allEmployees(args);
                        case "allShifts" -> allShifts(args);
                        case "addEmployee" -> addEmployee(args[1]);
                        case "removeEmployee" -> removeEmployee(args[1]);
                        case "addShift" -> addShift(args[1]);
                        case "removeShift" -> removeShiftByIDL(args[1]);
                        case "editShift" -> editShiftL(args[1]); // fix
    
                    System.out.println("Dropped connection from server: " + exception.getMessage());
    
    
        private void allEmployees(String[] allEmployees) {
            for (String employee : allEmployees) {
                if (!employee.equals("allEmployees")){
                   addEmployee(employee);
                }
            }
        }
    
        private void addEmployee(String employeeData) {
            try {
                String[] dataSplit = employeeData.split("\\.");
                this.client.employees.put(Integer.parseInt(dataSplit[0]), new Employee(employeeData));
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
    
        private void removeEmployee(String employeeID) {
            try {
                this.client.employees.remove(Integer.parseInt(employeeID));
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
    
        private void allShifts(String[] allShifts) {
            for (String shift : allShifts) {
                if (!shift.equals("allShifts")){
                    addShift(shift);
                }
            }
        }
    
        private void addShift(String shiftData) {
            try {
                String[] dataSplit = shiftData.split("\\.");
                this.client.shifts.put(Integer.parseInt(dataSplit[4]), new Shift(shiftData));
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
    
        private void removeShiftByIDL(String id) {
            try {
                this.client.shifts.remove(Integer.parseInt(id));
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
    
        private void editShiftL(String editedShift) {
            String[] shiftSplit = editedShift.split("\\.");
            try {
                int shiftID = Integer.parseInt(shiftSplit[4]);
                this.client.shifts.remove(shiftID);
                this.client.shifts.put(shiftID, new Shift(editedShift));
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }