java 学生成绩管理系统

Grade类:用来存储学生的信息

import java.io.Serializable;

class Grade implements Serializable{
	private int id;
	private String name;
	private double math;
	private double english;
	private double java;
	private double computer;
	private double average;

	public Grade(int id ,String name, double math, double english,double java,double computer) {
		this.id=id;
		this.name=name;
		this.math=math;
		this.english=english;
		this.java=java;
		this.computer=computer;
		average=(math+english+java+computer)/4;
	}

	@Override
	public String toString() {
		return "Grade [id=" + id + ", name=" + name + ", math=" + math + ", english=" + english + ", java=" + java
				+ ", computer=" + computer + "]";
	}

	public int getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getMath() {
		return math;
	}

	public void setMath(double math) {
		this.math = math;
	}

	public double getEnglish() {
		return english;
	}

	public void setEnglish(double english) {
		this.english = english;
	}

	public double getJava() {
		return java;
	}

	public void setJava(double java) {
		this.java = java;
	}

	public double getComputer() {
		return computer;
	}

	public void setComputer(double computer) {
		this.computer = computer;
	}
	public double getaverage() {
		return average;
	}

}

GradeLis类提供一些方法。例:添加成绩信息,删除成绩,查看平均成绩等方法

import java.util.Arrays;

public class GradeList {
	static Grade[] grade;
	public static  int studentCount;
	private int subject;
	
	/*
	 *Grade默认构造函数
	 */
	public GradeList() {
		this(10);
	}

	public GradeList(int size) {
		this.grade=new Grade[size];
		this.studentCount=0;
		this.subject=4;
	}
	
	/*
	 *显示成绩信息函数 
	 */
	public void show() {
		if(studentCount == 0) {
			System.out.println("nothing!");
		}
		averagesort();       	//调用平均成绩排序函数
		for(int i=0;i Mmax) {
							Mmax = grade[i].getMath();
						}
					}
					System.out.println("数学最高分 :"+Mmax);
					break;
			  case 2:			 //英语最高分 
					for(int i=0;i Emax) {
							Emax =grade[i].getEnglish();
						}
					}
					System.out.println("英语最高分: "+Emax);
					break;
			  case 3:			//java最高分 
					for(int i=0;i Jmax) {
							Jmax = grade[i].getJava();
						}
					}
					System.out.println("java最高分 : "+Jmax);
					break;
			  case 4:			//计算机基础最高分
					for(int i=0;i Cmax) {
							Cmax =grade[i].getComputer();
						}
					}
					System.out.println("计算机基础最高分: "+Cmax);
					break;
			}
	}
	
	/*
	 * 查最低分
	 */
	public void searchMin(int option) {
		double Mmin=grade[0].getMath(),Emin=grade[0].getEnglish(),Jmin=grade[0].getJava(),Cmin=grade[0].getComputer();
			
		switch(option) {
		//数学最低分
		case 1:
				for(int i=0;i

}

主函数:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.Scanner;

public class GradeManage {

public static Scanner in=new Scanner(System.in);
public static GradeList gradelist=new GradeList();

public static void main(String[] args) {
	// TODO Auto-generated method stubarg0	
	
	File file=new File("student.txt");
	try {
		if(file.exists()) {
				FileInputStream fInputStream = new FileInputStream(file);
				ObjectInputStream objectInputStream =new ObjectInputStream(fInputStream);
				GradeList.grade=(Grade[])objectInputStream.readObject();
				GradeList.studentCount = objectInputStream.readInt();
				objectInputStream.close();
		}
	}catch(Exception e1){
			
	}

	while(true) {
		System.out.println("----------------------------------");
		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("----------------------------------");
			
		int option=in.nextInt();
		if(option==6) {
			try {
					FileOutputStream fOutputStream = new FileOutputStream("student.txt");
					ObjectOutputStream objectOutputStream =new ObjectOutputStream(fOutputStream);
					objectOutputStream.writeObject(GradeList.grade);
					objectOutputStream.writeInt(GradeList.studentCount);
					objectOutputStream.close();
			}catch(Exception e1){
				
			}		
			break;
		}
		switch(option) {
			case 1:
				gradelist.show();	 		//显示所有成绩信息(按平均成绩降序)
				break;
			case 2:
				System.out.println(" 请输入你想查询的科目成绩(最高分,最低分): 1/数学,2/英语,/3java,4/计算机基础");
				int choice=in.nextInt();
				gradelist.searchMax(choice);			//查询各科最高分
				gradelist.searchMin(choice);			//查询各科最低分
				break;
			case 3:
				gradelist.average();			//平均成绩
				break;
			case 4:
				add();											//添加信息
				break;
			case 5:
				System.out.println("	请输入 id: ");
				gradelist.search(in.nextLine());			//查询学生成绩
			}
		}
		System.out.println(" 谢谢使用 !");
}

/*
 * 添加学生信息
 */
private static void add() {
		// TODO Auto-generated method stub
		System.out.println("请输入信息,请以逗号分割:(学号,姓名,数学成绩,英语成绩,java成绩,计算机成绩)");
		in.nextLine();			//读取回车键
		String input = in.nextLine();	 
		String[] str = input.split(",");
		for(int i=0; i

你可能感兴趣的:(java 学生成绩管理系统)