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 23,219 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

Nguyễn Hoàng Anh [C1907L]

Ngày viết: 13:24 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;

/**
 *
 * @author Redmibook 14
 */
/*
 * 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.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 test {

    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("9. Remove files.");
        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("Student2.txt");
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            file.createNewFile();
        } catch (IOException ex) {
            Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            fos = new FileOutputStream("Student2.txt", true);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(ListStudent);
        } 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;
        ObjectInputStream ois;
        try {
            fis = new FileInputStream("Student2.txt");
            ois = new ObjectInputStream(fis);
            List<Student> listStudent = (List<Student>) ois.readObject();
            for (Student student : listStudent) {
                System.out.println(student);
            }
        } 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(test.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:
                    File file = new File("Student2.txt");
                    file.delete();
                    System.out.println("File removed!");
                    break;
                case 0:
                    return;
            }
        }
        // TODO code application logic here
    }

}



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

/**
 *
 * @author Redmibook 14
 */
public class   Student implements Serializable{

    String rollNo, Name, Sex, Address;

    public Student(String rollNo, String Name, String Sex, String Address) {
        this.rollNo = rollNo;
        this.Name = Name;
        this.Sex = Sex;
        this.Address = Address;
    }

    public Student() {
    }

    public String getRollNo() {
        return rollNo;
    }

    public String getName() {
        return Name;
    }

    public String getSex() {
        return Sex;
    }

    public String getAddress() {
        return Address;
    }

    public void setRollNo(String rollNo) {
        this.rollNo = rollNo;
    }

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

    public void setSex(String Sex) {
        this.Sex = Sex;
    }

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

    public void input() {
        Scanner input = new Scanner(System.in);
        System.out.println("rollNo :");
        rollNo = input.nextLine();
        System.out.println("Name :");
        Name = input.nextLine();
        System.out.println("Sex :");
        Sex = input.nextLine();
        System.out.println("Address :");
        Address = input.nextLine();
    }
    public void display(){
        System.out.println(toString());
    }
    public static String getHeaderFormat(){
        return "RollNo,Name,Sex,Address";
    }
    @Override
    public String toString() {
        return "Student : " + "rollNo=" + rollNo + ", Name=" + Name + ", Sex=" + Sex + ", Address=" + Address ;
    }
    
}


Vũ Việt Đức [C1907L]

Ngày viết: 22:59 27/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 lession3;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
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 ADMIN
 */
public class Main {
    public static void main(String[] agrs) {
        List<Student> listStudent = new ArrayList<>();
        Scanner scan = new Scanner(System.in);
        
        while(true) {
            menu();
            int choose = Integer.parseInt(scan.nextLine());
            switch(choose) {
                case 1:
                    System.out.println("Please enter information: ");
                    Student std = new Student();
                    std.input();
                    listStudent.add(std);
                    break;
                case 2:
                    System.out.print("Enter id: ");
                    String id = scan.nextLine();
                    int countStd = 0;
                    
                    for(int i = 0; i < listStudent.size(); i++) {
                        System.out.println(i);
                        if(listStudent.get(i).getId().equalsIgnoreCase(id)){
                            Student std1 = new Student();
                            std1.input();
                            listStudent.set(i, std1);
                            countStd++;
                            break;
                        }
                    }
                    
                    if(countStd == 0){
                        System.out.println("Student not found");
                    }
                    
                    break;
                case 3:
                     System.out.print("Enter id: ");
                    String idD = scan.nextLine();
                    int countStd1 = 0;
                    
                    for(int i = 0; i < listStudent.size(); i++) {
                        System.out.println(i);
                        if(listStudent.get(i).getId().equalsIgnoreCase(idD)){
                            listStudent.remove(listStudent.get(i));
                            countStd1++;
                            break;
                        }
                    }
                    
                    if(countStd1 == 0){
                        System.out.println("Student not found");
                    }
                    break;
                case 4:
                    Collections.sort(listStudent, 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 < listStudent.size(); i++){
                        listStudent.get(i).show();
                    }
                    break;
                case 5:
                     Collections.sort(listStudent, new Comparator<Student>() {
                        @Override
                        public int compare(Student o1, Student o2) {
                            return o1.getName().compareTo(o2.getName());
                        }
                    });
                    for (int i = 0; i < listStudent.size(); i++) {
                        listStudent.get(i).show();
                    }
                    break;
                case 6:
                    for (int i = 0; i < listStudent.size(); i++) {
                        listStudent.get(i).show();
                    }
                    break;
                case 7:
                    FileOutputStream fos = null;
                    
                    try {
                        fos = new FileOutputStream("Student.txt");
                        String line = Student.getFileHeaderFormat() + "\n";
                         byte[] b = line.getBytes();
                         
                        fos.write(b);
            
                        for (int i = 0; i < listStudent.size(); i++) {
                            line = listStudent.get(i).getFileLineFormat() + "\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);
                            }
                        }
                    }
           
                    break;
                case 8:
                    FileReader reader = null;
                    BufferedReader bufferedReader = null;
                    
            {
                try {
                    reader = new FileReader("Student.txt");
                    bufferedReader = new BufferedReader(reader);
                    
                    String line;
            
                    while((line = bufferedReader.readLine()) != null) {
                        System.out.println(line);
                    }
                } 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 {
                        reader.close();
                    } catch (IOException ex) {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    if(bufferedReader != null) {
                        try {
                            bufferedReader.close();
                        } catch (IOException ex) {
                            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            }
                    break;
                case 0:
                    break;
                default:
                    System.out.println("Enter incorrect. Please enter again");
            }
            
            if(choose == 0){
                break;
            }
        }
    }
    
    public static void menu() {
        System.out.println("1. Add student.\n2. Edit student by id.\n3. Delete student by id.\n4. Sort student by gpa.\n5. Sort student by name.\n6. Show student.\n7. Lưu thông tin sv vào file student.txt\n8. Đọc thông tin sv từ file student.txt và hiển thị ra màn hình\n0. Exit.");
    }
}



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

import java.util.Scanner;

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

    public Student() {
    }

    public Student(String id, String name, String address, int age, float 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 float getGpa() {
        return gpa;
    }

    public void setGpa(float gpa) {
        this.gpa = gpa;
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.print("Enter id: ");
        id = scan.nextLine();
        System.out.print("Enter name: ");
        name = scan.nextLine();
        System.out.print("Enter age: ");
        age = Integer.parseInt(scan.nextLine());
        System.out.print("Enter address: ");
        address = scan.nextLine();
        System.out.print("Enter GPA: ");
        gpa = Float.parseFloat(scan.nextLine());
    }
    
    public void show() {
        System.out.println(this);
    }

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


Hoàng Quang Huy [C1907L]

Ngày viết: 22:46 27/04/2020




package lesson2;

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());
    }
}
-------------------------------------------------------------------------------------

package lesson2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
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);
                }
            }
        }
    }
}


Ngô Quang Huy [C1907L]

Ngày viết: 22:00 27/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 April27File;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
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;

/**
 *
 * @author Administrator
 */
public class Main {
    static List<Student> listStd = new ArrayList<>();
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        while(true){
            showMenu();
            System.out.println("Choose: ");
            int choose = Integer.parseInt(input.nextLine());
            switch(choose){
                case 1:
                    Student std = new Student();
                    std.input();
                    listStd.add(std);
                    break;
                case 2:
                    Edit();
                    break;
                case 3:
                    Delete();
                    break;
                case 4:
                    SortGPA();
                    break;
                case 5:
                    SortName();
                    break;
                case 6:
                    showStd();
                    break;
                case 7:
                    saveFile();
                    break;
                case 8:
                    readFile();
                    break;
                default:
                    System.exit(0);
                    break;
            }
        }
    }
    
    static void showMenu(){
        System.out.println("\n1. Add student.\n" +
        "2. Edit student by id.\n" +
        "3. Delete student by id.\n" +
        "4. Sort student by gpa.\n" +
        "5. Sort student by name.\n" +
        "6. Show student.\n"+
        "7. Save to student.txt.\n" +
        "8. Read from student.txt.\n" +
        "0. Exit.");
    }
    
    static void Edit(){
        Scanner input = new Scanner(System.in);
        System.out.println("Insert your ID: ");
        String id = input.nextLine();
        int dk=0;
        for (Student student : listStd) {
            if(student.getId().equalsIgnoreCase(id)){
                System.out.println("Edit Student:");
                student.input();
                dk=1;
                break;
            }
        }
        if(dk==0){
            System.out.println("Not found");
        }
    }
    
    static void Delete(){
        Scanner input = new Scanner(System.in);
        System.out.println("Insert your ID: ");
        String id = input.nextLine();
        int dk=0;
        for (Student student : listStd) {
            if(student.getId().equalsIgnoreCase(id)){
                listStd.remove(student);
                dk=1;
                System.out.println("Edit Student:");
                break;
            }
        }
        if(dk==0){
            System.out.println("Not found");
        }
    }
    
    static void showStd(){
        for (Student student : listStd) {
            student.output();
        }
    }
    
    static void SortGPA(){
        for ( int i=0;i<listStd.size()-1;i++) {
            for (int j = i+1; j < listStd.size(); j++) {
                if(listStd.get(j).getGpa()>listStd.get(i).getGpa()){
                    Collections.swap(listStd, i, j);
                }
            }
        }
    }
    
    static void SortName(){
        for ( int i=0;i<listStd.size()-1;i++) {
            for (int j = i+1; j < listStd.size(); j++) {
                if(listStd.get(i).getName().compareTo(listStd.get(j).getName())>0){
                    Collections.swap(listStd, i, j);
                }
            }
        }
    }
    
    static void saveFile(){
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("student.txt"); 
            String line;
            byte[] b;
            for (Student student : listStd) {
                line = student.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 {
                    System.out.println("Save successfull");
                    fos.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    static void readFile(){
        FileReader read = null;
        try {
            read = new FileReader("student.txt");
            BufferedReader bf = null;
            bf = new BufferedReader(read);
            
            String Line;
            while((Line = bf.readLine())!=null){
                System.out.println(Line);
            }
        } 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);
        }
    }
}



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

import java.util.Scanner;

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

    public Student() {
    }

    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 Float getGpa() {
        return gpa;
    }

    public void setGpa(Float gpa) {
        this.gpa = gpa;
    }
    
    public void input()
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Insert id: ");
        id = input.nextLine();
        System.out.print("Insert name: ");
        name = input.nextLine();
        System.out.print("Insert age: ");
        age = Integer.parseInt(input.nextLine());
        System.out.print("Insert address: ");
        address = input.nextLine();
        System.out.print("Insert gpa: ");
        gpa = Float.parseFloat(input.nextLine());
    }

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


trung [C1907L]

Ngày viết: 21:31 27/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 jv2;

import java.util.Scanner;

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

    public void input() {
        Scanner inp = new Scanner(System.in);
        System.out.println("Nhap vao id sinh vien");
        id = Integer.parseInt(inp.nextLine());
        System.out.println("Nhap vao ten sinh vien");
        name = inp.nextLine();
        System.out.println("Nha vao dia chi");
        address = inp.nextLine();
        System.out.println("Nhap vao tuoi");
        age = Integer.parseInt(inp.nextLine());
        System.out.println("Nhap vao gpa");
        gpa = Float.parseFloat(inp.nextLine());
    }

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

    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 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 float getGpa() {
        return gpa;
    }

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



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

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

/**
 *
 * @author prdox
 */
public class StudentMgr {

    List<Student> lstStudent;
    
    private static StudentMgr instance = null;
    
    public StudentMgr() {
        lstStudent = new ArrayList<>();
    }
    
    public synchronized static StudentMgr getInstance() {
        if (instance == null) {
            instance = new StudentMgr();
        }
        return instance;
    }
    
    public static void addStudent() {
        Student nStd = new Student();
        nStd.input();
        getInstance().lstStudent.add(nStd);
    }
    
    public static void editStudentById(int id) {
        Student edtStd = null;
        for (Student std : getInstance().lstStudent) {
            if (std.getId() == id) {
                edtStd = std;
                break;
            }
        }
        if (edtStd == null) return;
        Scanner inp = new Scanner(System.in);
        System.out.println("Nhap vao ten sinh vien");
        edtStd.setName(inp.nextLine());
        System.out.println("Nha vao dia chi");
        edtStd.setAddress(inp.nextLine());
        System.out.println("Nhap vao tuoi");
        edtStd.setAge(Integer.parseInt(inp.nextLine()));
        System.out.println("Nhap vao gpa");
        edtStd.setGpa(Float.parseFloat(inp.nextLine()));
    }
    
    public static void deleteStudentById(int id) {
        for (Student std : getInstance().lstStudent) {
            if (std.getId() == id) {
                getInstance().lstStudent.remove(std);
                break;
            }
        }
    }
    
    public static void sortStudentByGPA() {
        for (int i = 0; i < getInstance().lstStudent.size()-1; i++) {
            for (int j = i+1; j < getInstance().lstStudent.size(); j++) {
                if (getInstance().lstStudent.get(i).getGpa() > getInstance().lstStudent.get(j).getGpa()) {
                    Collections.swap(getInstance().lstStudent,i,j);
                }
            }
        }
    }
    
    public static void sortStudentByName() {
        for (int i = 0; i < getInstance().lstStudent.size()-1; i++) {
            for (int j = i+1; j < getInstance().lstStudent.size(); j++) {
                if (getInstance().lstStudent.get(i).getName().compareTo(getInstance().lstStudent.get(j).getName()) > 0) {
                    Collections.swap(getInstance().lstStudent,i,j);
                }
            }
        }
    }
    
    public static void showStudent() {
        getInstance().lstStudent.forEach((std) -> {
            std.output();
        });
    }
    
    public static void writeToFile() {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("student.txt");
            String line;
            byte[] b;
            for (Student std : getInstance().lstStudent) {
                line = std.toString();
                b = line.getBytes();
                fos.write(b);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(StudentMgr.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(StudentMgr.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException ex) {
                    Logger.getLogger(StudentMgr.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    public static void readFromFile() {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("student.txt");
            StringBuilder builder = new StringBuilder();
            int code;
            while ((code = fis.read()) != -1) {
                builder.append((char) code);
            }
            System.out.println(builder.toString());
        } catch (FileNotFoundException ex) {
            Logger.getLogger(StudentMgr.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(StudentMgr.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fis.close();
            } catch (IOException ex) {
                Logger.getLogger(StudentMgr.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }
}



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

import java.util.Scanner;

/**
 *
 * @author prdox
 */
public class Main {
    public static void main(String[] args) {
        int choose,id;
        Scanner inp = new Scanner(System.in);
        do {
            showMenu();
            choose = Integer.parseInt(inp.nextLine());
            switch(choose) {
                case 1:
                    StudentMgr.addStudent();
                    break;
                case 2:
                    id = Integer.parseInt(inp.nextLine());
                    StudentMgr.editStudentById(id);
                    break;
                case 3:
                    id = Integer.parseInt(inp.nextLine());
                    StudentMgr.deleteStudentById(id);
                    break;
                case 4:
                    StudentMgr.sortStudentByGPA();
                    break;
                case 5:
                    StudentMgr.sortStudentByName();
                    break;
                case 6:
                    StudentMgr.showStudent();
                    break;
                case 7:
                    StudentMgr.writeToFile();
                    break;
                case 8:
                    StudentMgr.readFromFile();
                    break;
                case 9:
                    System.out.println("Bye bye");
                    break;
                default:
                    System.out.println("Invalid input");
            }
            
        } while (choose != 9);
    }
    
    public 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("9. Exit.");
    }
}


Tài Liệu Tham Khảo

Ứng Dụng Tiện Ích



Đã sao chép!!!