Day38-SE學生系統

流程圖

在這裏插入圖片描述

代碼

.entity

package com.zhc.entity;

import java.io.Serializable;

public class Student implements Serializable{

	private Integer id;
	private String name;
	private String className;
	private double javaScore;
	private double htmlScore;
	private double sumScore;
	public static int COUNT = 0;
	
	public Student(){}

	public Student(String name, String className, double javaScore, double htmlScore) {
		super();
		this.id = COUNT;
		COUNT++;
		this.name = name;
		this.className = className;
		this.javaScore = javaScore;
		this.htmlScore = htmlScore;
		this.sumScore = javaScore + htmlScore;
	}
	
	
	public Integer getId() {
		return id;
	}

	public String getName() {
		return name;
	}

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

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public double getJavaScore() {
		return javaScore;
	}

	public void setJavaScore(double javaScore) {
		this.javaScore = javaScore;
	}

	public double getHtmlScore() {
		return htmlScore;
	}

	public void setHtmlScore(double htmlScore) {
		this.htmlScore = htmlScore;
	}

	public double getSumScore() {
		return sumScore;
	}

	
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", className=" + className + ", javaScore=" + javaScore
				+ ", htmlScore=" + htmlScore + ", sumScore=" + sumScore + "]";
	}
	
	
	
	
	
}

.manager

package com.zhc.manager;

import java.io.*;
import java.util.ArrayList;

import com.zhc.entity.Student;
import com.zhc.util.SystemComparator;

public class SystemManagerForService {

	private ArrayList<Student> stuList = null;

	public SystemManagerForService() {
		stuList = new ArrayList<Student>();
		try {
			load();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 程序啓動後,讀取文件中的對象,存儲到集合中
	 * 
	 * @throws IOException
	 */
	private void load() throws IOException {
		File file = new File("D:\\data.txt");
		if (file.exists()) {
			FileInputStream fis = new FileInputStream(file);// 字節輸入流
			ObjectInputStream ois = new ObjectInputStream(fis);// 對象輸入流
			Object obj = null;
			Student stu = new Student();
			while (true) { // 循環讀入對象

				try {
					obj = ois.readObject();
					Student stu2 = (Student) obj;

					// 每次循環,找到當前對象id最大的,作爲自增id的當前值。
					Student.COUNT = stu.getId() > stu2.getId() ? stu.getId() : stu2.getId();

				} catch (Exception e) {
					break;
				}
				stuList.add((Student) obj);
			}

			System.out.println("初始化完成,有學員信息:" + stuList.size());
			ois.close();
			fis.close();

		} else {
			file.createNewFile();
		}

	}

	public void saveByStream() {
		try {
			FileOutputStream fos = new FileOutputStream("D:\\data.txt", false);
			ObjectOutputStream oos = new ObjectOutputStream(fos);

			for (int i = 0; i < stuList.size(); i++) {
				Student stu = stuList.get(i);
				oos.writeObject(stu);
			}
			oos.close();
			fos.close();

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

	public void seeAll() {
		for (int i = 0; i < stuList.size(); i++) {
			System.out.println(stuList.get(i));
		}
	}

	public boolean add(Student stu) {
		boolean b = stuList.add(stu);
		System.out.println("添加狀態:" + b);
		return b;
	}

	public Object deletData(int id) {
		checkId(id);
		Object o = stuList.remove(id);
		return o;
	}

	/**
	 * 檢查下標是否合法
	 * 
	 * @param id
	 */
	public void checkId(int id) {
		if (id < 0 || id >= stuList.size()) {
			throw new ArrayIndexOutOfBoundsException("出現問題:" + id);
		}
	}

	public Student get(int id) {
		System.out.println(stuList.get(id));
		return stuList.get(id);
	}

	public void sortByClass(String className) {
		for (int i = 0; i < stuList.size(); i++) {
			if (stuList.get(i).getClassName().equals(className)) {
				System.out.println(stuList.get(i));
			}
		}
	}

	public void sort(SystemComparator<Student> sc) {
		Object[] sortTemp = stuList.toArray();

		for (int i = 0; i < sortTemp.length - 1; i++) {

			for (int j = i + 1; j < sortTemp.length; j++) {

				if (sc.compare((Student) sortTemp[i], (Student) sortTemp[j]) < 0) {
					Student temp = (Student) sortTemp[i];
					sortTemp[i] = sortTemp[j];
					sortTemp[j] = temp;
				}
			}
		}
		for (Object object : sortTemp) {
			System.out.println(object);
		}

	}

}

.menu

package com.zhc.menu;

import java.util.Scanner;

import com.zhc.service.SystemServiceForMenu;

public class SystemMainMenu {

	static Scanner input = new Scanner(System.in);
	public static void main(String[] args) {

		SystemServiceForMenu ssm = new SystemServiceForMenu();
		
		while(true){
			System.out.println("1.查看所有學員、2.新增學員、"
					+ "3.根據ID刪除學員、4.根據ID查詢學員、"
					+ "5.根據ID修改信息、6.根據班級查詢學員、"
					+ "7.根據姓名查詢學員、8.根據需求排序、0.退出");
			System.out.println("輸入選項:");
			int choice = input.nextInt();
			switch(choice){
			case 1:{
				ssm.seeAll();
				break;
			}
			
			case 2:{
				ssm.add();
				break;
			}
			
			case 3:{
				ssm.deletData();
				break;
			}
			
			case 4:{
				ssm.get();
				break;
			}
			
			case 5:{
				ssm.upDate();
				break;
			}
			
			case 6:{
				ssm.sortByClass();
				break;
			}
			
			case 8:{
				ssm.sort();
				break;
			}
			
			case 0:{
				ssm.saveByStream();
				System.out.println("bye");
				return;
			}
			
			default:
				break;
			
			
			}
			
		}
		
	}

}

.service

package com.zhc.service;

import java.util.Scanner;

import com.zhc.entity.Student;
import com.zhc.manager.SystemManagerForService;
import com.zhc.util.SystemComparator;

public class SystemServiceForMenu {

	Scanner input = new Scanner(System.in);
	SystemManagerForService mfs = new SystemManagerForService();

	/**
	 * 查看全部成員
	 */
	public void seeAll() {
		System.out.println("開始查看所有學員");
		mfs.seeAll();
	}

	/**
	 * 添加一個成員
	 */
	public void add() {
		System.out.println("開始新增學員");
		System.out.print("輸入姓名:");
		String name = input.next();

		System.out.print("輸入班級:");
		String className = input.next();

		System.out.print("輸入JAVA:");
		double javaScore = input.nextDouble();

		System.out.print("輸入HTML:");
		double htmlScore = input.nextDouble();

		Student stu = new Student(name, className, javaScore, htmlScore);
		mfs.add(stu);
	}

	/**
	 * 根據ID刪除學員
	 */
	public void deletData() {
		System.out.println("開始刪除成員");
		System.out.println("輸入成員編號:");
		int id = input.nextInt();
		mfs.checkId(id);
		mfs.deletData(id);
	}

	/**
	 * 根據ID查詢學員
	 */
	public void get() {
		System.out.println("開始查詢單一學員");
		System.out.println("輸入ID:");
		int id = input.nextInt();
		mfs.checkId(id);
		mfs.get(id);
	}

	/**
	 * 根據ID修改成員信息
	 */
	public void upDate() {
		System.out.println("開始修改單一學員,輸入ID:");
		int id = input.nextInt();
		mfs.checkId(id);
		Student stu = mfs.get(id);
		while (true) {
			System.out.println("1.修改姓名、2.修改班級、3.修改JAVA、4.修改HTML、5.退出修改");
			int choice = input.nextInt();

			switch (choice) {
			case 1: {
				System.out.println("輸入新的");
				String name = input.next();
				stu.setName(name);
				break;
			}

			case 2: {
				System.out.println("輸入新的");
				String newClass = input.next();
				stu.setClassName(newClass);
				break;
			}

			case 3: {
				System.out.println("輸入新的");
				double newJava = input.nextDouble();
				stu.setJavaScore(newJava);
				break;
			}

			case 4: {
				System.out.println("輸入新的");
				double newHtml = input.nextDouble();
				stu.setHtmlScore(newHtml);
				break;
			}

			case 5: {
				return;
			}

			default:
				break;

			}
		}
	}

	/**
	 * 根據class查詢學生信息
	 */
	public void sortByClass() {
		System.out.println("開始根據班級查詢學員");
		System.out.println("輸入班級:");
		String className = input.next();

		mfs.sortByClass(className);
	}

	/**
	 * 排序
	 */
	public void sort() {
		// TODO Auto-generated method stub
		System.out.println("開始排序查詢學員");
		while (true) {
			System.out.println("輸入排序方式:1.按總分降序、0.退出");
			int choice = input.nextInt();
			switch (choice) {
			case 1:
				mfs.sort(new SystemComparator<Student>() {
					public int compare(Student stu1, Student stu2) {
						return (int) ((stu1.getSumScore() - stu2.getSumScore()) * 100);
					}

				});
				break;

			case 0: {
				return;
			}

			default:
				break;

			}

		}
	}

	public void saveByStream() {
		System.out.println("開始保存文件");
		mfs.saveByStream();
	}

}

.util

package com.zhc.util;

public interface SystemComparator<E> {

	public int compare(E stu1, E stu2);
}

運行圖

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章