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.
- */
- package javacore_season8_exercise3;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.Scanner;
- /**
- *
- * @author HuyLV
- */
- public class Exercise3 {
- public static int option;
- public static Scanner scanner;
- public static ArrayList<Student> students;
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- scanner = new Scanner(System.in);
- students = new ArrayList<Student>();
- menu();
- }
- private static void menu() {
- do {
- displayMenu();
- option = scanner.nextInt();
- scanner.nextLine();
- switch (option) {
- case 1:
- input();
- break;
- case 2:
- list();
- break;
- case 3:
- sortByYear();
- break;
- case 4:
- sortByGender();
- break;
- case 5:
- findStudentByName();
- break;
- case 6:
- findStudentById();
- break;
- case 7:
- calculateMark();
- break;
- case 8:
- sortByRank();
- break;
- default:
- break;
- }
- } while (option != 9);
- System.exit(0);
- }
- private static void displayMenu() {
- System.out.println("----------------------------Menu----------------------------");
- System.out.println("1. Nhap sinh vien.");
- System.out.println("2. Liet ke danh sach sinh vien.");
- System.out.println("3. Phan loai sinh vien theo nien khoa.");
- System.out.println("4. Phan loai sinh vien theo gioi tinh.");
- System.out.println("5. Tim kiem sinh vien theo ten.");
- System.out.println("6. Tim kiem sinh vien theo ma sinh vien.");
- System.out.println("7. Tinh diem trung binh va sap xep");
- System.out.println("8. Xep loai sinh vien theo thu hang diem trung binh.");
- System.out.println("9. Thoat.");
- }
- private static void input() {
- Student student = new Student();
- System.out.println("Hay nhap ma sinh vien: ");
- student.id = scanner.nextInt();
- scanner.nextLine();
- System.out.println("Hay nhap ten sinh vien: ");
- student.name = scanner.nextLine();
- System.out.println("Hay nhap ngay sinh: ");
- student.dob = scanner.nextLine();
- System.out.println("Hay nhap gioi tinh (nam/nu): ");
- String gen = scanner.nextLine();
- if (gen.equals("nam")) {
- student.gender = true;
- } else {
- student.gender = false;
- }
- System.out.println("Hay nhap lop: ");
- student.grade = scanner.nextLine();
- System.out.println("Hay nhap nien khoa: ");
- student.year = scanner.nextInt();
- scanner.nextLine();
- System.out.println("Hay nhap diem toan: ");
- student.markMath = scanner.nextFloat();
- System.out.println("Hay nhap diem tieng anh: ");
- student.markEn = scanner.nextFloat();
- System.out.println("Hay nhap diem van: ");
- student.markLite = scanner.nextFloat();
- students.add(student);
- }
- private static void list() {
- System.out.println("Danh sach cac sinh vien da nhap:");
- for (int i = 0; i < students.size(); i++) {
- displayStudent(students.get(i));
- }
- System.out.println();
- }
- private static void sortByYear() {
- Collections.sort(students, new Comparator<Student>() {
- @Override
- public int compare(Student o1, Student o2) {
- if (o1.year < o2.year) {
- return -1;
- } else {
- return 1;
- }
- }
- });
- System.out.println("Danh sach cac sinh vien phan loai theo nien khoa:");
- for (int i = 0; i < students.size(); i++) {
- String gender;
- if (students.get(i).gender) {
- gender = "Nam";
- } else {
- gender = "Nu";
- }
- System.out.println("Nien khoa: " + students.get(i).year + "\t\t"
- + "Ma sinh vien: " + students.get(i).id + "\t\t"
- + "Ho ten: " + students.get(i).name + "\t\t"
- + "Ngay sinh: " + students.get(i).dob + "\t\t"
- + "Gioi tinh: " + gender + "\t\t"
- + "Lop: " + students.get(i).grade + "\t\t"
- + "Diem toan: " + students.get(i).markMath + "\t\t"
- + "Diem tieng anh: " + students.get(i).markEn + "\t\t"
- + "Diem van: " + students.get(i).markLite + "\t\t"
- );
- }
- System.out.println();
- }
- private static void sortByGender() {
- System.out.println("Danh sach cac sinh vien phan loai theo gioi tinh:");
- ArrayList<Student> maleStudents = new ArrayList<Student>();
- ArrayList<Student> femaleStudents = new ArrayList<Student>();
- for (int i = 0; i < students.size(); i++) {
- if (students.get(i).gender) {
- maleStudents.add(students.get(i));
- } else {
- femaleStudents.add(students.get(i));
- }
- }
- System.out.println("Nu:");
- for (Student femaleStudent : femaleStudents) {
- displayStudent(femaleStudent);
- }
- System.out.println();
- System.out.println("Nam:");
- for (Student maleStudent : maleStudents) {
- displayStudent(maleStudent);
- }
- System.out.println();
- }
- private static void findStudentByName() {
- System.out.print("Hay nhap ten sinh vien ban muon tim: ");
- String name = scanner.nextLine();
- boolean isFound = false;
- int index = 0;
- for (int i = 0; i < students.size(); i++) {
- if (students.get(i).name.equals(name)) {
- isFound = true;
- index = i;
- }
- }
- if (isFound) {
- displayStudent(students.get(index));
- } else {
- System.out.println("Sinh vien ban tim khong ton tai!");
- }
- }
- private static void findStudentById() {
- System.out.print("Hay nhap ma sinh vien ban muon tim: ");
- int id = scanner.nextInt();
- boolean isFound = false;
- int index = 0;
- for (int i = 0; i < students.size(); i++) {
- if (students.get(i).id == id) {
- isFound = true;
- index = i;
- }
- }
- if (isFound) {
- displayStudent(students.get(index));
- } else {
- System.out.println("Sinh vien ban tim khong ton tai!");
- }
- }
- private static void calculateMark() {
- System.out.println("Danh sach cac sinh vien phan loai theo diem trung binh:");
- for (int i = 0; i < students.size(); i++) {
- Student student = students.get(i);
- student.markTotal = (student.markMath + student.markEn + student.markLite) / 3;
- }
- Collections.sort(students, new Comparator<Student>() {
- @Override
- public int compare(Student o1, Student o2) {
- if (o1.markTotal > o2.markTotal) {
- return -1;
- } else {
- return 1;
- }
- }
- });
- for (int i = 0; i < students.size(); i++) {
- displayStudentByMark(students.get(i), false);
- }
- }
- private static void sortByRank() {
- System.out.println("Danh sach cac sinh vien phan loai theo thu hang:");
- for (int i = 0; i < students.size(); i++) {
- Student student = students.get(i);
- if (student.markTotal < 5) {
- student.rank = "D";
- } else if (student.markTotal >= 5 && student.markTotal < 7) {
- student.rank = "C";
- } else if (student.markTotal >= 7 && student.markTotal < 9) {
- student.rank = "B";
- } else if (student.markTotal >= 9) {
- student.rank = "A";
- }
- }
- Collections.sort(students, new Comparator<Student>() {
- @Override
- public int compare(Student o1, Student o2) {
- if (o1.markTotal > o2.markTotal) {
- return -1;
- } else {
- return 1;
- }
- }
- });
- for (int i = 0; i < students.size(); i++) {
- displayStudentByMark(students.get(i), true);
- }
- }
- private static void displayStudent(Student student) {
- String gender;
- if (student.gender) {
- gender = "nam";
- } else {
- gender = "nu";
- }
- System.out.println("Ma sinh vien: " + student.id + "\t\t"
- + "Ho ten: " + student.name + "\t\t"
- + "Ngay sinh: " + student.dob + "\t\t"
- + "Gioi tinh: " + gender + "\t\t"
- + "Lop: " + student.grade + "\t\t"
- + "Nien khoa: " + student.year + "\t\t"
- + "Diem toan: " + student.markMath + "\t\t"
- + "Diem tieng anh: " + student.markEn + "\t\t"
- + "Diem van: " + student.markLite + "\t\t");
- }
- private static void displayStudentByMark(Student student, boolean isRank) {
- if (isRank) {
- String rank = "";
- switch (student.rank) {
- case "A":
- rank = "Gioi";
- break;
- case "B":
- rank = "Kha";
- break;
- case "C":
- rank = "Trung Binh";
- break;
- case "D":
- rank = "Duoi Trung Binh";
- break;
- }
- System.out.println("Ma sinh vien: " + student.id + "\t\t"
- + "Ho ten: " + student.name + "\t\t"
- + "Diem tong ket: " + student.markTotal
- + "Xep loai: " + rank);
- } else {
- System.out.println("Ma sinh vien: " + student.id + "\t\t"
- + "Ho ten: " + student.name + "\t\t"
- + "Diem tong ket: " + student.markTotal);
- }
- }
- }
Add Comment
Please, Sign In to add comment