By GokiSoft.com| 15:33 12/07/2023|
Java Advanced

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

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ình0. 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 ,

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

vuong huu phu [T2008A]
vuong huu phu

2021-03-13 11:56:11



/*
 * 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.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.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Admin
 */
public class test {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
     int lua_chon, n ;
     List<Sv> svlist = new ArrayList<>();
        do {   
            menu();
        System.out.println("Nhap su lua chon");
        lua_chon =Integer.parseInt(sc.nextLine());
        
        switch(lua_chon){
            
            case 1:
                System.out.println("Nhap so luong sv");
                n = Integer.parseInt(sc.nextLine());
                for (int i = 0; i < n; i++) {
                    Sv sv = new Sv();
                    sv.nhap();
                    svlist.add(sv);
                }
                break;
                
                case 2:
                     System.out.println("Nhap id can sua");
                    String sid = sc.nextLine();
                    int e = 0;
                    for (Sv sv : svlist) {
                        if (sv.id.equalsIgnoreCase(sid)) {
                         svlist.remove(sv);
                         e++;
                        }
                        sv.hienthi();
                    }
                    if (e == 0 ) {
                        System.out.println("Khong tim thay sinh vien co ma la "+ sid);
                    }
                    if(e != 0){
                        System.out.println("ooooo");
                        Sv sv1= new Sv(); 
                        sv1.nhap();
                        svlist.add(sv1);
                    }
                    
                break;
                case 3:
                    
               int t =0;
                    System.out.println("Nhap id can xoa");
                    String eid = sc.nextLine();
                    for (Sv sv : svlist) {
                        if (sv.id.equalsIgnoreCase(eid)) {
                         svlist.remove(sv);
                          t++;
                        }
                        sv.hienthi();
                    }
                    if (t == 0 ) {
                        System.out.println("Khong tim thay sinh vien co ma la "+ eid);
                    }
                    
                break;
                
                case 4:
                    Collections.sort(svlist, (Sv o1, Sv o2) -> {
                        if (o1.getGpa() > o2.getGpa()) {
                            return 1;
                        }else{
                            return-1;
                        }
            });
                    break;
                    
                case 5:
                  Collections.sort(svlist, (Sv o1, Sv o2) -> {
                      if (o1.getName().compareToIgnoreCase(o2.getName()) > 0) {
                          return 1;
                      }else{
                          return -1;
                      }
            });
                break;
                
                case 6:
                for (int i = 0; i < svlist.size(); i++) {
                        svlist.get(i).hienthi();
                    }
                break;

                case 7:
                    FileOutputStream fos = null;
                    ObjectOutputStream oos= null;
                try {
                    fos = new FileOutputStream("student.txt");
                    oos = new ObjectOutputStream (fos);
                    oos.writeObject(svlist);
                } 
                
                catch (FileNotFoundException ex) {
                    Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                    
                catch (IOException ex) {
                    Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
                }
                 finally{
                    if (fos != null) {
                        try {
                            fos.close();
                        } catch (IOException ex) {
                            Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                    if (oos != null) {
                        try {
                            oos.close();
                        } catch (IOException ex) {
                            Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            
                    break;
                 
                case 8:
                    FileInputStream fis = null;
                    ObjectInputStream ois = null;
                try {
                    fis = new FileInputStream("student.txt");
                    ois = new ObjectInputStream (fis);
                svlist = (List<Sv>) ois.readObject();
                    System.out.println(svlist);
                }
                 catch (ClassNotFoundException | IOException ex) {
                    Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
                }
                 finally{
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException ex) {
                            Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                    if (ois != null) {
                        try {
                            ois.close();
                        } catch (IOException ex) {
                            Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
                break;
                case 0:
                    System.out.println("Thoat!!!!!!!!!!!");
                break;
                default:
                    System.err.println("Nhap lai!!!!");
        }
        } while (lua_chon < 7);
    }
    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. Luu thong tin vao file student.txt");
        System.out.println("8. Doc thong tin sinh vien tu file student.txt");
        System.out.println("0 Thoat!!!!");
    }
}



vuong huu phu [T2008A]
vuong huu phu

2021-03-13 11:56:01



/*
 * 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]
Nguyễn Tiến Đạt

2021-03-12 15:17:51


#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]
Lê Trí Dũng

2020-04-29 08:59:47



/*
 * 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]
Nguyễn Hữu Đạt

2020-04-29 08:00:27



/*
 * 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]
Nguyễn Hoàng Anh

2020-04-28 06:29:09



/*
 * 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
    }

}



Nguyễn Hoàng Anh [C1907L]
Nguyễn Hoàng Anh

2020-04-28 06:24:18



/*
 * 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]
Vũ Việt Đức

2020-04-27 15:59:32



/*
 * 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]
Hoàng Quang Huy

2020-04-27 15:46:21




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ô Quang Huy

2020-04-27 15:00:09



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