"...git@git.cs.usask.ca:esc568/cmpt-370-group-project.git" did not exist on "7cf10e529e9134ba064e7e89b7dad916edc62ff4"
Newer
Older
import java.net.*;
import java.io.*;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
private PrintWriter writer;
private Socket socket;
protected ArrayList<String> shifts;
protected ArrayList<String> employees;
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
public Model(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());
}
}
//todo
//Return
void getAllEmployees() {
}
void updateAllShifts(){
writer.println("allShifts");
}
void updateAllEmployees(){
writer.println("allEmployees");
}
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);
}
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
void removeShift(int id, String date, int start, int end){
writer.println("removeShift/"+id+"/"+date+"/"+start+"/"+end);
}
void printAllShifts() {
System.out.println(this.shifts);
}
/*
Parameters:
startOfWeek - The first day of the week in "yyyy-mm-dd" format
endOfWeek - The last day of the week in "yyyy-mm-dd" format
Return: ArrayList<String>
*/
ArrayList<String> getShiftsByWeek(String startOfWeek, String endOfWeek){
String[] startSplit = startOfWeek.split("-");
String[] endSplit = endOfWeek.split("-");
ArrayList<String> output = new ArrayList<>();
for(String shift: this.shifts){
if(!shift.equals("allShifts")){
String[] shiftSplit = shift.split("\\.");
String[] dateSplit = shiftSplit[1].split("-");
try {
if(Integer.parseInt(startSplit[0]) >= Integer.parseInt(dateSplit[0]) &&
Integer.parseInt(startSplit[1]) >= Integer.parseInt(dateSplit[1]) &&
Integer.parseInt(startSplit[2]) >= Integer.parseInt(dateSplit[2])){
if(Integer.parseInt(endSplit[0]) >= Integer.parseInt(dateSplit[0]) &&
Integer.parseInt(endSplit[1]) >= Integer.parseInt(dateSplit[1]) &&
Integer.parseInt(endSplit[2]) >= Integer.parseInt(dateSplit[2])){
output.add(shift);
}
}
} catch (Exception e){
System.out.println("Error: Parsing string to int");
}
}
}
return output;
}
//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
Model client = new Model(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!");
}
public ArrayList<String> returnFormattedEmployeeNames(){
ArrayList<String> result = new ArrayList<>();
ArrayList<String> temp = this.employees;
String tempName = "";
temp.remove(0);
for(int i = 0; i < temp.size(); i++){
String[] templist = temp.get(i).split("\\.");
for(int j =0; j < 3; j++){
if(j == 1 || j == 2){
tempName += templist[j];
}
if(j == 1){
tempName += " ";
}
}
result.add(tempName);
tempName = "";
}
System.out.println(result);
return result;