java写的小的学生管理程序

import java.io.Serializable;

public class Student implements Serializable{
		private String name ; 
		private int age ;
		private int grade ;
		
		public Student(String name, int age, int grade) {
			this.name = name ;
			this.age = age ; 
			this.grade = grade ;
		}
		
		public void setName(String name) {
			this.name = name;
		}
		
		public void setAge(int age) {
			this.age = age ;
		}
		
		public void setGrade(int grade) {
			this.grade = grade;
		}
		
		public String getName() {
			return this.name;
		}
		
		public int getAge() {
			return this.age;
		}
		
		public int getGrade() {
			return this.grade;
		}
		
		public Student() {
			
		}
		public String toString() {
			return name+" "+age+" "+grade;
		}
		
}

  

以上是关于Student类的定义,大家也可以做自己的修改,这都是源码哦

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.ArrayList;
import java.util.Scanner;

public class Main {
	
	static ArrayList<Student> s = new ArrayList<Student>();   //设置为整个类都可以访问的 
	
	public static void main(String[] args) {
		try {
			System.out.println("数据载入中...");
			ObjectInputStream in = new ObjectInputStream(
					new FileInputStream("obj.bat"));
			try {
				s = ((ArrayList<Student>)in.readObject());
				System.out.println("数据载入完成!");
			} catch (ClassNotFoundException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			in.close();
		} catch (FileNotFoundException e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
		}
		while(true) {
			choose() ;
			try {
				ObjectOutputStream out = new ObjectOutputStream(
							new FileOutputStream("obj.bat"));
				out.writeObject(s);
				out.close();
			} catch (FileNotFoundException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}
	public static void choose() {
		Scanner in = new Scanner(System.in);
		System.out.println("1 add " + "2 rm " + "\n" + "3 show " + "4 quit" );
		int flag = Integer.parseInt(in.nextLine());
		//in.close();
		switch(flag) {
		case 1 : add(); break ;
		case 2 : rm(); break ;
		case 3 : show(); break ;
		case 4 : quit(); break ;
		}
	}
	
	public static void add() {
		Scanner in = new Scanner(System.in);
		Student s1 = new Student();
		System.out.println("please input the name of the student: ");
		s1.setName( in.nextLine() );
		System.out.println("please input the age of the student: ");
		s1.setAge(Integer.parseInt(in.nextLine()));
		System.out.println("please input the grade of the student: ");
		s1.setGrade(Integer.parseInt(in.nextLine())) ;
		s.add(s1);
		//in.close();
		System.out.println("over");
	}
	
	public static void show() {
		Student [] s_all = new Student[s.size()] ;
		s.toArray(s_all);
		for(Student s1 : s_all) {
			System.out.println(s1.toString());
		}
	}
	
	public static void quit() {
		System.out.println("exit");
		System.exit(0);
	}
	
	public static void rm() {
		byte [] temp = new byte[20];
		String name = "";
		try {
			System.in.read(temp);
			name = new String(temp);
		}catch(IOException e) {
		}
		Student [] s_all = new Student[s.size()];
		s.toArray(s_all);
		for(Student s1 : s_all) {
			
			if(s1.getName().trim().equals(name.trim())) {
				System.out.println(s.indexOf(s1));
				s.remove(s.indexOf(s1));
				System.out.println(s1.getName()+"have been removed");
			}
		}
	}
}

  

这上面的是一个主要的操作类,定义了一些可以进行的操作,大家也可以根据自己的需要进行修改,这里我只写了一个按照name删除的方法,其实也可以根据age和grade的操作,其实都是大同小异的。

在这里感觉在这个程序上收获最多的是关于ArrayList的使用,真的是方便,大大节省了关于存储文件和读取文件的过程,ObjectOutputStream和ObjectInputStream的使用大大减少了关于文件的操作。

当然了,这只是我作为一个初学者的认识,如果又不对的地方,还请大家指出来,我会更正的。

最后,等自己学完Swing之后,我争取可以做出一个带UI的程序,到时候也会分享的。

原文:https://blog.csdn.net/qq_42330141/article/details/89159173

posted @ 2019-05-14 17:11  持剑走天涯  阅读(313)  评论(0编辑  收藏  举报
指纹锁安装 淋浴房安装 晾衣架安装 窗帘安装 灯具安装 卫浴安装 智能锁安装 家具安装