java基础阶段学生信息管理系统

最近在学java基础,做了一些题目,还有很多不足,但会慢慢改进的。
使用IO流、集合等知识的小案例
实现功能:添加学生信息,删除学生信息,修改学生信息,查询学生信息,退出系统
缺点:每次运行后必须要先添加学生,可以读取文件中的信息,但是修改学生信息报异常,原因在于修改学生的分支语句里存储信息的文件采用new File()的方法
使用到的流:序列化流与反序列化流、输入输出流
关闭流:jdk1.7后try catch 可以自动关闭流,不必再手动关闭
try(

){
}
catch{

效果展示

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

添加学生信息

将学生信息存入list集合并返回

public ArrayList<Student> add(ArrayList<Student> list) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入学号:");
		Integer id = sc.nextInt();
		System.out.println("请输入姓名:");
		String name = sc.next();
		System.out.println("请输入年龄:");
		int age = sc.nextInt();
		list.add(new Student(id, name, age));
		Iterator<Student> it = list.iterator();
		System.out.println("是否继续添加学生信息y or n:");
		String pd = sc.next();
		if ((pd.equals("y")) || pd.equals("Y")) {
			add(list);
		}
		return list;
	}

在分支语句里接收list集合,使用ObjectOutputStream输出到文件中

case "1":
				try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(
						new FileOutputStream(new File("学生信息.txt")));) {
					ArrayList<Student> list = new ArrayList<Student>();
					objectOutputStream.writeObject(new ServiceImpl().add(list));
				} catch (Exception e) {
					e.printStackTrace();
				}

				break;

删除学生

先从文件中读取信息,用list集合存储,遍历list集合,删除符合要求的元素属性,返回删除后的list集合

public ArrayList<Student> deleteStudent(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
		Scanner sc = new Scanner(System.in);
		ArrayList<Student> list = new ArrayList<Student>();
		list = (ArrayList<Student>) objectInputStream.readObject();
		System.out.println("请输入要删除学生的学号:");
		Integer stuId = sc.nextInt();
		Iterator<Student> it = list.iterator();
		while (it.hasNext()) {
			Student stu = it.next();
			if (stu.getId().equals(stuId)) {
				it.remove();
			}
		}
		return list;
	}

在分支语句里接收list集合,使用ObjectOutputStream输出到文件中

case "4":
				try (ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("学生信息.txt"));) {
					ArrayList<Student> students = new ServiceImpl().deleteStudent(objectInputStream);
					try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(
							new FileOutputStream(new File("学生信息.txt")));) {

						objectOutputStream.writeObject(students);
					} catch (Exception e) {
						e.printStackTrace();
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
				break;

修改学生信息

先从文件中读取信息,用list集合存储,遍历list集合,修改符合要求的元素属性,返回修改好的list集合

public ArrayList<Student> updateStudent(ObjectInputStream objectInputStream)
			throws IOException, ClassNotFoundException {
		Scanner sc = new Scanner(System.in);
		ArrayList<Student> list = new ArrayList<Student>();
		list = (ArrayList<Student>) objectInputStream.readObject();
		System.out.println("请输入修改学生的学号:");
		Integer stuId = sc.nextInt();
		System.out.println("修改后的学号:");
		Integer id = sc.nextInt();
		System.out.println("修改后的学生姓名:");
		String name = sc.next();
		System.out.println("修改后的学生年龄:");
		Integer age = sc.nextInt();
		Iterator<Student> it = list.iterator();
		while (it.hasNext()) {
			Student stu = it.next();
			if (stu.getId().equals(stuId)) {
				stu.setId(id);
				stu.setName(name);
				stu.setAge(age);
			}
		}
		return list;
	}

在分支语句里接收list集合,使用ObjectOutputStream输出到文件中

case "3":

				try (ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("学生信息.txt"));) {
					ArrayList<Student> students = new ServiceImpl().updateStudent(objectInputStream);
					try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(
							new FileOutputStream(new File("学生信息.txt")));) {

						objectOutputStream.writeObject(students);
					} catch (Exception e) {
						e.printStackTrace();
					}

				} catch (Exception e) {
					e.printStackTrace();
				}

				break;

查询学生信息

从文件中读取信息,用list集合存储并遍历,分支语句中调用查询方法

public void query(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
		ArrayList<Student> list = new ArrayList<Student>();
		list = (ArrayList<Student>) objectInputStream.readObject();
		Iterator<Student> it = list.iterator();
		System.out.println("学号\t姓名\t年龄\t");
		while (it.hasNext()) {
			Student stu = it.next();
			System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge());
		}
	}

全部代码

package 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class 学生信息管理系统 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("欢迎登陆学生信息后台管理系统");
		while (true) {
			System.out.println("-----------学生信息管理系统----------");
			System.out.println();
			System.out.println("1、添加  2、查询  3、修改  4、删除  5、退出系统");
			System.out.println();
			System.out.println("---------------------------------");
			String choose = sc.next();
			switch (choose) {
			case "1":
				try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(
						new FileOutputStream(new File("学生信息.txt")));) {
					ArrayList<Student> list = new ArrayList<Student>();
					objectOutputStream.writeObject(new ServiceImpl().add(list));
				} catch (Exception e) {
					e.printStackTrace();
				}

				break;
			case "2":
				try (ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("学生信息.txt"));) {
					new ServiceImpl().query(objectInputStream);
				} catch (Exception e) {
					e.printStackTrace();
				}

				break;
			case "3":

				try (ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("学生信息.txt"));) {
					ArrayList<Student> students = new ServiceImpl().updateStudent(objectInputStream);
					try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(
							new FileOutputStream(new File("学生信息.txt")));) {

						objectOutputStream.writeObject(students);
					} catch (Exception e) {
						e.printStackTrace();
					}

				} catch (Exception e) {
					e.printStackTrace();
				}

				break;
			case "4":
				try (ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("学生信息.txt"));) {
					ArrayList<Student> students = new ServiceImpl().deleteStudent(objectInputStream);
					try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(
							new FileOutputStream(new File("学生信息.txt")));) {

						objectOutputStream.writeObject(students);
					} catch (Exception e) {
						e.printStackTrace();
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			case "5":
				new ServiceImpl().exit();
				break;
			}

		}
	}

}

class Student implements Serializable {
	private Integer id;
	private String name;
	private int age;

	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 Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Override
	public String toString() {
		return "学号:" + id + ",姓名:" + name + ", 年龄" + age;
	}

	public Student(Integer id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}

	public Student() {
		super();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (age != other.age)
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

}

interface Service {
	public ArrayList<Student> add(ArrayList<Student> list);

	public void query(ObjectInputStream objectInputStream) throws ClassNotFoundException, IOException;

	public ArrayList<Student> updateStudent(ObjectInputStream objectInputStream)
			throws IOException, ClassNotFoundException;
	public void exit();
	public ArrayList<Student> deleteStudent(ObjectInputStream objectInputStream)throws IOException, ClassNotFoundException;
}

class ServiceImpl implements Service {
	public ArrayList<Student> add(ArrayList<Student> list) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入学号:");
		Integer id = sc.nextInt();
		System.out.println("请输入姓名:");
		String name = sc.next();
		System.out.println("请输入年龄:");
		int age = sc.nextInt();
		list.add(new Student(id, name, age));
		Iterator<Student> it = list.iterator();
		System.out.println("是否继续添加学生信息y or n:");
		String pd = sc.next();
		if ((pd.equals("y")) || pd.equals("Y")) {
			add(list);
		}
		return list;
	}

	public void query(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
		ArrayList<Student> list = new ArrayList<Student>();
		list = (ArrayList<Student>) objectInputStream.readObject();
		Iterator<Student> it = list.iterator();
		System.out.println("学号\t姓名\t年龄\t");
		while (it.hasNext()) {
			Student stu = it.next();
			System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge());
		}
	}

	@Override
	public ArrayList<Student> updateStudent(ObjectInputStream objectInputStream)
			throws IOException, ClassNotFoundException {
		Scanner sc = new Scanner(System.in);
		ArrayList<Student> list = new ArrayList<Student>();
		list = (ArrayList<Student>) objectInputStream.readObject();
		System.out.println("请输入修改学生的学号:");
		Integer stuId = sc.nextInt();
		System.out.println("修改后的学号:");
		Integer id = sc.nextInt();
		System.out.println("修改后的学生姓名:");
		String name = sc.next();
		System.out.println("修改后的学生年龄:");
		Integer age = sc.nextInt();
		Iterator<Student> it = list.iterator();
		while (it.hasNext()) {
			Student stu = it.next();
			if (stu.getId().equals(stuId)) {
				stu.setId(id);
				stu.setName(name);
				stu.setAge(age);
			}
		}
		return list;
	}
	
	public void exit() {
		System.exit(0);
	}

	@Override
	public ArrayList<Student> deleteStudent(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
		Scanner sc = new Scanner(System.in);
		ArrayList<Student> list = new ArrayList<Student>();
		list = (ArrayList<Student>) objectInputStream.readObject();
		System.out.println("请输入要删除学生的学号:");
		Integer stuId = sc.nextInt();
		Iterator<Student> it = list.iterator();
		while (it.hasNext()) {
			Student stu = it.next();
			if (stu.getId().equals(stuId)) {
				it.remove();
			}
		}
		return list;
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值