|
| 1 | + |
| 2 | +import java.util.ArrayList; |
| 3 | +import java.util.Collections; |
| 4 | + |
| 5 | +/** |
| 6 | + * |
| 7 | + * @author THAYCACAC |
| 8 | + */ |
| 9 | +public class Manager { |
| 10 | + |
| 11 | + //Show menu |
| 12 | + public static void menu() { |
| 13 | + System.out.println(" 1. Create"); |
| 14 | + System.out.println(" 2. Find and Sort"); |
| 15 | + System.out.println(" 3. Update/Delete"); |
| 16 | + System.out.println(" 4. Report"); |
| 17 | + System.out.println(" 5. Exit"); |
| 18 | + System.out.print(" Enter your choice: "); |
| 19 | + } |
| 20 | + |
| 21 | + //Allow user create new student |
| 22 | + public static void createStudent(int count, ArrayList<Student> ls) { |
| 23 | + //if number of students greater than 10 ask user continue or not |
| 24 | + if (count > 10) { |
| 25 | + System.out.print("Do you want to continue (Y/N): "); |
| 26 | + if (!Validation.checkInputYN()) { |
| 27 | + return; |
| 28 | + } |
| 29 | + } |
| 30 | + //loop until user input not duplicate |
| 31 | + while (true) { |
| 32 | + System.out.print("Enter id: "); |
| 33 | + String id = Validation.checkInputString(); |
| 34 | + System.out.print("Enter name student: "); |
| 35 | + String name = Validation.checkInputString(); |
| 36 | + if (!Validation.checkIdExist(ls, id, name)) { |
| 37 | + System.err.println("Id has exist student. Pleas re-input."); |
| 38 | + continue; |
| 39 | + } |
| 40 | + System.out.print("Enter semester: "); |
| 41 | + String semester = Validation.checkInputString(); |
| 42 | + System.out.print("Enter name course: "); |
| 43 | + String course = Validation.checkInputCourse(); |
| 44 | + //check student exist or not |
| 45 | + if (Validation.checkStudentExist(ls, id, name, semester, course)) { |
| 46 | + ls.add(new Student(id, name, semester, course)); |
| 47 | + count++; |
| 48 | + System.out.println("Add student success."); |
| 49 | + return; |
| 50 | + } |
| 51 | + System.err.println("Duplicate."); |
| 52 | + |
| 53 | + } |
| 54 | + } |
| 55 | + //Allow user create find and sort |
| 56 | + |
| 57 | + public static void findAndSort(ArrayList<Student> ls) { |
| 58 | + //check list empty |
| 59 | + if (ls.isEmpty()) { |
| 60 | + System.err.println("List empty."); |
| 61 | + return; |
| 62 | + } |
| 63 | + ArrayList<Student> listStudentFindByName = listStudentFindByName(ls); |
| 64 | + if (listStudentFindByName.isEmpty()) { |
| 65 | + System.err.println("Not exist."); |
| 66 | + } else { |
| 67 | + Collections.sort(listStudentFindByName); |
| 68 | + System.out.printf("%-15s%-15s%-15s\n", "Student name", "semester", "Course Name"); |
| 69 | + //loop from first to last element of list student |
| 70 | + for (Student student : listStudentFindByName) { |
| 71 | + student.print(); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + //List student found by name |
| 77 | + public static ArrayList<Student> listStudentFindByName(ArrayList<Student> ls) { |
| 78 | + ArrayList<Student> listStudentFindByName = new ArrayList<>(); |
| 79 | + System.out.print("Enter name to search: "); |
| 80 | + String name = Validation.checkInputString(); |
| 81 | + for (Student student : ls) { |
| 82 | + //check student have name contains input |
| 83 | + if (student.getStudentName().contains(name)) { |
| 84 | + listStudentFindByName.add(student); |
| 85 | + } |
| 86 | + } |
| 87 | + return listStudentFindByName; |
| 88 | + } |
| 89 | + |
| 90 | + //Allow user update and delete |
| 91 | + public static void updateOrDelete(int count, ArrayList<Student> ls) { |
| 92 | + //if list empty |
| 93 | + if (ls.isEmpty()) { |
| 94 | + System.err.println("List empty."); |
| 95 | + return; |
| 96 | + } |
| 97 | + System.out.print("Enter id: "); |
| 98 | + String id = Validation.checkInputString(); |
| 99 | + ArrayList<Student> listStudentFindByName = getListStudentById(ls, id); |
| 100 | + //check list empty |
| 101 | + if (listStudentFindByName.isEmpty()) { |
| 102 | + System.err.println("Not found student."); |
| 103 | + return; |
| 104 | + } else { |
| 105 | + Student student = getStudentByListFound(listStudentFindByName); |
| 106 | + System.out.print("Do you want to update (U) or delete (D) student: "); |
| 107 | + //check user want to update or delete |
| 108 | + if (Validation.checkInputUD()) { |
| 109 | + System.out.print("Enter name student: "); |
| 110 | + String name = Validation.checkInputString(); |
| 111 | + System.out.print("Enter semester: "); |
| 112 | + String semester = Validation.checkInputString(); |
| 113 | + System.out.print("Enter name course: "); |
| 114 | + String course = Validation.checkInputCourse(); |
| 115 | + //check student exist or not |
| 116 | + if (Validation.checkStudentExist(ls, id, name, semester, course)) { |
| 117 | + student.setStudentName(name); |
| 118 | + student.setSemester(semester); |
| 119 | + student.setCourseName(course); |
| 120 | + System.err.println("Update success."); |
| 121 | + } |
| 122 | + return; |
| 123 | + } else { |
| 124 | + ls.remove(student); |
| 125 | + count--; |
| 126 | + System.err.println("Delete success."); |
| 127 | + return; |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + //Get student user want to update/delete in list found |
| 133 | + public static Student getStudentByListFound(ArrayList<Student> listStudentFindByName) { |
| 134 | + System.out.println("List student found: "); |
| 135 | + int count = 1; |
| 136 | + System.out.printf("%-10s%-15s%-15s%-15s\n", "Number", "Student name", |
| 137 | + "semester", "Course Name"); |
| 138 | + //display list student found |
| 139 | + for (Student student : listStudentFindByName) { |
| 140 | + System.out.printf("%-10d%-15s%-15s%-15s\n", count, |
| 141 | + student.getStudentName(), student.getSemester(), |
| 142 | + student.getCourseName()); |
| 143 | + count++; |
| 144 | + } |
| 145 | + System.out.print("Enter student: "); |
| 146 | + int choice = Validation.checkInputIntLimit(1, listStudentFindByName.size()); |
| 147 | + return listStudentFindByName.get(choice - 1); |
| 148 | + } |
| 149 | + |
| 150 | + //Get list student find by id |
| 151 | + public static ArrayList<Student> getListStudentById(ArrayList<Student> ls, String id) { |
| 152 | + ArrayList<Student> getListStudentById = new ArrayList<>(); |
| 153 | + for (Student student : ls) { |
| 154 | + if (id.equalsIgnoreCase(student.getId())) { |
| 155 | + getListStudentById.add(student); |
| 156 | + } |
| 157 | + } |
| 158 | + return getListStudentById; |
| 159 | + } |
| 160 | + |
| 161 | + //Print report |
| 162 | + public static void report(ArrayList<Student> ls) { |
| 163 | + if (ls.isEmpty()) { |
| 164 | + System.err.println("List empty."); |
| 165 | + return; |
| 166 | + } |
| 167 | + ArrayList<Report> lr = new ArrayList<>(); |
| 168 | + int size = ls.size(); |
| 169 | + for (int i = 0; i < size; i++) { |
| 170 | + int total = 0; |
| 171 | + for (Student student1 : ls) { |
| 172 | + for (Student student2 : ls) { |
| 173 | + if (student1.getId().equalsIgnoreCase(student2.getId()) |
| 174 | + && student1.getCourseName().equalsIgnoreCase(student2.getCourseName())) { |
| 175 | + total++; |
| 176 | + } |
| 177 | + } |
| 178 | + if (Validation.checkReportExist(lr, student1.getStudentName(), |
| 179 | + student1.getCourseName(), total)) { |
| 180 | + lr.add(new Report(student1.getStudentName(), |
| 181 | + student1.getCourseName(), total)); |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + //print report |
| 186 | + for (int i = 0; i < lr.size(); i++) { |
| 187 | + System.out.printf("%-15s|%-10s|%-5d\n", lr.get(i).getStudentName(), |
| 188 | + lr.get(i).getCourseName(), lr.get(i).getTotalCourse()); |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments