IMG-LOGO
×

Tài Liệu Học

Khoá học lập trình Java Core nâng cao

Exception & Collection

[Video] Tìm hiểu exception + debug + package - lập trình Java Nâng Cao [Video] Tìm hiểu collections, Sets, Maps và Generic - lập trình java nâng cao [Video] Chương trình quản lý sinh viên bằng HashMap - Java Advanced

File

[Video] Hướng dẫn đọc ghi file FileInputStream, FileOutputStream, ObjectInputStream trong java - lập trình java nâng cao [Video] Bài tập quản lý sách & lưu thông tin trên Files [Video] File - Quản lý thông tin sinh viên [Video] Quản lý thông tin sinh & lưu object file - Java nâng cao [Video] Viết ứng dụng WinRar | WinZip | 7Zip bằng Java - Nén file Java & giải nén file Java - Khoá học Java [Video] Bài tập quản lý bán vé máy bay - java nâng cao

Thread

[Video] Phân tích quản lý ứng dụng trong HĐH (Window, Mac, Linux)-Phân tích Game đa luồng Java [Video] Tìm hiểu Thread - Đồng bộ Thread - lập trình java nâng cao - Bổ sung thêm lớp học mới. [Video] Giải thích synchronized + Lap trinh Java nang cao [Video] Thread thực hiện in ngẫu nhiên các số nguyên và ký tự - Phần 1 [Video] Thread thực hiện in ngẫu nhiên các số nguyên và ký tự & Đông bộ thread + wait notify + synchronized + Java nâng cao - Phần 2 [Video] Synchronized 2 Thread - Bài tập mảng tên + địa chỉ - Java nâng cao [Video] Bài tập ôn luyện tổng hợp File, OOP, Thread trong java [Video] Sử dụng MultiThreading sinh ký tự a-zA-Z trong java

CSDL

[Video] Kết nối MySQL trong java swing JDBC trong java - lập trình java nâng cao [Video] Kết nối CSDL bằng java swing phần 1 - lập trình java nâng cao [Video] Kết nối CSDL bằng java swing phần 2 - lập trình java nâng cao [Video] Chương trình quản lý thư viện Java + XML Java + CSDL Java- SQL Java [Video] Phần mềm quản lý sinh viên MySQL + Java - Chương trình quản lý sinh viên MySQL + Java - Lập Trình Java [Video] Bài Tập Quản Lý Sinh Viên - CSDL - Java Swing [Video] Chương trình quản lý sản phẩm - quản lý tin tức - quản lý bán hàng - Lập trình Java [Video] Chức năng register & login + CSDL - Lập trình Java nâng cao - Phần 1 [Video] Hướng dẫn sử dụng Persistence Java - Kết nối database Java - MySQL Java nâng cao

Design Pattern & Đa Ngôn Ngữ

[Video] Design Pattern lập trình java nâng cao - lập trình java nâng cao

XML & JSON & CSDL

[Video] C1 > Chương trình quản lý sinh viên Java + Import/Export JSON + File - Lập Trình Java [Video] C2 > Chương trình quản lý sinh viên Java + Import/Export JSON + File - Lập Trình Java

Ôn Tập Tổng Quát & Exmination

[Video] Bài tập - Chường trình quản lý sở thú - Lập trình Java - Lập trình Java nâng cao - Phần 1 [Video] Bài tập - Chường trình quản lý sở thú - Lập trình Java - Lập trình Java nâng cao - Phần 2




Trang Chủ Java Advanced File - Quản lý thông tin sinh viên

File - Quản lý thông tin sinh viên

by GokiSoft.com - 10:22 25/09/2021 22,986 Lượt Xem

Viết chương trình quản lý sinh viên. Mỗi đối tượng sinh viên có các thuộc tính sau: id, name, age, address và gpa (điểm trung bình). Yêu cầu: tạo ra một menu với các chức năng sau: 

/****************************************/
1. Add student.
2. Edit student by id.
3. Delete student by id.
4. Sort student by gpa.
5. Sort student by name.
6. Show student.

7. Lưu thông tin sv vào file student.txt

8. Đọc thông tin sv từ file student.txt và hiển thị ra màn hình
0. Exit.
/****************************************/

Chú ý mỗi thông tin sinh viên được lưu trên 1 dòng. Các thuộc tính cách nhau bằng dấu ,

Bình luận



Chia sẻ từ lớp học

vuong huu phu [T2008A]

Ngày viết: 18:56 13/03/2021



/*
 * 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 javaapplication29;

import java.io.Serializable;
import java.util.Scanner;

/**
 *
 * @author Admin
 */
public class Sv implements Serializable{
    String id, name, address;
    int age;
    double gpa;

    public Sv() {
    }

    public Sv(String id, String name, String address, int age, double gpa) {
        this.id = id;
        this.name = name;
        this.address = address;
        this.age = age;
        this.gpa = gpa;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getGpa() {
        return gpa;
    }

    public void setGpa(double gpa) {
        this.gpa = gpa;
    }
    public void nhap(){
    Scanner sc = new Scanner(System.in);
        System.out.println("Nhap id");
        id = sc.nextLine();
        System.out.println("Nhap name ");
        name = sc.nextLine();
        System.out.println("Nhap age");
        age = Integer.parseInt(sc.nextLine());
        System.out.println("Nhap address");
        address = sc.nextLine();
        System.out.println("Nhap gpa");
        gpa = Double.parseDouble(sc.nextLine());
        System.out.println("");
        
    }

    @Override
    public String toString() {
        return "id=" + id + ", name=" + name + ", address=" + address + ", age=" + age + ", gpa=" + gpa;
    }
    public void hienthi(){
        System.out.println(toString());
    }
}


Nguyễn Tiến Đạt [T2008A]

Ngày viết: 22:17 12/03/2021


#Main.java


/*
 * 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 Java2.lesson2.QuanLyThongTinSinhVienBangFile;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author MyPC
 */
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        ArrayList<Student> studentList = new ArrayList<>();
        int choose;
        do{
            showMenu();
            System.out.println("Lua chon chuong trinh:");
            choose = Integer.parseInt(scan.nextLine());
            switch(choose){
                case 1:
                    int n;
                    System.out.println("Lua chon so sinh vien muon nhap:");
                    n = Integer.parseInt(scan.nextLine());
                    for (int i = 0; i < n; i++) {
                        Student student = new Student();
                        student.input();
                        studentList.add(student);
                    }
                    break;
                case 2:
                    System.out.println("Id cua sinh vien muon edit:");
                    String idEdit = scan.nextLine();
                    int checkEdit =0;
                    for (Student student : studentList) {
                        if(student.id.equalsIgnoreCase(idEdit)){
                            System.out.println("Da tim thay!!");
                            student.input();
                            checkEdit++;
                            break;
                        }
                    }
                    if(checkEdit == 0) System.out.println("Khong tim thay sinh vien nao!!");
                    break;
                case 3:
                    System.out.println("Id cua sinh vien muon xoa:");
                    String idDelete = scan.nextLine();
                    int checkDel =0;
                    for (Student student : studentList) {
                        if(student.id.equalsIgnoreCase(idDelete)){
                            System.out.println("Da tim thay va xoa thanh cong!!");
                            studentList.remove(student);
                            checkDel++;
                            break;
                        }
                    }
                    if(checkDel == 0) System.out.println("Khong tim thay sinh vien nao!!");
                    break;
                case 4:
                    Collections.sort(studentList, (Student o1, Student o2) -> {
                        if(o1.gpa < o2.gpa) return 1;
                        return -1;
                    });
                    System.out.println("Da sort thanh cong!!");
                    break;
                case 5:
                    Collections.sort(studentList, (Student o1, Student o2) -> o1.name.compareToIgnoreCase(o2.name));
                    System.out.println("Da sort thanh cong!!");
                    break;
                case 6:
                    studentList.forEach((student) -> {
                        System.out.println(student);
                    });
                    break;
                case 7:
                    writeFile(studentList);
                    break;
                case 8:
                    readFile();
                    break;
                case 0:
                    System.out.println("Tam biet!!");
                    break;
                default:
                    System.out.println("Nhap sai!!");
                    break;
            }
        }while(choose != 0);
    }
    static void showMenu(){
        System.out.println("1.Add student");
        System.out.println("2.Edit student by id");
        System.out.println("3.Delete student by id");
        System.out.println("4.Sort student by gpa");
        System.out.println("5.Sort student by name");
        System.out.println("6.Show student");
        System.out.println("7.Save in student.txt");
        System.out.println("8.Read student.txt");
        System.out.println("0.Exit");
    }
    
    static void writeFile(ArrayList<Student> studentList){
        FileOutputStream fos = null;
        File file = new File("student.txt");
        try {
            fos = new FileOutputStream(file);
            for (Student student : studentList) {
                byte[] b = student.getLine().getBytes("utf8");
                fos.write(b);
            }
            
        } catch (FileNotFoundException | UnsupportedEncodingException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            System.out.println("Ghi vao file thanh cong!!");
        }
    }
    
    static void readFile(){
        FileInputStream fis = null;
        InputStreamReader reader = null;
        BufferedReader bReader = null;
        File file = new File("student.txt");
        try {
            fis = new FileInputStream(file);
            reader = new InputStreamReader(fis,"utf8");
            bReader = new BufferedReader(reader);
            String s;
            while((s = bReader.readLine()) != null){
                System.out.println(s);
            }
            
        } catch (FileNotFoundException | UnsupportedEncodingException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            System.out.println("Read thanh cong!!");
        }
    }
}


#Student.java


/*
 * 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 Java2.lesson2.QuanLyThongTinSinhVienBangFile;

import java.util.Scanner;

/**
 *
 * @author MyPC
 */
public class Student {
    String name, address, id;
    int age;
    float gpa;

    public Student() {
    }

    public Student(String name, String address, String id, int age, float gpa) {
        this.name = name;
        this.address = address;
        this.id = id;
        this.age = age;
        this.gpa = gpa;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public float getGpa() {
        return gpa;
    }

    public void setGpa(float gpa) {
        this.gpa = gpa;
    }
    
    public void input(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap ID:");
        id = scan.nextLine();
        System.out.println("Nhap ten:");
        name = scan.nextLine();
        System.out.println("Tuoi:");
        age = Integer.parseInt(scan.nextLine());
        System.out.println("Dia chi:");
        address = scan.nextLine();
        System.out.println("GPA:");
        gpa = Float.parseFloat(scan.nextLine());
    }

    @Override
    public String toString() {
        return "Student{" + "name=" + name + ", address=" + address + ", id=" + id + ", age=" + age + ", gpa=" + gpa + '}';
    }
    public String getLine(){
        return id + "," + name + "," + age + "," + address + "," + gpa + "\n";
    }
}


#student.txt


1,Dat,19,Ha Noi,3.8
2,Lan Huong,20,Hai Duong,4.0


Lê Trí Dũng [C1907L]

Ngày viết: 15:59 29/04/2020



/*
 * 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 StudentMgr;

import java.util.Scanner;

/**
 *
 * @author Dzung
 */
public class Student {
    int id, age, GPA;
    String name, address;

    public Student() {
    }

    public Student(int id, int age, int GPA, String name, String address) {
        this.id = id;
        this.age = age;
        this.GPA = GPA;
        this.name = name;
        this.address = address;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getGPA() {
        return GPA;
    }

    public void setGPA(int GPA) {
        this.GPA = GPA;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Student{" + "id=" + id + ", age=" + age + ", GPA=" + GPA + ", name=" + name + ", address=" + address + '}';
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Input id: ");
        id = Integer.parseInt(scan.nextLine());
        System.out.println("Input name: ");
        name = scan.nextLine();
        System.out.println("Input age: ");
        age = Integer.parseInt(scan.nextLine());
        System.out.println("Input GPA: ");
        GPA = Integer.parseInt(scan.nextLine());
        System.out.println("Input address: ");
        address = scan.nextLine();
    }
    public void display() {
        System.out.println("toString()");
    }
}



/*
 * 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 StudentMgr;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Dzung
 */
public class Menu {
          static List<Student> studentList = new ArrayList();
    static Scanner scan = new Scanner(System.in);
    public static void main(String[] args) throws IOException {
        
        int choose;
        
        do {
            menu();
            choose = Integer.parseInt(scan.nextLine());

            switch (choose) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    editStudent();
                    break;
                case 3:
                    deleteStudent();
                    break;
                case 4:
                    sortStudentGpa();
                    break;
                case 5:
                    sortStudentName();
                    break;
                case 6:
                    showStudent();
                    break;
                case 7:
                    saveStudent();
                    break;
                case 8:
                    readStudent();
                    break;
                default:
                    System.out.println("Exit!!");
                    break;
            }
        } while (choose != 9);
    }
    static void addStudent() {
        System.out.println("Add student:");
        int n = Integer.parseInt(scan.nextLine());
        for (int i = 0; i < n; i++) {
            Student std = new Student();
            std.input();

            studentList.add(std);
        }
    }

    static void editStudent() {
        System.out.println("Input ID need to edit:");
        String id = scan.nextLine();
        int count = 0;
        for (Student editStudent : studentList) {
            if (editStudent.getId().equalsIgnoreCase(id)) {
                editStudent.input();
                count++;
            }
        }
        if (count == 0) {
            System.out.println("Not found ID Student");
        }
    }

    static void deleteStudent() {
        System.out.println("Input ID need to delete: ");
        String id = scan.nextLine();
        for (Student deleteStudent : studentList) {
            if (deleteStudent.getID() == ID) {
                studentList.remove(deleteStudent);
            }
        }
    }

    static void sortStudentGpa() {
        Collections.sort(studentList, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if (o1.getGPA() > o2.getGPA()) {
                    return 1;
                }
                return -1;
            }
        });
        for (int i = 0; i < studentList.size(); i++) {
            studentList.get(i).display();
        }
    }

    static void sortStudentName() {
        Collections.sort(studentList, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        for (int i = 0; i < studentList.size(); i++) {
            studentList.get(i).display();
        }
    }

    static void showStudent() {
        System.out.println("Show all students: ");
        for (Student student : studentList) {
            student.display();
        }
    }

    static void saveStudent() {
        
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream("student.txt");
            oos = new ObjectOutputStream(fos);
            
            oos.writeObject(studentList);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

        } finally {
            if(fos != null){
            try {
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
            }
            if(oos != null){
            try {
                oos.close();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        }
    }

    static void readStudent() {

        FileInputStream fis = null;
        ObjectInputStream ois = null;

        try {
            fis = new FileInputStream("student.txt");
            ois = new ObjectInputStream(fis);
            studentList = (List<Student>) ois.readObject();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

        } finally {
            if(fis != null){
            try {
                fis.close();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
            }
            if(ois != null){
            try {
                ois.close();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        }        
    }
    static void menu(){
        System.out.println("1. Add student");
        System.out.println("2. Edit student by id");
        System.out.println("3. Delete student by id.");
        System.out.println("4. Sort student by gpa.");
        System.out.println("5. Sort student by name.");
        System.out.println("6. Show students.");
        System.out.println("7. Save students info into student.txt");
        System.out.println("8. Read students info from file student.txt");
        System.out.println("Choose:");
    }
}


Nguyễn Hữu Đạt [C1907L]

Ngày viết: 15:00 29/04/2020



/*
 * 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 qlSinhVien;
import java.util.Scanner;

public class Student {

    private String id, name, address;
    private int age;
    private double gpa;

    public Student() {
    }

    public Student(String id, String name, String address, int age, double gpa) {
        this.id = id;
        this.name = name;
        this.address = address;
        this.age = age;
        this.gpa = gpa;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getGpa() {
        return gpa;
    }

    public void setGpa(double gpa) {
        this.gpa = gpa;
    }

    @Override
    public String toString() {
        return "Student{" + "id=" + id + ", name=" + name + ", address=" + address + ", age=" + age + ", gpa=" + gpa + '}';
    }

    public void input() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter id: ");
        this.id = scan.nextLine();
        System.out.println("Enter name: ");
        this.name = scan.nextLine();
        System.out.println("Enter age: ");
        this.age = Integer.parseInt(scan.nextLine());
        System.out.println("Enter address: ");
        this.address = scan.nextLine();
        System.out.println("Enter GPA: ");
        this.gpa = Double.parseDouble(scan.nextLine());
    }
}



/*
 * 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 qlSinhVien;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {

    static List<Student> stdList = new ArrayList<>();

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int choose;
        do {
            showMenu();
            choose = Integer.parseInt(scan.nextLine());
            switch (choose) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    editStudent();
                    break;
                case 3:
                    deleteStudent();
                    break;
                case 4:
                    sortByGPA();
                    break;
                case 5:
                    sortByName();
                    break;
                case 6:
                    showStudent();
                    break;
                case 7:
                    saveToFile();
                    break;
                case 8:
                    readFromFile();
                    break;
                case 0:
                    System.out.println("Exit");
                    break;
            }
        } while (choose != 0);
    }

    static void showMenu() {
        System.out.println("1. Add Student");
        System.out.println("2. Edit student by id.");
        System.out.println("3. Delete student by id.");
        System.out.println("4. Sort student by gpa.");
        System.out.println("5. Sort student by name.");
        System.out.println("6. Show student.");
        System.out.println("7. Lưu thông tin sv vào file student.txt");
        System.out.println("8. Đọc thông tin sv từ file student.txt và hiển thị ra màn hình");
        System.out.println("0. Exit.");
        System.out.println("Lựa chọn của bạn: ");
    }

    public static void addStudent() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter number of students: ");
        int n = Integer.parseInt(scan.nextLine());
        for (int i = 0; i < n; i++) {
            Student std = new Student();
            std.input();
            stdList.add(std);
        }
    }

    public static void editStudent() {
        Scanner scan = new Scanner(System.in);
        String id;
        int count = 0;
        System.out.println("Enter student's id you want to edit: ");
        id = scan.nextLine();
        for (int i = 0; i < stdList.size(); i++) {
            if (stdList.get(i).getId().equalsIgnoreCase(id)) {
                stdList.get(i).input();
                count++;
            }
        }
        if (count == 0) {
            System.out.println("Cannot find student with id " + id);
        }
    }

    public static void deleteStudent() {
        Scanner scan = new Scanner(System.in);
        String id;
        int count = 0;
        System.out.println("Enter student's id you want to delete: ");
        id = scan.nextLine();
        for (int i = 0; i < stdList.size(); i++) {
            if (stdList.get(i).getId().equalsIgnoreCase(id)) {
                stdList.remove(stdList.get(i));
                count++;
            }
        }
        if (count == 0) {
            System.out.println("Cannot find student with id " + id);
        }
    }

    public static void sortByGPA() {
        for (int i = 0; i < stdList.size() - 1; i++) {
            for (int j = i + 1; j < stdList.size(); j++) {
                if (stdList.get(i).getGpa() > stdList.get(j).getGpa()) {
                    Collections.swap(stdList, i, j);
                }
            }
        }
        stdList.forEach((student) -> {
            System.out.println(student.toString());
        });
    }

    public static void sortByName() {
        for (int i = 0; i < stdList.size() - 1; i++) {
            for (int j = i + 1; j < stdList.size(); j++) {
                if (stdList.get(i).getName().compareTo(stdList.get(j).getName()) > 0) {
                    Collections.swap(stdList, i, j);
                }
            }
        }
        stdList.forEach((student) -> {
            System.out.println(student.toString());
        });
    }

    public static void showStudent() {
        stdList.forEach((student) -> {
            System.out.println(student.toString());
        });
    }

    public static void saveToFile() {
        FileOutputStream fos = null;
        File file_output = new File("D://student.txt");
        try {
            fos = new FileOutputStream(file_output,true);

            String line;
            byte[] b;
            for (Student std : stdList) {
                line = std.toString() + "\n";
                b = line.getBytes();
                fos.write(b);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    static void readFromFile() {
        FileInputStream fis = null;
        File file_input = new File("D://student.txt");
        try {
            fis = new FileInputStream(file_input);

            StringBuilder builder = new StringBuilder();
            int code;
            while ((code = fis.read()) != -1) {
                builder.append((char) code);
            }
            String content = builder.toString();
            System.out.println(content);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}


Nguyễn Hoàng Anh [C1907L]

Ngày viết: 13:29 28/04/2020



/*
 * 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 April27;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Redmibook 14
 */
public class main {

    public static void menu() {
        System.out.println("1. Add student.");
        System.out.println("2. Edit student by id.");
        System.out.println("3. Delete student by id.");
        System.out.println("4. Sort student by gpa.");
        System.out.println("5. Sort student by name.");
        System.out.println("6. Show student.");
        System.out.println("7. Lưu thông tin sv vào file student.txt");
        System.out.println("8. Đọc thông tin sv từ file student.txt và hiển thị ra màn hình");
        System.out.println("0. Exit.");
    }

    public static void addStudent(List<Student> ListStudent) {
        while (true) {
            System.out.println("Enter student " + (ListStudent.size() + 1) + " info");
            Student student = new Student();
            student.input();
            ListStudent.add(student);
            System.out.println("Continue Y/N ?");
            Scanner input = new Scanner(System.in);
            String y = input.nextLine();
            if (y.equalsIgnoreCase("N")) {
                break;
            }
        }

    }

    public static void editStudent(List<Student> ListStudent) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter student id :");
        String roll = input.nextLine();
        int i;
        for (i = 0; i < ListStudent.size(); i++) {
            if (ListStudent.get(i).rollNo.equals(roll)) {
                System.out.println("Change student info.");
                Student std = new Student();
                std.input();
                ListStudent.set(i, std);
                break;
            }
        }
        if (i == ListStudent.size()) {
            System.out.println("Student id is not exists!");
        }
    }

    public static void removeStudent(List<Student> ListStudent) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter student id :");
        String roll = input.nextLine();
        int i;
        for (i = 0; i < ListStudent.size(); i++) {
            if (ListStudent.get(i).rollNo.equals(roll)) {
                ListStudent.remove(i);
                break;
            }
        }
        if (i == ListStudent.size()) {
            System.out.println("Student id is not exists!");
        }
    }

    public static void sortStudentById(List<Student> ListStudent) {
        for (int i = 0; i < ListStudent.size(); i++) {
            for (int j = i + 1; j < ListStudent.size(); j++) {
                if (ListStudent.get(i).rollNo.compareTo(ListStudent.get(j).rollNo) > 0) {
                    Student temp;
                    temp = ListStudent.get(i);
                    ListStudent.set(i, ListStudent.get(j));
                    ListStudent.set(j, temp);
                }
            }
        }
        for (int i = 0; i < ListStudent.size(); i++) {
            ListStudent.get(i).display();
        }
    }

    public static void sortStudentByName(List<Student> ListStudent) {
        for (int i = 0; i < ListStudent.size(); i++) {
            for (int j = i + 1; j < ListStudent.size(); j++) {
                if (ListStudent.get(i).Name.compareTo(ListStudent.get(j).Name) > 0) {
                    Student temp;
                    temp = ListStudent.get(i);
                    ListStudent.set(i, ListStudent.get(j));
                    ListStudent.set(j, temp);
                }
            }
        }
        for (int i = 0; i < ListStudent.size(); i++) {
            ListStudent.get(i).display();
        }
    }

    public static void ShowStudent(List<Student> ListStudent) {
        for (int i = 0; i < ListStudent.size(); i++) {
            ListStudent.get(i).display();
        }
    }

    public static void outputFile(List<Student> ListStudent) {
        File file = new File("Student.txt");
        FileOutputStream fos = null;

        try {
            file.createNewFile();
        } catch (IOException ex) {
            Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            fos = new FileOutputStream("Student.txt", true);
            for (Student std : ListStudent) {
                String line = std.toString() + "\n";
                byte[] b = line.getBytes();

                fos.write(b);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                    System.out.println("File created!");
                } catch (IOException ex) {
                    Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public static void readFile() {
        FileInputStream fis = null;
        BufferedReader buferedReader = null;
        try {
            fis = new FileInputStream("Student.txt");
            StringBuilder builder = new StringBuilder();
            int code;
            while ((code = fis.read()) != -1) {
                builder.append((char) code);
            }
            String content = builder.toString();
            System.out.println(content);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fis.close();
            } catch (IOException ex) {
                Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        List<Student> ListStudent = new ArrayList();
        Scanner input = new Scanner(System.in);
        while (true) {
            menu();
            String choiceStr = input.nextLine();
            int choice = 10;
            try {
                choice = Integer.parseInt(choiceStr);
            } catch (NumberFormatException ex) {
                System.out.println("Please input number!");
            }
            switch (choice) {
                case 1:
                    addStudent(ListStudent);
                    break;
                case 2:
                    editStudent(ListStudent);
                    break;
                case 3:
                    removeStudent(ListStudent);
                    break;
                case 4:
                    sortStudentById(ListStudent);
                    break;
                case 5:
                    sortStudentByName(ListStudent);
                    break;
                case 6:
                    ShowStudent(ListStudent);
                    break;
                case 7:
                    outputFile(ListStudent);
                    break;
                case 8:
                    readFile();
                    break;
                case 9:
                    readFile();
                    break;
                case 0:
                    return;
            }
        }
        // TODO code application logic here
    }

}


Tài Liệu Tham Khảo

Ứng Dụng Tiện Ích



Đã sao chép!!!