//学生管理系统,利用IO实现
import java.util.Arrays;
public class Student {
private String name;
private String id;
private String gender;
private int age;
private double weight;
private double hight;
private String[] hobby;
public Student() {
}
public Student(String name, String id,String gender, int age, double weight, double hight, String[] hobby) {
this.name = name;
this.id = id;
this.gender = gender;
this.age = age;
this.weight = weight;
this.hight = hight;
this.hobby = hobby;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getHight() {
return hight;
}
public void setHight(double hight) {
this.hight = hight;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", id='" + id + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
", weight=" + weight +
", hight=" + hight +
", hobby=" + Arrays.toString(hobby) +
'}';
}
}
student类对象
下面利用了IO流来实现导入student.txt文件里面的信息
student.txt的内容
李响 2109124001 男 19 200.0 185.0 踢足球,玩手机 李佳豪 2109124002 男 18 178.0 120.0 打游戏,写代码 李佳豪 2109124003 男 20 183.0 150.0 写代码,弹吉他,旅游,拍风景 黄彦鑫 2109124004 男 21 120.0 168.0 刷抖音,旅游,爬山 常妍雨 2109124005 女 19 110.0 168.0 绝地求生,追剧,写代码 郝少杰 2109214007 男 19 120.0 178.0 打游戏,刷抖音,学习 王佳和 2109124012 男 20 200.0 185.0 学习,开车 吕佳俊 2109124018 男 19 100.0 180.0 学习,数学,C++ 赵凯悦 2109124022 男 19 160.0 178.0 算法,java,听歌 郑梓桐 2109124027 男 19 160.0 180.0 java,打篮球
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
/**
* 利用字符缓冲输出输入流对象实现
* IO流将StudentInfo文件中的数据导出来,然后更改,继续写入文件
*/
public class Arrays {
//主函数
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
ArrayList<Student> student = read();
pro();
System.out.print("请输入功能数字:");
int num = sc.nextInt();
while (num!=0) {
switch (num) {
case 0:
num=0;
break;
case 1:
student.add(wir());
break;
case 2:
select(student);
break;
case 3:
delete(student);
break;
case 4:
Update(student);
break;
case 5:
show();
break;
}
System.out.print("请再次输入数字:");
pro();
num=sc.nextInt();
}
}
//更新修改学生信息
public static void Update(ArrayList<Student> s) throws Exception {
System.out.print("请输入需要修改人的学号:");
Scanner sc = new Scanner(System.in);
String id = sc.next();
for (Student student : s) {
if(student.getId().equals(id)){
pro2();
System.out.print("请输入需要修改的项目的序号:");
int num = sc.nextInt();
while (num!=0){
switch (num){
case 0:
break;
case 1:
UpdateName(student);
WriteInTxt(s);
break;
case 2:
UpdateId(student);
WriteInTxt(s);
break;
case 3:
UpdateGenderStudent(student);
WriteInTxt(s);
break;
case 4:
UpdateAge(student);
WriteInTxt(s);
break;
case 5:
UpdateWeight(student);
WriteInTxt(s);
break;
case 6:
UpdateHight(student);
WriteInTxt(s);
break;
case 7:
UpdateHobby(student);
WriteInTxt(s);
break;
}
System.out.println();
pro2();
System.out.print("请输入需要修改的项目的序号:");
num= sc.nextInt();
}
}
}
}
//更新列表消息
public static void pro2(){
System.out.println("功能");
System.out.println("0,退出");
System.out.println("1,姓名");
System.out.println("2,学号");
System.out.println("3,性别");
System.out.println("4,年龄");
System.out.println("5,重量");
System.out.println("6,身高");
System.out.println("7,爱好");
}
//更新爱好
public static Student UpdateHobby(Student s){
Scanner sc = new Scanner(System.in);
System.out.print("请输入新的爱好:");
String str = sc.next();
String[] split = str.split(",");
s.setHobby(split);
return s;
}
//更新身高
public static Student UpdateHight(Student s){
Scanner sc = new Scanner(System.in);
System.out.print("请输入新的身高:");
s.setHight(sc.nextDouble());
return s;
}
//更新体重
public static Student UpdateWeight(Student s){
Scanner sc = new Scanner(System.in);
System.out.print("请输入新的体重:");
s.setWeight(sc.nextDouble());
return s;
}
//更新年龄
public static Student UpdateAge(Student s){
Scanner sc = new Scanner(System.in);
System.out.print("请输入新的年龄:");
s.setAge(sc.nextInt());
return s;
}
//更新性别
public static Student UpdateGenderStudent(Student s){
Scanner sc = new Scanner(System.in);
System.out.print("请输入新的性别:");
s.setGender(sc.next());
return s;
}
//更新ID
public static Student UpdateId(Student s){
Scanner sc = new Scanner(System.in);
System.out.print("请输入新的学号:");
s.setId(sc.next());
return s;
}
//更新名字
public static Student UpdateName(Student s){
Scanner sc = new Scanner(System.in);
System.out.print("请输入新的名字:");
s.setName(sc.next());
return s;
}
//写入文件
public static void WriteInTxt(ArrayList<Student> s) throws Exception{
BufferedWriter bw = new BufferedWriter(new FileWriter("StudentInfo.txt"));
for (Student student : s) {
String a = null;
StringBuffer b = new StringBuffer();
for(int i=0;i<student.getHobby().length;i++){
b.append(student.getHobby()[i]);
if(i!= student.getHobby().length-1){
b.append(",");
}
}
bw.write(student.getName()+" "+student.getId()+" "+student.getGender()+" "+student.getAge()+" "+student.getWeight()+" "+student.getHight()+" "+b.toString());
bw.newLine();
bw.flush();
}
}
//功能函数
public static void pro(){
System.out.println("功能");
System.out.println("0,退出");
System.out.println("1,添加学生");
System.out.println("2,查找学生");
System.out.println("3,删除学生");
System.out.println("4,更改学生信息");
System.out.println("5,打印全部学生信息");
}
//展示文件信息
public static void show() throws Exception{
BufferedReader br = new BufferedReader(new FileReader("StudentInfo.txt"));
String line = null;
while((line=br.readLine())!=null){
System.out.println(line);
}
}
//删除
public static void delete(ArrayList<Student> s) throws Exception{
Scanner sc = new Scanner(System.in);
BufferedWriter bw = new BufferedWriter(new FileWriter("StudentInfo.txt"));
System.out.println("请输入需要删除的人的姓名");
String name = sc.next();
for(int i =0 ;i<s.size();i++){
if(s.get(i).getName().equals(name)){
s.remove(i);
System.out.println("删除成功");
}
}
for (Student student : s) {
String a = null;
StringBuffer b = new StringBuffer();
for(int i=0;i<student.getHobby().length;i++){
b.append(student.getHobby()[i]);
if(i!= student.getHobby().length-1){
b.append(",");
}
}
bw.write(student.getName()+" "+student.getId()+" "+student.getGender()+" "+student.getAge()+" "+student.getWeight()+" "+student.getHight()+" "+b.toString());
bw.newLine();
bw.flush();
}
}
//查找
public static void select(ArrayList<Student> s){
Scanner sc = new Scanner(System.in);
System.out.println("请输入查找关键字(姓名 学号 性别 年龄 重量 身高 爱好 或者 全部信息)");
String next = sc.next();
switch (next){
case "姓名":
System.out.print("请输入要查找的姓名:");
String name = sc.next();
SelectByName(s, name);
break;
case "学号":
System.out.print("请输入要查找的学号:");
String id = sc.next();
SelectById(s,id);
break;
case "性别":
System.out.print("请输入要查找的性别:");
String sex = sc.next();
SelectBySex(s, sex);
break;
case "年龄":
System.out.print("请输入要查找的年龄:");
int age = sc.nextInt();
SelectByAge(s, age);
break;
case "重量":
System.out.print("请输入要查找的重量:");
double weight = sc.nextDouble();
SelectByWeight(s,weight);
break;
case "身高":
System.out.print("请输入要查找的身高:");
double hight = sc.nextDouble();
SelectByHeight(s,hight);
break;
case "爱好":
System.out.print("请输入要查找的爱好:");
String s1 = sc.nextLine();
SelectByHobby(s,s1);
break;
case "全部信息":
System.out.print("请输入要查找的姓名:");
Student s2 = new Student();
s2.setName(sc.next());
System.out.print("请输入要查找的学号:");
s2.setId(sc.next());
System.out.print("请输入要查找的性别:");
s2.setGender(sc.next());
System.out.print("请输入要查找的年龄:");
s2.setAge(sc.nextInt());
System.out.print("请输入要查找的重量:");
s2.setWeight(sc.nextDouble());
System.out.print("请输入要查找的身高:");
s2.setHight(sc.nextDouble());
System.out.print("请输入要查找的爱好:");
String n = sc.next();
String[] split = n.split(",");
s2.setHobby(split);
SelectByAll(s,s2);
}
}
//查找全部
public static void SelectByAll(ArrayList<Student> s,Student s1){
int flag = 0;
for (Student student : s) {
if(s1.toString().equals(student.toString())){
System.out.println(student);
flag=1;
}
}
if(flag==0){
System.out.println("!!!数据文件里面没有要查找的人!!!");
System.out.println();
}
}
//查找爱好
public static void SelectByHobby(ArrayList<Student> s ,String h){
String[] split = h.split(" ");
String s1 = split.toString();
System.out.println(s1);
int flag = 0;
for (Student student : s) {
if(s1.equals(student.getHobby().toString())){
System.out.println(student);
flag=1;
}
}
if(flag==0){
System.out.println("!!!数据文件里面没有要查找的人!!!");
System.out.println();
}
}
//查找身高
private static void SelectByHeight(ArrayList<Student> s ,double w) {
int flag = 0;
for (Student student : s) {
if(w==student.getHight()){
System.out.println(student);
flag=1;
}
}
if(flag==0){
System.out.println("!!!数据文件里面没有要查找的人!!!");
System.out.println();
}
}
// 查找体重
private static void SelectByWeight(ArrayList<Student> s ,double w) {
int flag = 0;
for (Student student : s) {
if(w==student.getWeight()){
System.out.println(student);
flag=1;
}
}
if(flag==0){
System.out.println("!!!数据文件里面没有要查找的人!!!");
System.out.println();
}
}
// 查找年龄
private static void SelectByAge(ArrayList<Student> s ,int age) {
int flag = 0;
for (Student student : s) {
if(age==student.getAge()){
System.out.println(student);
flag=1;
}
}
if(flag==0){
System.out.println("!!!数据文件里面没有要查找的人!!!");
System.out.println();
}
}
// 查找性别
private static void SelectBySex(ArrayList<Student> s ,String sex) {
int flag = 0;
for (Student student : s) {
if(sex.equals(student.getGender())){
System.out.println(student);
flag=1;
}
}
if(flag==0){
System.out.println("!!!数据文件里面没有要查找的人!!!");
System.out.println();
}
}
// 查找id
private static void SelectById(ArrayList<Student> s ,String id) {
int flag = 0;
for (Student student : s) {
if(id.equals(student.getId())){
System.out.println(student);
flag=1;
}
}
if(flag==0){
System.out.println("!!!数据文件里面没有要查找的人!!!");
System.out.println();
}
}
// 查找名字
public static void SelectByName(ArrayList<Student> s ,String next){
int flag = 0;
for (Student student : s) {
if(next.equals(student.getName())){
System.out.println(student);
flag=1;
}
}
if(flag==0){
System.out.println("!!!数据文件里面没有要查找的人!!!");
System.out.println();
}
}
// 由文件中读取出来,返回一个ArrayList集合
public static ArrayList<Student> read() throws Exception {
BufferedReader br = new BufferedReader(new FileReader("StudentInfo.txt"));
String line;
ArrayList<Student> student = new ArrayList<>();
//将学生信息由StudentInfo中读取进来,加载进ArrayList中
while((line=br.readLine())!=null){
Student Stu = new Student();
String[] s = line.split(" ");
String[] split = s[s.length-1].split(",");
Stu.setName(s[0]);
Stu.setId(s[1]);
Stu.setGender(s[2]);
Stu.setAge(Integer.parseInt(s[3]));
Stu.setWeight(Double.parseDouble(s[4]));
Stu.setHight(Double.parseDouble(s[5]));
Stu.setHobby(split);
student.add(Stu);
}
return student;
}
//写入或者加入一个学生对象
public static Student wir() throws Exception{
Scanner sc = new Scanner(System.in);
BufferedWriter bw = new BufferedWriter(new FileWriter("StudentInfo.txt",true));
Student stu = new Student();
System.out.println("请输入学生的信息(按照 姓名 学号 性别 年龄 重量 身高 爱好填写)");
//写入文件
String info = sc.nextLine();
bw.write(info);
bw.newLine();
bw.flush();
bw.close();
//加载成对象
Student Stu = new Student();
String[] s = info.split(" ");
String[] split = s[s.length-1].split(",");
Stu.setName(s[0]);
Stu.setId(s[1]);
Stu.setGender(s[2]);
Stu.setAge(Integer.parseInt(s[3]));
Stu.setWeight(Double.parseDouble(s[4]));
Stu.setHight(Double.parseDouble(s[5]));
Stu.setHobby(split);
return Stu;
}
}