Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.TreeSet;
- import model.Report;
- import model.Student;
- /**
- *
- * @author phuon
- */
- public class Manager {
- public void displayMenu() {
- System.out.println("WELCOME TO STUDENT MANAGEMENT");
- System.out.println(" 1. Create");
- System.out.println(" 2. Find and Sort");
- System.out.println(" 3. Update/Delete");
- System.out.println(" 4. Report");
- System.out.println(" 5. Exit");
- System.out.print(" Your choice [1-5]: ");
- }
- public void createStudent(HashMap<String, TreeSet<Student>> records) {
- DataInput in = new DataInput();
- System.out.println();
- System.out.println("--------Create Student--------");
- int size = getNumberOfRecords(records);
- while (true) {
- if (size > 10) {
- System.out.print("Do you want to continue (Y/N): ");
- boolean isYes = in.isAOrB("Y", "N");
- if (!isYes) {
- break;
- }
- }
- String id = in.inputID();
- TreeSet<Student> students = records.get(id);
- String name;
- if (students != null) {
- Student firstRecord = students.first();
- name = firstRecord.getName();
- System.out.println("Name: " + name);
- } else {
- name = in.inputName();
- }
- System.out.print("Semester: ");
- int semester = in.inputInt("Semester", 1, 9);
- String courseName = in.inputCourseName();
- Student student = new Student(id, name, semester, courseName);
- if (students == null) {
- students = new TreeSet<>();
- students.add(student);
- records.put(id, students);
- } else {
- boolean isSuccess = students.add(student);
- if (!isSuccess) {
- System.out.println("Student records is already exists, enter again.");
- }
- }
- System.out.println();
- }
- }
- public void findAndSort(HashMap<String, TreeSet<Student>> records) {
- DataInput in = new DataInput();
- System.out.println();
- System.out.println("--------Find Student--------");
- String name = in.inputName();
- TreeSet<Student> students = getRecordByName(records, name);
- if (students.isEmpty()) {
- System.out.println("Can not find student with name contains \"" + name + "\"");
- } else {
- String lineFormat = "%-21s%10s%13s\n";
- System.out.format(lineFormat, "Student name", "Semester", "Course name");
- for (Student stu : students) {
- System.out.println(stu.getSearchResult());
- }
- }
- System.out.println();
- }
- public void updateOrDelete(HashMap<String, TreeSet<Student>> records) {
- DataInput in = new DataInput();
- System.out.println();
- System.out.println("--------Update/Delete Student--------");
- if (!records.isEmpty()) {
- String id = in.inputExistedID(records);
- TreeSet<Student> students = records.get(id);
- String lineFormat = "%-9s%-21s%10s%13s\n";
- System.out.format(lineFormat, "Index", "Student name", "Semester", "Course name");
- int i = 0;
- for (Student stu : students) {
- System.out.format("%-9s%s\n", i, stu.getSearchResult());
- i++;
- }
- System.out.print("Choose record you want to Update/Delete "
- + "[0-" + students.size() + "]: ");
- int index = in.inputInt("index", 0, students.size());
- Student stu = getRecordByIndex(students, index);
- System.out.print("Do you want to update (U) or delete (D) student: ");
- boolean isUpdate = in.isAOrB("U", "D");
- if (!isUpdate) {
- students.remove(stu);
- System.out.println("Remove record successfull.");
- } else {
- String newName = in.inputName();
- System.out.print("Semester: ");
- int newSemester = in.inputInt("Semester", 1, 9);
- String newCourseName = in.inputCourseName();
- String oldName = stu.getName();
- int oldSemester = stu.getSemester();
- String oldCourseName = stu.getCourseName();
- if (newSemester == oldSemester && newCourseName.equals(oldCourseName)) {
- if (newName.equals(oldName)) {
- System.out.println("Update fail: Report is unchanged");
- } else {
- updateNewName(students, newName);
- }
- } else {
- Student s = new Student(id, newName, newSemester, newCourseName);
- Student old = new Student(id, oldName, newSemester, newCourseName);
- if (!students.contains(old)) {
- students.remove(stu);
- students.add(s);
- if (!newName.equals(oldName)) {
- updateNewName(students, newName);
- }
- } else {
- System.out.println("Update fail: Report is already exists");
- }
- }
- }
- } else {
- System.out.println("Empty data, please create student record first");
- }
- System.out.println();
- }
- public void showReport(HashMap<String, TreeSet<Student>> records) {
- System.out.println();
- System.out.println("-------- Student Reports --------");
- List<Report> reports = getReports(records);
- Collections.sort(reports);
- if (!reports.isEmpty()) {
- String lineFormat = "%-21s%12s%18s\n";
- System.out.format(lineFormat, "Student name", "Course name", "Total of courses");
- for (Report rp : reports) {
- System.out.println(rp.toString());
- }
- } else {
- System.out.println("Empty data, please create student record first");
- }
- System.out.println();
- }
- private TreeSet<Student> getRecordByName(HashMap<String, TreeSet<Student>> records, String name) {
- TreeSet<Student> result = new TreeSet<>();
- for (Map.Entry<String, TreeSet<Student>> entry : records.entrySet()) {
- TreeSet<Student> students = entry.getValue();
- for (Student record : students) {
- if (record.getName().contains(name)) {
- result.add(record);
- }
- }
- }
- return result;
- }
- private Student getRecordByIndex(TreeSet<Student> students, int index) {
- int i = 0;
- Student stu = null;
- for (Student s : students) {
- if (i == index) {
- stu = s;
- break;
- }
- i++;
- }
- return stu;
- }
- private int getNumberOfRecords(HashMap<String, TreeSet<Student>> records) {
- int size = 0;
- for (Map.Entry<String, TreeSet<Student>> entry : records.entrySet()) {
- size += entry.getValue().size();
- }
- return size;
- }
- private void updateNewName(TreeSet<Student> students, String newName) {
- for (Student stu : students) {
- stu.setName(newName);
- }
- }
- private List<Report> getReports(HashMap<String, TreeSet<Student>> records) {
- List<Report> result = new ArrayList<>();
- for (Map.Entry<String, TreeSet<Student>> entry : records.entrySet()) {
- TreeSet<Student> students = entry.getValue();
- String studentName = students.first().getName();
- int javaCount = 0;
- int dotNetCount = 0;
- int cCount = 0;
- for (Student stu : students) {
- switch (stu.getCourseName()) {
- case "Java": {
- javaCount++;
- break;
- }
- case ".Net": {
- dotNetCount++;
- break;
- }
- case "C/C++": {
- cCount++;
- break;
- }
- }
- }
- if (javaCount > 0) {
- Report report = new Report(studentName, "Java", javaCount);
- result.add(report);
- }
- if (dotNetCount > 0) {
- Report report = new Report(studentName, ".Net", dotNetCount);
- result.add(report);
- }
- if (cCount > 0) {
- Report report = new Report(studentName, "C/C++", cCount);
- result.add(report);
- }
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement