-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathCommandHandler.java
More file actions
182 lines (161 loc) · 5.79 KB
/
CommandHandler.java
File metadata and controls
182 lines (161 loc) · 5.79 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package com.example;
import java.util.*;
public class CommandHandler {
private List<Student> students;
private List<Teacher> teachers;
private List<Course> courses;
public CommandHandler(List<Student> students, List<Course> courses, List<Teacher> teachers) {
this.students = students;
this.courses = courses;
this.teachers = teachers;
}
public void executeCommand(String wholeCommand){
String[] commandPart = wholeCommand.split(" ");
String command = commandPart[0];
switch (command){
case "ENROLL":
enrollStudent(commandPart[1],commandPart[2]);
break;
case "ASSIGN":
assignTeacher(commandPart[1],commandPart[2]);
break;
case "SHOW":
System.out.println(command);
handleShow(commandPart);
break;
case "LOOKUP":
lookUp(commandPart);
break;
}
}
public Student findStudentById(String student_id){
Student student = null;
for(Student s: students){
if(s.getStudentId().equals(student_id)){
student = s;
return student;
}
}
return null;
}
public Course findCourseById(String course_id){
Course course = null;
for(Course c: courses){
if(c.getCourseId().equals(course_id)){
course = c;
return course;
}
}
return null;
}
public Teacher findTeacherById(String teacher_id){
Teacher teacher = null;
for(Teacher t: teachers){
if(t.getTeacherId().equals(teacher_id)){
teacher = t;
return teacher;
}
}
return null;
}
private void lookUp(String[] commandPart) {
switch (commandPart[1]){
case "COURSE":
Course course = findCourseById(commandPart[2]);
if (course != null)
System.out.println(course.toString());
else
System.out.println("Course not found.");
break;
case "STUDENT":
Student student = findStudentById(commandPart[2]);
if (student != null)
System.out.println(student.toString());
else
System.out.println("Student not found.");
break;
case "TEACHER":
Teacher teacher = findTeacherById(commandPart[2]);
if (teacher != null)
System.out.println(teacher.toString());
else
System.out.println("Teacher not found.");
break;
}
}
private void enrollStudent(String student_id, String course_id) {
Student student = findStudentById(student_id);
Course course = findCourseById(course_id);
if (student != null && course!= null) {
student.setCourse(course);
updateMoneyEarned(course);
System.out.println(student.toString());
} else {
System.out.println("Couldn't assign the course");
}
}
public void assignTeacher(String teacher_id, String course_id){
Teacher teacher = findTeacherById(teacher_id);
Course course = findCourseById(course_id);
if (teacher != null && course!=null){
course.setTeacher(teacher);
System.out.println(course.toString());
}else {
System.out.println("Couldn't assign the course");
}
}
private void updateMoneyEarned(Course course){
double total = course.getMoney_earned() + course.getPrice();
course.setMoney_earned(total);
}
private void handleShow(String[] command) {
System.out.println(Arrays.toString(command) + "del handle");
switch (command[1]){
case "COURSES":
showCourses();
break;
case "STUDENTS":
showStudents();
break;
case "TEACHERS":
showTeachers();
break;
case "PROFIT":
showProfit();
break;
}
}
private void showStudents() {
System.out.println("llego a show students");
StringBuilder studentsString = new StringBuilder();
for (Student student: students){
studentsString.append(student.toString()).append("\n");
}
System.out.println("Student usando el toString() es:");
System.out.println(studentsString);
}
private void showTeachers() {
StringBuilder teachersString = new StringBuilder();
for (Teacher teacher: teachers){
teachersString.append(teacher.toString()).append("\n");
}
System.out.println(teachersString);
}
private void showProfit() {
double totalEarned = courses.stream().mapToDouble(Course::getMoney_earned).sum();
double totalSalaries = teachers.stream().mapToDouble(Teacher::getSalary).sum();
System.out.println("===== FINANCIAL REPORT =====");
System.out.println("Total income from courses: $" + totalEarned);
System.out.println("Total expenses (teachers' salaries): $" + totalSalaries);
System.out.println("------------------------------");
System.out.printf("Net profit: $%.2f%n", (totalEarned - totalSalaries));
System.out.println("==============================");
}
private void showCourses() {
StringBuilder coursesString = new StringBuilder();
for (Course course: courses){
coursesString.append(course.toString()).append("\n");
}
System.out.println(coursesString);
}
}