Skip to content
Snippets Groups Projects
ScheduleClient.java 3.77 KiB
package com.example.schedulerapp;

import java.net.*;
import java.io.*;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class ScheduleClient {
    private PrintWriter writer;
    private Socket socket;
    protected ArrayList<String> shifts;
    protected ArrayList<String> employees;

    public ScheduleClient(String hostname, int port) {
        this.shifts =  new ArrayList<>();
        this.employees = new ArrayList<>();
        try {
            InetAddress ip = InetAddress.getByName(hostname);
            this.socket = new Socket(ip, port);
            System.out.println("Connected to the scheduling server");

            new ReceiveThread(this.socket, this).start();

            OutputStream output = this.socket.getOutputStream();
            this.writer = new PrintWriter(output, true);

        } catch (UnknownHostException error) {
            System.out.println("Server not found: " + error.getMessage());
        } catch (IOException error) {
            System.out.println("I/O Error: " + error.getMessage());
        }
    }

    void getAllShifts(){
        writer.println("allEmployees");
    }

    void getAllEmployees(){
        writer.println("allShifts");
    }

    void addEmployee(String first_name, String last_name) {
        writer.println("addEmployee/" + first_name + "/" + last_name);
    }

    void removeEmployee(String first_name, String last_name) {
        writer.println("removeEmployee/" + first_name + "/" + last_name);
    }

    void printAllEmployees(){
        System.out.println(this.employees);
    }

    String getEmployeeByID(int id) {
        if (!this.employees.isEmpty()) {
            for(String e: this.employees){
                if(!e.equals("allEmployees")){
                    String[] eSplit = e.split("\\.");
                    try {
                        if(Integer.parseInt(eSplit[0]) == id){
                            return eSplit[1] + " " + eSplit[2];
                        }
                    } catch (NumberFormatException exception){
                        System.out.println("getEmployeeByID: string cannot be integer parsed");
                    }

                }
            }
            return "No Employee with that ID";
        }
        return "No Employees";
    }

    void addShift(int id, String date, int start, int end){
        writer.println("addShift/"+id+"/"+date+"/"+start+"/"+end);
    }

    void removeShift(int id, String date, int start, int end){
        writer.println("removeShift/"+id+"/"+date+"/"+start+"/"+end);
    }

    void printAllShifts() {
        System.out.println(this.shifts);
    }

    //todo
    String getShiftsByWeek(){
        return "Need to implement";
    }

    //todo
    String getEmployeeShiftsByWeek(){
        return "Need to implement";
    }

    void logOut() {
        writer.println("logout");
        try {
            this.socket.close();
        } catch (IOException ex){
            System.out.println("Error: Failed to close socket");
        }
    }

    public static void main(String[] args) {
        String hostname = "localhost";  //"localhost if connecting to local server, or "172.16.1.99" connecting to Rafi's server
        int port = 8989; //Rafi's server port is 8989
        ScheduleClient client = new ScheduleClient(hostname, port);
        System.out.print("Retrieving Data");
        while(client.employees.isEmpty() && client.shifts.isEmpty()){
            System.out.print(".");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                System.out.println("Error: ScheduleClient main. Interrupted while retrieving data ");
            }
        }
        System.out.println();
        System.out.println("Data retrieved.");
        System.out.println("ScheduleClient is ready to use!");
    }
}