By GokiSoft.com|
15:02 25/03/2020|
Java Advanced
Share Code - Hướng dẫn chữa bài quản lý sinh vien + file
Hướng dẫn chữa bài quản lý sinh vien + file
Sour code
/*
* 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.lession3.files;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
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 Diep.Tran
*/
public class Main {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
List<Student> studentList = new ArrayList<>();
int choose;
do {
showMenu();
choose = Integer.parseInt(input.nextLine());
switch(choose) {
case 1:
addStudent(studentList);
break;
case 2:
editStudentById(studentList);
break;
case 3:
deleteStudentById(studentList);
break;
case 4:
sortStudentByGPA(studentList);
displayAllStudent(studentList);
break;
case 5:
sortStudentByName(studentList);
displayAllStudent(studentList);
break;
case 6:
displayAllStudent(studentList);
break;
case 7:
saveFile(studentList);
break;
case 8:
readFile(studentList);
displayAllStudent(studentList);
break;
case 9:
System.out.println("Exit!!!");
break;
default:
System.out.println("Input failed!!!");
break;
}
} while(choose != 9);
}
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");
System.out.println("Choose: ");
}
private static void addStudent(List<Student> studentList) {
System.out.println("Nhap so sinh vien can them: ");
int n = Integer.parseInt(input.nextLine());
for (int i = 0; i < n; i++) {
Student std = new Student();
//nhap thong tin sinh vien
std.input();
studentList.add(std);
}
}
private static void editStudentById(List<Student> studentList) {
System.out.println("Nhap ID sinh vien can sua thong tin: ");
int id = Integer.parseInt(input.nextLine());
//tim sinh vien
Student stdFind = null;
for (Student student : studentList) {
if(student.getId() == stdFind.getId()) {
stdFind = student;
break;
}
}
if(stdFind != null) {
//edit
stdFind.edit();
} else {
System.out.println("Not found");
}
}
private static void deleteStudentById(List<Student> studentList) {
System.out.println("Nhap ID sinh vien can xoa: ");
int id = Integer.parseInt(input.nextLine());
Student stdFind = null;
for (Student student : studentList) {
if(student.getId() == stdFind.getId()) {
stdFind = student;
break;
}
}
if(stdFind != null) {
studentList.remove(stdFind);
} else {
System.out.println("Not found");
}
}
private static void sortStudentByGPA(List<Student> studentList) {
Collections.sort(studentList, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if(o1.getGpa() > o2.getGpa()) {
return 1;
}
return -1;
}
});
}
private static void displayAllStudent(List<Student> studentList) {
for (Student student : studentList) {
student.display();
}
}
private static void sortStudentByName(List<Student> studentList) {
Collections.sort(studentList, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getName().compareToIgnoreCase(o2.getName());
}
});
}
private static void saveFile(List<Student> studentList) {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
String filename = "student.txt";
fos = new FileOutputStream(filename);
bos = new BufferedOutputStream(fos);
//ghi du lieu vao FILE
for (Student student : studentList) {
String line = student.getLine();
byte[] b;
try {
b = line.getBytes("utf8");
bos.write(b);
} catch (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);
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if(bos != null) {
bos.close();
}
if(fos != null) {
fos.close();
}
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private static void readFile(List<Student> studentList) {
FileReader reader = null;
BufferedReader bufferedReader = null;
try {
String filename = "student.txt";
reader = new FileReader(filename);
bufferedReader = new BufferedReader(reader);
//doc du lieu tu FILE
String line = null;
while((line = bufferedReader.readLine()) != null) {
Student std = new Student();
std.parseLine(line);
studentList.add(std);
}
} 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 {
if(bufferedReader != null) {
bufferedReader.close();
}
if(reader != null) {
reader.close();
}
} 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 java2.lession3.files;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class Student {
String name, address;
int id;
float gpa;
int age;
public Student() {
}
public Student(String name, int age, String address, int id, float gpa) {
this.name = name;
this.age = age;
this.address = address;
this.id = id;
this.gpa = gpa;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public float getGpa() {
return gpa;
}
public void setGpa(float gpa) {
this.gpa = gpa;
}
public void input() {
Scanner input = new Scanner(System.in);
System.out.println("Nhap ID: ");
id = Integer.parseInt(input.nextLine());
edit();
}
public void edit() {
Scanner input = new Scanner(System.in);
System.out.println("Nhap ten: ");
name = input.nextLine();
System.out.println("Nhap tuoi: ");
age = Integer.parseInt(input.nextLine());
System.out.println("Nhap dia chi: ");
address = input.nextLine();
System.out.println("Nhap diem trung binh: ");
gpa = Float.parseFloat(input.nextLine());
}
public void display() {
System.out.println(this);
}
@Override
public String toString() {
return "Student{" + "name=" + name + ", address=" + address + ", id=" + id + ", gpa=" + gpa + ", age=" + age + '}';
}
String getLine() {
//name, address, id, gpa, age
return name + "," + address + "," + id + "," + gpa + "," + age + "\n";
}
public void parseLine(String line) {
//remove whiteplace
line = line.trim();
String[] params = line.split(",");
if(params.length < 5) {
System.out.println("Data error");
return;
}
name = params[0];
address = params[1];
id = Integer.parseInt(params[2]);
gpa = Float.parseFloat(params[3]);
age = Integer.parseInt(params[4]);
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)