學生管理系統--改良版
阿新 • • 發佈:2018-12-11
學生管理系統–對學生的基本資訊進行增、刪、改、查,之前使用集合完成的,程式執行資料才存在,這次使用檔案來儲存,可以使資料在硬碟中儲存,該類需要專案的根目錄下放個“student.txt”檔案-------在控制檯顯示
主類:
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
System.out.println("【學生管理系統】");
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("請選擇以下功能:");
System.out.println("1、新增 2、刪除 3、修改 4、按學號查詢 5、全部查詢 6、退出");
int op = sc.nextInt();
switch (op) {
case 1:
addStudent(sc);
break ;
case 2:
deleteStudent(sc);
break;
case 3:
updateStudent(sc);
break;
case 4:
findById(sc);
break;
case 5:
findAll ();
break;
case 6:
System.out.println("謝謝使用,再見!");
System.exit(0);
default:
System.out.println("輸入有誤,請重新輸入!");
}
}
}
private static void findAll() {
ArrayList<Student> stuList = readAll();
System.out.println("【查詢結果】");
if (stuList == null || stuList.size() == 0) {
System.out.println("無資料!");
} else {
System.out.println("============================");
System.out.println("學號\t\t姓名\t\t性別\t\t年齡");
for (Student student : stuList) {
System.out.println(student.getStuNo() + "\t\t" + student.getStuName() + "\t\t"
+ student.getStuSex() + "\t\t" + student.getStuAge());
}
}
}
private static void findById(Scanner sc) {
ArrayList<Student> stuList = readAll();
System.out.println("請輸入要查詢的學號:");
int stuNo = sc.nextInt();
System.out.println("【查詢結果】");
for (Student student : stuList) {
if (student.getStuNo() == stuNo) {
System.out.println("學號\t\t姓名\t\t性別\t\t年齡");
System.out.println(student.getStuNo() + "\t\t" + student.getStuName() + "\t\t"
+ student.getStuSex() + "\t\t" + student.getStuAge());
return;
}
}
System.out.println("學號:" + stuNo + "--沒有找到!");
}
private static void updateStudent(Scanner sc) {
ArrayList<Student> stuList = readAll();
System.out.println("請輸入要修改的學號:");
int stuNo = sc.nextInt();
for (Student student : stuList) {
if (student.getStuNo() == stuNo) {
System.out.println("請輸入姓名:【輸入0 保留原值】");
String name = sc.next();
System.out.println("請輸入性別:【輸入0 保留原值】");
String sex = sc.next();
System.out.println("請輸入年齡:【輸入0 保留原值】");
int age = sc.nextInt();
if (!"0".equals(name)) {
student.setStuName(name);
}
if (!"0".equals(sex)) {
student.setStuSex(sex);
}
if (age != 0) {
student.setStuAge(age);
}
System.out.println("修改成功!");
writeAll(stuList);
return;
}
}
System.out.println("學號:" + stuNo + "--沒有找到!");
}
private static void deleteStudent(Scanner sc) {
ArrayList<Student> stuList = readAll();
System.out.println("請輸入要刪除的學號:");
int stuNo = sc.nextInt();
for (Student student : stuList) {
if (student.getStuNo() == stuNo) {
System.out.println(student);
System.out.println("確認是否刪除【y/n】");
String s = sc.next();
if ("y".equalsIgnoreCase(s)) {
stuList.remove(student);
writeAll(stuList);
System.out.println("刪除成功!");
return;
} else {
System.out.println("取消操作!");
return;
}
}
}
System.out.println("學號:"+stuNo+"--沒有找到!");
}
private static void addStudent(Scanner sc) {
ArrayList<Student> stuList = readAll();
int stuNo = 0;
a:
while (true) {
System.out.println("請輸入學號:");
stuNo = sc.nextInt();
for (Student student : stuList) {
if (student.getStuNo() == stuNo) {
System.out.println("學號:" + stuNo + "--已存在!");
continue a;
}
}
break;
}
System.out.println("請輸入姓名:");
String name = sc.next();
System.out.println("請輸入性別:");
String sex = sc.next();
System.out.println("請輸入年齡:");
int age = sc.nextInt();
stuList.add(new Student(stuNo, name, sex, age));
writeAll(stuList);
System.out.println("新增成功!");
}
private static ArrayList<Student> readAll() {
ArrayList<Student> stuList = new ArrayList<>();
try (
BufferedReader in = new BufferedReader(new FileReader("student.txt"))
) {
String row = null;
while ((row = in.readLine()) != null) {
String[] arr = row.split(",");
Student student = new Student();
student.setStuNo(Integer.parseInt(arr[0]));
student.setStuName(arr[1]);
student.setStuSex(arr[2]);
student.setStuAge(Integer.parseInt(arr[3]));
stuList.add(student);
}
return stuList;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static void writeAll(ArrayList<Student> stuList) {
try (
BufferedWriter out = new BufferedWriter(new FileWriter("student.txt"))
) {
for (Student student : stuList) {
out.write(student.getStuNo() + "," + student.getStuName() + "," + student.getStuSex() + "," + student.getStuAge());
out.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
學生類資訊:
public class Student {
private int stuNo;
private String stuName;
private String stuSex;
private int stuAge;
public Student() {
}
public Student(int stuNo, String stuName, String stuSex, int stuAge) {
this.stuNo = stuNo;
this.stuName = stuName;
this.stuSex = stuSex;
this.stuAge = stuAge;
}
public int getStuNo() {
return stuNo;
}
public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuSex() {
return stuSex;
}
public void setStuSex(String stuSex) {
this.stuSex = stuSex;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
@Override
public String toString() {
return "Student{" +
"stuNo=" + stuNo +
", stuName='" + stuName + '\'' +
", stuSex='" + stuSex + '\'' +
", stuAge=" + stuAge +
'}';
}
}