学生简易管理系统

功能:
存储学生成绩学号名字姓名等信息
实现简单的管理查询
实现类专门调用方法类中的方法

public class Demo {
	public static void main(String[] args) throws Exception {
		StudentSystem ss=new StudentSystem();
		ss.showMenu();
	}
}

方法类

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StudentSystem {
	Scanner input=new Scanner(System.in);
	static List<Student> stuList=new ArrayList<Student>();
	static {
//		stuList.add(new Student("张三","男",21,"123",60));
//		stuList.add(new Student("李四","女",22,"124",70));
//		stuList.add(new Student("王五","男",19,"125",50));
//		stuList.add(new Student("赵六","女",18,"122",55));
//		FileUtils.saveFile(stuList);
		try {
			stuList=FileUtils.getFile();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void showMenu() throws Exception {
		while(true) {
			System.out.println("");
			System.out.println("操作\t1,查看\t2,新增\t3,修改\t4,删除\t5,退出系统");
			String chose=input.next();
			switch (chose) {
			case "1":
				//查询方法
				showStudentMessage();
				break;
			case "2":
				//新增学员方法
				addStudent();
				break;
			case "3":
				//修改学员信息
				updateStudentInfo();
				break;
			case "4":
				//删除学员信息
				delStudent();
				break;
			case "5":
				System.out.println("欢迎下次使用");
				return;

			default:
				System.out.println("没有该选项请重新选择");
				break;
			}
		}
	}
	//删除学员信息
	public void delStudent() throws Exception {
		stuList=FileUtils.getFile();
		System.out.println("输入要删除学生的学号");
		String num=input.next();
		Student s=stuList.stream().filter(x->x.getNum().equals(num)).findAny().orElse(null);
		if(s==null) {
			System.out.println("没有该学员");
			return;
			
		}		
		stuList.remove(s); 
		FileUtils.saveFile(stuList);
		
	}
	//修改学员信息
	public void updateStudentInfo() throws Exception {
		stuList=FileUtils.getFile();
		System.out.println("输入该学员的学号");
		String num=input.next();
		
		try {
			Student s=stuList.stream().filter(x->x.getNum().equals(num)).findAny().get();
			System.out.println("请输入学生的姓名");
			String name=input.next();
			System.out.println("请输入学生的性别");
			String sex=input.next();
			System.out.println("请输入学生的年龄");
			int age=input.nextInt();
			System.out.println("请输入学生的成绩");
			double score=input.nextDouble();
			System.out.println("修改完成");
			s.setName(name);
			s.setAge(age);
			s.setScore(score);
			s.setSex(sex);
			FileUtils.saveFile(stuList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("没有该人");
			return;
		}		
	}
	//新增学员
	public void addStudent() throws Exception {
		stuList=FileUtils.getFile();
		System.out.println("请输入学生的姓名");
		String name=input.next();
		System.out.println("请输入学生的性别");
		String sex=input.next();
		System.out.println("请输入学生的年龄");
		int age=input.nextInt();
		System.out.println("请输入学生的学号");
		String num=input.next();
		System.out.println("请输入学生的成绩");
		double score=input.nextDouble();
		Student s=new Student(name, sex, age, num, score);
		//检测学员是否已经存在  
		boolean a=stuList.stream().anyMatch(x->x.getName().equals(s.getName())&&x.getNum().equals(s.getNum()));
		if(a) {
			System.out.println("已有此人");
		}else {
			System.out.println("添加成功");
			stuList.add(s);
		}
		FileUtils.saveFile(stuList);
		
	}
	//查看学员信息
	public void showStudentMessage2() throws Exception {
		stuList=FileUtils.getFile();
		System.out.println("名字"+"\t"+"性别"+"\t"+"年龄"+"\t"+"学号"+"\t"+"成绩");
		
		stuList.stream().forEach(System.out::println);
		
	}
	public void showStudentMessage() throws Exception {
		stuList=FileUtils.getFile();
		System.out.println("你想按什么排序1,成绩2,学号");
		String a=input.next();
		
			switch (a) {
			case "1":
				System.out.println("名字"+"\t"+"性别"+"\t"+"年龄"+"\t"+"学号"+"\t"+"成绩");
				stuList.stream().sorted((s1,s2)->Double.compare(s1.getScore(),s2.getScore())).forEach(System.out::println);
				break;
			case "2":
				System.out.println("名字"+"\t"+"性别"+"\t"+"年龄"+"\t"+"学号"+"\t"+"成绩");
				stuList.stream().sorted((s1,s2)->Integer.compare(Integer.valueOf(s1.getNum()), Integer.valueOf(s2.getNum()))).forEach(System.out::println);
				break;
			default:
				System.out.println("请重新选择");
				break;
			}	
	}
}

管理文件信息类,实现的数据格式大小等的控制

import java.io.Serializable;

public class Student implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8125610403783569770L;
	private String name;
	private String sex;
	private int age;
	private String num;
	private double score;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getNum() {
		return num;
	}
	public void setNum(String num) {
		this.num = num;
	}
	public double getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}
	public Student(String name, String sex, int age, String num, double score) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
		this.num = num;
		this.score = score;
	}
	public Student() {
		super();
	}
	@Override
	public String toString() {
		return name + "\t" + sex + "\t" + age + "\t" + num + "\t" + score;
	}
	
}

设置存储文件等工具类

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.List;

public class FileUtils {
	private final static String PATH="d:\\student.info";
	public static void saveFile(List<Student> list) {
		ObjectOutputStream  osi=null;
		try {
			osi=new ObjectOutputStream(new FileOutputStream(PATH));
			
			osi.writeObject(list);
			osi.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				osi.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static List<Student> getFile() throws Exception{
		ObjectInputStream ois=null;
		try {
			ois=new ObjectInputStream(new FileInputStream(PATH));
			List<Student> list=(List<Student>)ois.readObject();
			return list;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				ois.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值