学生成绩管理系统功能增加操作的实现(java)

Java_学生成绩管理系统功能增加操作的实现

需求分析

1. 学生成绩管理系统应该有学生的学号,姓名,性别,年龄,数学成绩,英语成绩,语文成绩,总分;学生的学号不能重复,总分应为数学成绩、英语成绩和语文成绩之和。
2. 实现的功能为增删改查。
3. 遵从java封装的特性,应有学生类和增删改查功能类。

定义学生类

遵循封装特性,成员变量用private修饰,给每个成员变量设置get()和set()方法。
public class Student {
	//学号
	private int id;
	//姓名
	private String name;
	//年龄
	private int age;
	// 性别
	private char gender;
	//数学成绩
	private int mathScore;
	//语文成绩
	private int chnScore;
	//英语成绩
	private int engScore;
	//总分
	private int totalScore;
	public Student() {}

	public Student(String name, int age, char gender, int mathScore, int chnScore, int				engScore) {
	
		this.name = name;
		this.age = age;
		this.gender = gender;
		this.mathScore = mathScore;
		this.chnScore = chnScore;
		this.engScore = engScore;
	
		// 计算总分
		this.totalScore = mathScore + chnScore + engScore;
	}

	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 int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public char getGender() {
		return gender;
	}

	public void setGender(char gender) {
		this.gender = gender;
	}

	public int getMathScore() {
		return mathScore;
	}

	public void setMathScore(int mathScore) {
		// 修改总分 
		// 1. 减去原数学成绩
		this.totalScore -= this.mathScore;		
		// 2. 加上现有数学成绩
		this.totalScore += mathScore;		
		this.mathScore = mathScore;
	}
	public int getChnScore() {
		return chnScore;
}
	public void setChnScore(int chnScore) {
		// 参考数学成绩修改方法
		this.totalScore -= this.chnScore;
		this.totalScore += chnScore;
		this.chnScore = chnScore;
	}
	public int getEngScore() {
		return engScore;
	}
	public void setEngScore(int engScore) {
		// 参考数学成绩修改方法
		this.totalScore -= this.engScore;
		this.totalScore += engScore;
		this.engScore = engScore;
	}
	public int getTotalScore() {
		return totalScore;
	}
	public void setTotalScore(int totalScore) {
		this.totalScore = totalScore;
	}
	public int getRank() {
		return rank;
	}
	public void setRank(int rank) {
		this.rank = rank;
	}
}
如何保证总分的值一直为数学成绩 + 语文成绩 + 英语成绩的和

为保证总分与各科成绩相加一致,在输入学生每科成绩数据的时候自动计算总分的值;这里在有参的构造方法中设置totalScore = mathScore + chnScore + engScore。

public Student(String name, int age, char gender, int mathScore, int chnScore, int				engScore) {
	
		this.name = name;
		this.age = age;
		this.gender = gender;
		this.mathScore = mathScore;
		this.chnScore = chnScore;
		this.engScore = engScore;
	
		// 计算总分
		this.totalScore = mathScore + chnScore + engScore;
	}
保证学生的学号不重复

每个学生都有唯一的学号,学号重复会导致数据错误;为了避免输入错误的学号导致数据异常,我们定义一个静态变量,每当创建新的学生对象,将学生的学号设置为静态变量的值,然后静态变量的值+1,成为下一个学生对象的学号。

/**
 * 类对象计数器,每一次创建对象,赋值ID属性,同时自增长
 */
private static int count = 1;

// 构造代码块
{
	// 使用count计数器赋值ID属性,同时count++自增操作
	id = count++;
}

增删改查方法类的实现

以增加操作为例

创建一个Student类的数组用来存储学生数据

private Student[] allStus;

设置数组的容量,数组占内存实际的空间大小往往要比数组存储的数据大小多一点,这是因为数组的属性(比如.length())也跟着数组一起占用内存空间。

private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

定义数组初始化默认容量大小

private static final int DEFAULT_CAPACITY = 10;

定义一个计数器,计算数组中存储的学生的个数,每存放一个学生类数据,size自动加1

private int size = 0;

定义无参构造方法和有参构造方法

public StudentManager() {

	allStus = new Student[DEFAULT_CAPACITY];
}

/**
 * 给予用户初始化底层保存数据的数组容量大小空间
 * 
 * @param initCapacity 用户指定的初始化容量,不能小于等于0,不能大于MAX_ARRAY_SIZE
 * 					   MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8
 */
public StudentManager(int initCapacity) {
	// 不允许!!!
	if (initCapacity <= 0 || initCapacity > MAX_ARRAY_SIZE) {
		System.out.println("超出合理范围,给予一个默认的初始化容量使用");
		allStus = new Student[DEFAULT_CAPACITY];
	} else {
		// 满足要求,按照用户指定的容量创建对应的数组
		allStus = new Student[initCapacity];
	}
}

每次执行增加操作数组都有可能存满;所以定义一个数组扩容方法,每当执行增加操作时判断数据需不需要扩容,如果需要则执行扩容操作。

/**
 * 底层保存Student数据数组扩容方法
 * 
 * @param minCapacity 指定要求的最小容量,作为容量约束!!!
 */
private void grow(int minCapacity) {
	// 1. 获取源数组容量
	int oldCapacity = allStus.length;
	
	// oldCapacity >> 1 当前数据右移1 ==> / 2 但是效率高那么一丢丢
	// 2. 计算得到新数组容量,新数组容量是源数据数组的1.5倍左右
	int newCapacity = oldCapacity + (oldCapacity >> 1);
	
	// 3. 判断新数组容量要求是否能够满足最小容量要求
	if (minCapacity > newCapacity) {
		newCapacity = minCapacity;
	}
	
	// 4. 创建新数组
	Student[] temp = new Student[newCapacity];
	
	// 5. 复制源数据数组中的内容到新数组中
	for (int i = 0; i < size; i++) {
		temp[i] = allStus[i];
	}
	
	// 6. 保存新数组首地址
	allStus = temp;
}

写一个add()方法

/**
 * 添加一个学生类对象到底层Student数组allStus中
 * 
 * @param student Student类型对象
 * @return 添加成功返回true,否则返回false
 */
public boolean add(Student student) {
	
	// 如果发现size有效元素个数和数组的容量一致
	if (size == allStus.length) {
		// 最小要求是当前有效元素个数+1,因为目前是添加一个元素
		System.out.println("Array Grow!!!");
		grow(size + 1);
	}
	
	allStus[size] = student;
	size += 1;
	
	return true;
}

public void show() {
	for (int i = 0; i < size; i++) {
		System.out.println(allStus[i]);
	}
}
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值