rockman1510

Exercises 3

Feb 7th, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.82 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package javacore_season8_exercise3;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.Collections;
  10. import java.util.Comparator;
  11. import java.util.Scanner;
  12.  
  13. /**
  14.  *
  15.  * @author HuyLV
  16.  */
  17. public class Exercise3 {
  18.  
  19.     public static int option;
  20.     public static Scanner scanner;
  21.     public static ArrayList<Student> students;
  22.  
  23.     /**
  24.      * @param args the command line arguments
  25.      */
  26.     public static void main(String[] args) {
  27.         // TODO code application logic here
  28.         scanner = new Scanner(System.in);
  29.         students = new ArrayList<Student>();
  30.         menu();
  31.     }
  32.  
  33.     private static void menu() {
  34.         do {
  35.             displayMenu();
  36.             option = scanner.nextInt();
  37.             scanner.nextLine();
  38.             switch (option) {
  39.                 case 1:
  40.                     input();
  41.                     break;
  42.                 case 2:
  43.                     list();
  44.                     break;
  45.                 case 3:
  46.                     sortByYear();
  47.                     break;
  48.                 case 4:
  49.                     sortByGender();
  50.                     break;
  51.                 case 5:
  52.                     findStudentByName();
  53.                     break;
  54.                 case 6:
  55.                     findStudentById();
  56.                     break;
  57.                 case 7:
  58.                     calculateMark();
  59.                     break;
  60.                 case 8:
  61.                     sortByRank();
  62.                     break;
  63.                 default:
  64.                     break;
  65.             }
  66.         } while (option != 9);
  67.         System.exit(0);
  68.     }
  69.  
  70.     private static void displayMenu() {
  71.         System.out.println("----------------------------Menu----------------------------");
  72.         System.out.println("1. Nhap sinh vien.");
  73.         System.out.println("2. Liet ke danh sach sinh vien.");
  74.         System.out.println("3. Phan loai sinh vien theo nien khoa.");
  75.         System.out.println("4. Phan loai sinh vien theo gioi tinh.");
  76.         System.out.println("5. Tim kiem sinh vien theo ten.");
  77.         System.out.println("6. Tim kiem sinh vien theo ma sinh vien.");
  78.         System.out.println("7. Tinh diem trung binh va sap xep");
  79.         System.out.println("8. Xep loai sinh vien theo thu hang diem trung binh.");
  80.         System.out.println("9. Thoat.");
  81.     }
  82.  
  83.     private static void input() {
  84.         Student student = new Student();
  85.         System.out.println("Hay nhap ma sinh vien: ");
  86.         student.id = scanner.nextInt();
  87.         scanner.nextLine();
  88.         System.out.println("Hay nhap ten sinh vien: ");
  89.         student.name = scanner.nextLine();
  90.         System.out.println("Hay nhap ngay sinh: ");
  91.         student.dob = scanner.nextLine();
  92.         System.out.println("Hay nhap gioi tinh (nam/nu): ");
  93.         String gen = scanner.nextLine();
  94.         if (gen.equals("nam")) {
  95.             student.gender = true;
  96.         } else {
  97.             student.gender = false;
  98.         }
  99.         System.out.println("Hay nhap lop: ");
  100.         student.grade = scanner.nextLine();
  101.         System.out.println("Hay nhap nien khoa: ");
  102.         student.year = scanner.nextInt();
  103.         scanner.nextLine();
  104.         System.out.println("Hay nhap diem toan: ");
  105.         student.markMath = scanner.nextFloat();
  106.         System.out.println("Hay nhap diem tieng anh: ");
  107.         student.markEn = scanner.nextFloat();
  108.         System.out.println("Hay nhap diem van: ");
  109.         student.markLite = scanner.nextFloat();
  110.         students.add(student);
  111.     }
  112.  
  113.     private static void list() {
  114.         System.out.println("Danh sach cac sinh vien da nhap:");
  115.         for (int i = 0; i < students.size(); i++) {
  116.             displayStudent(students.get(i));
  117.         }
  118.         System.out.println();
  119.     }
  120.  
  121.     private static void sortByYear() {
  122.         Collections.sort(students, new Comparator<Student>() {
  123.             @Override
  124.             public int compare(Student o1, Student o2) {
  125.                 if (o1.year < o2.year) {
  126.                     return -1;
  127.                 } else {
  128.                     return 1;
  129.                 }
  130.             }
  131.         });
  132.         System.out.println("Danh sach cac sinh vien phan loai theo nien khoa:");
  133.         for (int i = 0; i < students.size(); i++) {
  134.             String gender;
  135.             if (students.get(i).gender) {
  136.                 gender = "Nam";
  137.             } else {
  138.                 gender = "Nu";
  139.             }
  140.             System.out.println("Nien khoa: " + students.get(i).year + "\t\t"
  141.                     + "Ma sinh vien: " + students.get(i).id + "\t\t"
  142.                     + "Ho ten: " + students.get(i).name + "\t\t"
  143.                     + "Ngay sinh: " + students.get(i).dob + "\t\t"
  144.                     + "Gioi tinh: " + gender + "\t\t"
  145.                     + "Lop: " + students.get(i).grade + "\t\t"
  146.                     + "Diem toan: " + students.get(i).markMath + "\t\t"
  147.                     + "Diem tieng anh: " + students.get(i).markEn + "\t\t"
  148.                     + "Diem van: " + students.get(i).markLite + "\t\t"
  149.             );
  150.         }
  151.         System.out.println();
  152.     }
  153.  
  154.     private static void sortByGender() {
  155.         System.out.println("Danh sach cac sinh vien phan loai theo gioi tinh:");
  156.         ArrayList<Student> maleStudents = new ArrayList<Student>();
  157.         ArrayList<Student> femaleStudents = new ArrayList<Student>();
  158.         for (int i = 0; i < students.size(); i++) {
  159.             if (students.get(i).gender) {
  160.                 maleStudents.add(students.get(i));
  161.             } else {
  162.                 femaleStudents.add(students.get(i));
  163.             }
  164.         }
  165.         System.out.println("Nu:");
  166.         for (Student femaleStudent : femaleStudents) {
  167.             displayStudent(femaleStudent);
  168.         }
  169.         System.out.println();
  170.         System.out.println("Nam:");
  171.         for (Student maleStudent : maleStudents) {
  172.             displayStudent(maleStudent);
  173.         }
  174.         System.out.println();
  175.     }
  176.  
  177.     private static void findStudentByName() {
  178.         System.out.print("Hay nhap ten sinh vien ban muon tim: ");
  179.         String name = scanner.nextLine();
  180.         boolean isFound = false;
  181.         int index = 0;
  182.         for (int i = 0; i < students.size(); i++) {
  183.             if (students.get(i).name.equals(name)) {
  184.                 isFound = true;
  185.                 index = i;
  186.             }
  187.         }
  188.         if (isFound) {
  189.             displayStudent(students.get(index));
  190.         } else {
  191.             System.out.println("Sinh vien ban tim khong ton tai!");
  192.         }
  193.     }
  194.  
  195.     private static void findStudentById() {
  196.         System.out.print("Hay nhap ma sinh vien ban muon tim: ");
  197.         int id = scanner.nextInt();
  198.         boolean isFound = false;
  199.         int index = 0;
  200.         for (int i = 0; i < students.size(); i++) {
  201.             if (students.get(i).id == id) {
  202.                 isFound = true;
  203.                 index = i;
  204.             }
  205.         }
  206.         if (isFound) {
  207.             displayStudent(students.get(index));
  208.         } else {
  209.             System.out.println("Sinh vien ban tim khong ton tai!");
  210.         }
  211.     }
  212.  
  213.     private static void calculateMark() {
  214.         System.out.println("Danh sach cac sinh vien phan loai theo diem trung binh:");
  215.         for (int i = 0; i < students.size(); i++) {
  216.             Student student = students.get(i);
  217.             student.markTotal = (student.markMath + student.markEn + student.markLite) / 3;
  218.         }
  219.         Collections.sort(students, new Comparator<Student>() {
  220.             @Override
  221.             public int compare(Student o1, Student o2) {
  222.                 if (o1.markTotal > o2.markTotal) {
  223.                     return -1;
  224.                 } else {
  225.                     return 1;
  226.                 }
  227.             }
  228.         });
  229.         for (int i = 0; i < students.size(); i++) {
  230.             displayStudentByMark(students.get(i), false);
  231.         }
  232.     }
  233.  
  234.     private static void sortByRank() {
  235.         System.out.println("Danh sach cac sinh vien phan loai theo thu hang:");
  236.         for (int i = 0; i < students.size(); i++) {
  237.             Student student = students.get(i);
  238.             if (student.markTotal < 5) {
  239.                 student.rank = "D";
  240.             } else if (student.markTotal >= 5 && student.markTotal < 7) {
  241.                 student.rank = "C";
  242.             } else if (student.markTotal >= 7 && student.markTotal < 9) {
  243.                 student.rank = "B";
  244.             } else if (student.markTotal >= 9) {
  245.                 student.rank = "A";
  246.             }
  247.         }
  248.         Collections.sort(students, new Comparator<Student>() {
  249.             @Override
  250.             public int compare(Student o1, Student o2) {
  251.                 if (o1.markTotal > o2.markTotal) {
  252.                     return -1;
  253.                 } else {
  254.                     return 1;
  255.                 }
  256.             }
  257.         });
  258.         for (int i = 0; i < students.size(); i++) {
  259.             displayStudentByMark(students.get(i), true);
  260.         }
  261.     }
  262.  
  263.     private static void displayStudent(Student student) {
  264.         String gender;
  265.         if (student.gender) {
  266.             gender = "nam";
  267.         } else {
  268.             gender = "nu";
  269.         }
  270.         System.out.println("Ma sinh vien: " + student.id + "\t\t"
  271.                 + "Ho ten: " + student.name + "\t\t"
  272.                 + "Ngay sinh: " + student.dob + "\t\t"
  273.                 + "Gioi tinh: " + gender + "\t\t"
  274.                 + "Lop: " + student.grade + "\t\t"
  275.                 + "Nien khoa: " + student.year + "\t\t"
  276.                 + "Diem toan: " + student.markMath + "\t\t"
  277.                 + "Diem tieng anh: " + student.markEn + "\t\t"
  278.                 + "Diem van: " + student.markLite + "\t\t");
  279.     }
  280.  
  281.     private static void displayStudentByMark(Student student, boolean isRank) {
  282.         if (isRank) {
  283.             String rank = "";
  284.             switch (student.rank) {
  285.                 case "A":
  286.                     rank = "Gioi";
  287.                     break;
  288.                 case "B":
  289.                     rank = "Kha";
  290.                     break;
  291.                 case "C":
  292.                     rank = "Trung Binh";
  293.                     break;
  294.                 case "D":
  295.                     rank = "Duoi Trung Binh";
  296.                     break;
  297.             }
  298.             System.out.println("Ma sinh vien: " + student.id + "\t\t"
  299.                     + "Ho ten: " + student.name + "\t\t"
  300.                     + "Diem tong ket: " + student.markTotal
  301.                     + "Xep loai: " + rank);
  302.         } else {
  303.             System.out.println("Ma sinh vien: " + student.id + "\t\t"
  304.                     + "Ho ten: " + student.name + "\t\t"
  305.                     + "Diem tong ket: " + student.markTotal);
  306.         }
  307.     }
  308. }
Add Comment
Please, Sign In to add comment