-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentService.java
More file actions
124 lines (116 loc) · 2.8 KB
/
StudentService.java
File metadata and controls
124 lines (116 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
82
83
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
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class StudentService implements Runnable
{
private Socket _socket;
private Classroom _classroom;
private Scanner in;
private PrintWriter out;
public StudentService(Socket socket, Classroom classroom)
{
_socket = socket;
_classroom = classroom;
}
public void run()
{
try
{
try
{
in = new Scanner(_socket.getInputStream());
out = new PrintWriter(_socket.getOutputStream());
doService();
}
finally
{
_socket.close();
}
}
catch (IOException exception)
{
exception.printStackTrace();
}
}
public void doService() throws IOException
{
try
{
while (true)
{
if (!in.hasNext())
{
return;
}
String command = in.next();
if (command.equals("quit"))
{
out.println("Session Ending");
out.flush();
return;
}
else
{
String result = executeCommand(command);
out.println(result);
out.flush();
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public String executeCommand(String command)
{
//String account = in.next();
String result;
try
{
if (command.equals("addstudent"))
{
String name = in.nextLine();
double gpa = in.nextDouble();
result = addStudent(name, gpa);
return result;
}
else if (command.equals("student"))
{
int id = in.nextInt();
Student student = _classroom.getStudent(id);
return student.toString();
}
else if (command.equals("studentlist"))
{
result = getStudentList();
return result;
}
else if (command.equals("averagegpa"))
{
result = _classroom.getAverageGpa();
return result;
}
return "";
}
catch(Exception ex)
{
return "";
}
}
private String getStudentList()
{
String buffer = "";
for (Student student : _classroom.getStudents())
{
buffer += student + "\n";
}
return buffer;
}
private String addStudent(String name, Double gpa)
{
int id = _classroom.AddStudent(name, gpa);
return Integer.toString(id);
}
}