利用目前所学JavaSE知识完善学生管理系统

目前学习知识对学生管理系统完善
完善点:

1、自定义异常处理

2、剥离排序算法核心内容,提高代码复用率(利用接口约束规范,插件式编程)

3、新增数据过滤展示(利用接口约束规范,插件式编程)

自定义异常处理
/**
 * 自定义异常,用于创建StudentManager对象时用户传入的初始化容量不合法操作!抛出异常处理
 * @author qiuyi
 *
 */
public class IllegalCapacityException extends Exception{

	private static final long serialVersionUID = 1L;
	
	public IllegalCapacityException() {
		
	}
	
	public IllegalCapacityException(String message) {
		super(message);
	}

}

/**
 * 自定义异常,用于在StudentManager对象底层数组扩容的过程发现数组的容量超出了最大数组容量要求
 * @author qiuyi
 *
 */
public class OverflowMaxArraySizeException extends Exception{

	
	private static final long serialVersionUID = 1L;
	
	public OverflowMaxArraySizeException() {
	}
	
	public OverflowMaxArraySizeException(String message) {
		super(message);
	}
}

异常抛出:

	/**
	 * 用户指定初始化容量,但是要求初始化容量在合理范围以内,不能小于0,不能大于数组容量最大值,MAX_ARRAY_SIZE
	 * 
	 * @param initCapacity 用户指定的初始化容量
	 * @throws IllegalCapacityException 用户初始化参数不合法异常
	 */
	public StudentManager(int initCapacity) throws IllegalCapacityException {
		if (initCapacity < 0 || initCapacity > MAX_ARRAY_SIZE) {
			/* 异常抛出!!!!,暂时未讲,补充知识点,暂时使用System.exit(0);退出程序 */
			throw new IllegalCapacityException("Input Parameter is Invalid");
		}
		allStus = new Student[initCapacity];
	}
/**
	 * 类内私有化方法,用于在添加元素过程中,出现当前底层数组容量不足的情况下对底层数组进行扩容操作,满足使用要求
	 * 
	 * @param minCapacity 添加操作要求的最小容量
	 * @throws OverflowMaxArraySizeException 数组容量超出最大范围
	 */
	private void grow(int minCapacity) throws OverflowMaxArraySizeException {
		// 1、获取原数组容量
		int oldCapacity = allStus.length;

		// 2、计算得到新数组容量,新数组容量大约是原数组的1.5倍
		int newCapacity = oldCapacity + oldCapacity / 2;

		// 3、判断新数组容量是否满足最小容量要求
		if (minCapacity > newCapacity) {
			newCapacity = minCapacity;
		}

		// 4、判断当前容量是否超出了MAX_VALUE_SIZE
		if (newCapacity > MAX_ARRAY_SIZE) {
			/* 这里需要一个异常处理,目前采用程序退出 */
			throw new OverflowMaxArraySizeException("Overflow Max_Array_Size");
		}

		// 5、创建新数组
		Student[] temp = new Student[newCapacity];

		// 6、数据拷贝
		for (int i = 0; i < oldCapacity; i++) {
			temp[i] = allStus[i];
		}

		// 7、使用allStus保存新数组首地址
		allStus = temp;
	}

异常捕获:

try {
					stm.add(new Student(100, "小明", 16, '男', 90.0F));
				} catch (OverflowMaxArraySizeException e) {
					System.out.println("数据已满!!!!!!!!");
				}
				break;
剥离排序算法核心内容

接口:

import com.qfedu.student.system.entity.Student;

public interface StudentCompare {
	/**
	 * 要求实现类完成的比较两个学生类对象的方法,具体的比较方式由实现类决定
	 * @param stu1 Student类型
	 * @param stu2 Student类型
	 * @return 比较结果返回boolean
	 */
	boolean compara(Student stu1, Student stu2); 
}

接口实现类:

import com.qfedu.student.system.compare.StudentCompare;
import com.qfedu.student.system.entity.Student;

/**
 * 学生成绩升序排序要求
 * 是StudentCompare接口实现类对象
 * 
 * @author qiuyi
 *
 */
public class ScoreAscCompare implements StudentCompare{

	@Override
	public boolean compara(Student stu1, Student stu2) {
		return stu1.getScore() > stu2.getScore();
	}
	
}
import com.qfedu.student.system.compare.StudentCompare;
import com.qfedu.student.system.entity.Student;

/**
 * 学生成绩降序排序要求
 * 是StudentCompare接口实现类对象
 * 
 * @author qiuyi
 *
 */
public class ScoreDescCompare implements StudentCompare{

	@Override
	public boolean compara(Student stu1, Student stu2) {
		return stu1.getScore() < stu2.getScore();
	}

}

排序算法:

/**
	 * 根据用户指定排序方式进行排序
	 * @param com 这里需要的参数类型是StudentCompare接口,要求传入的对象是StudentCompare接口实现类对象
	 */
	public void sortUsingCompare(StudentCompare com) {
		
		/*
		 * 排序算法操作不能再原数据数组中进行直接操作,需要另外准备一个用于排序的临时数组,这里就设计到一个数据拷贝过程
		 */
		Student[] sortTemp = new Student[size];
		
		// 数据拷贝
		for (int i = 0; i < sortTemp.length; i++) {
			sortTemp[i] = allStus[i];
		}
		
		// 降序排序(选择排序法)
		for (int i = 0; i < sortTemp.length - 1; i++) {
			//找出极值对应下标
			int maxIndex = i;
			//进行排序
			for (int j = i + 1; j < sortTemp.length; j++) {
				// 使用接口中规定的方法,替换比较规则!!!
				if (com.compara(sortTemp[maxIndex], sortTemp[j])) {
					maxIndex = j;
				}
			}
			if (maxIndex != i) {
				Student temp = sortTemp[maxIndex];
				sortTemp[maxIndex] = sortTemp[i];
				sortTemp[i] = temp;
			}
		}
		
		for (int i = 0; i < size; i++) {
			System.out.println(sortTemp[i]);
		}
	}
新增数据过滤展示:

接口:

public interface StudentFilter {

	/**
	 * 要求实现类完成的对学生对象进行数据过滤
	 * @param stu Student类对象
	 * @return boolean类型
	 */
	boolean accept(Student stu);
	
}

实现类:

public class ScoreThanOrEqualFiltrate implements StudentFilter {
	private float score;
	
	public ScoreThanOrEqualFiltrate(float score) {
		this.score = score;
	}

	@Override
	public boolean accept(Student stu) {
		return stu.getScore() >= score;
	}
}

public class GenderFiltrate implements StudentFilter{
	private char gender;
	
	public GenderFiltrate(char gender) {
		this.gender = gender;
	}

	@Override
	public boolean accept(Student stu) {
		return stu.getGender() == gender;
	}

}

核心代码:

	public void filterStudentInfo(StudentFilter stf) {
		
		// 根据接口实现类返回的条件进行数据筛选
		for (int i = 0; i < size; i++) {
			if (stf.accept(allStus[i])) {
				System.out.println(allStus[i]);
			}
		}
	}

完整代码压缩包已上传,文件名StudentSystemV2.1

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值