面向对象案例-学生信息管理系统V0.6

更新版本

面向对象案例 - 学生信息管理系统V1.0

项目要求:

实体类:
	学生类:
		id, 姓名,年龄,性别,成绩
	需要使用数组保存学生信息
		Student[] allStu
	需要完成的方法
		1. 根据学生的ID,找到对应的学生对象
		2. 完成方法,添加新学生
		3. 完成方法,删除指定ID的学生
		4. 完成方法,展示数组中所有的学生信息
		5. 根据学生成绩,完成降序排序

项目分析

1.创建Student类,定义学生的各项信息
2.创建ArrayFunction类,定义学生信息的各项操作。
3.创建Demo主类

Student类分析实现

1.定义学生类,创建私有成员属性
   	private int id;
	private String name;
	private int age;
	private char gender;
	private float score;
    其中需要注意的是,id项由程序生成,非人工自行输入,只累加,不减少
2.定义有参无参构造方法
3.实现各项成员属性getter,setter方法

public class Student {
	private int id;
	private String name;
	private int age;
	private char gender;
	private float score;
	
	public Student() {}

	public Student(String name, int age, char gender, float score) {

		this.name = name;
		this.age = age;
		this.gender = gender;
		this.score = score;
	}


	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 float getScore() {
		return score;
	}

	public void setScore(float score) {
		this.score = score;
	}
}

ArrayFunc类分析实现

public class ArrayFunc {
        //成员属性:
    	
	int count = 0;				// 累计学生数量累加
	int size = 0;				// 数组长度计数 == 5个
	Student[] allStu = new Student[5];	// 初始化先设置容量Capacity == 5个

	/**
	 * 根据传入的学生id查找对应的学生信息
	 * 
	 * @param id 学生 目标id
	 */
	public void find(int id) {
		int temp = -1;

		for (int i = 0; i < size; i++) {
			if (id == allStu[i].getId()) {
				temp = i;
				break;
			}
		}

		if (temp < 0) {
			System.out.println("你要查找的id不存在");
		} else {
			System.out.println("id是:" + allStu[id].getId() + ",姓名是:" + allStu[id].getName());
		}

	}

	/**
	 * 传入一个学生元素,插入到对应数组中
	 * 插入数组之前调用check函数判断数组是否满员,check返回true就直接插入,返回false则调用扩容;
	 * 
	 * @param student Student类型的元素
	 */
	public void insert(Student student) {
		boolean flag = this.check();

		if (flag) {
			allStu[size] = student;
			student.setId(count);
			size++;
			count++;
		}
	}

	/**
	 * 判断数组当前有效元素长度是否小于数组长度;
	 * 
	 * @return 确认当前有效元素长度小于数组长度后返回true,否则扩容后返回true
	 */
	public boolean check() {
		boolean flag = false;
		if (size < allStu.length - 1) {//数组提前扩容,后续展示空间不需考虑越界问题
			flag = true;
		} else {
			expand();
			flag = true;
		}
		return flag;
	}

	/**
	 * 数组扩容
	 * 
	 */
	public void expand() {
		Student[] newStu = new Student[allStu.length * 2];
		System.arraycopy(allStu, 0, newStu, 0, allStu.length);

		allStu = newStu;
	}

	/**
	 * 删除指定id的元素
	 * 
	 * @param delId 要删除的指定id
	 */
	public void del(int delId) {

		int delIndex = size + 1;
		boolean flag = true;

		for (int i = 0; i < size; i++) {
			if (delId == allStu[i].getId()) {
				delIndex = i;
				flag = false;
				size--;
				break;
			}
		}

		if (flag) {
			System.out.println("你要删除的id:" + delId + "不存在");
		}

		
		
		for (int i = delIndex; i <= size; i++) {
			allStu[i] = allStu[i + 1];
		}

	}

	/**
	 * 根据学生分数排序算法
	 * 
	 */
	public void sort() {
		for (int i = 0; i < size - 1; i++) {
			for (int j = i + 1; j < size; j++) {
				if (allStu[i].getScore() < allStu[j].getScore()) {
					Student temp = allStu[i];
					allStu[i] = allStu[j];
					allStu[j] = temp;
				}
			}
		}
	}
	
	/**
	 * 展示数组
	 */
	public void show() {
		for (int i = 0; i < size; i++) {
			System.out.println("id是:" + allStu[i].getId() + ",姓名是:" + allStu[i].getName());
		}
	}

}

Demo主类实现

public class Demo {
	public static void main(String[] args) {

		// 生成Student 对象
		Student student = new Student("张0", 18, '男', 90F);//匿名对象

		ArrayFunc arrFunc = new ArrayFunc();

		arrFunc.insert(student);

		arrFunc.insert(new Student("张1", 26, '男', 91F));
		arrFunc.insert(new Student("张2", 26, '男', 92F));
		arrFunc.insert(new Student("张3", 26, '男', 93F));
		arrFunc.insert(new Student("张4", 26, '男', 94F));
		arrFunc.insert(new Student("张5", 26, '男', 95F));
		arrFunc.insert(new Student("张6", 26, '男', 96F));
		arrFunc.insert(new Student("张7", 26, '男', 97F));
		arrFunc.insert(new Student("张8", 26, '男', 98F));
		arrFunc.insert(new Student("张9", 26, '男', 99F));
		
		arrFunc.find(6);// 根据下标找到对应同学
		System.out.println("-----------------");
		
		arrFunc.show();// 展示所有的学生信息
		System.out.println("-----------------");

		arrFunc.sort();// 这里加入allStu为什么报错?
		arrFunc.show();
		System.out.println("-----------------");
		
		arrFunc.del(0);// 删除指定id学生
		arrFunc.show();
		System.out.println("-----------------");
		
		arrFunc.insert(new Student("张10", 26, '男', 99F));

		arrFunc.show();
		System.out.println("-----------------");	
		
                arrFunc.sort();
		arrFunc.show();
		System.out.println("-----------------");		
		
		
	}
}

代码结果

id是:6,姓名是:张6
-----------------
id是:0,姓名是:张0
id是:1,姓名是:张1
id是:2,姓名是:张2
id是:3,姓名是:张3
id是:4,姓名是:张4
id是:5,姓名是:张5
id是:6,姓名是:张6
id是:7,姓名是:张7
id是:8,姓名是:张8
id是:9,姓名是:张9
-----------------
id是:9,姓名是:张9
id是:8,姓名是:张8
id是:7,姓名是:张7
id是:6,姓名是:张6
id是:5,姓名是:张5
id是:4,姓名是:张4
id是:3,姓名是:张3
id是:2,姓名是:张2
id是:1,姓名是:张1
id是:0,姓名是:张0
-----------------
id是:9,姓名是:张9
id是:8,姓名是:张8
id是:7,姓名是:张7
id是:6,姓名是:张6
id是:5,姓名是:张5
id是:4,姓名是:张4
id是:3,姓名是:张3
id是:2,姓名是:张2
id是:1,姓名是:张1
-----------------
id是:9,姓名是:张9
id是:8,姓名是:张8
id是:7,姓名是:张7
id是:6,姓名是:张6
id是:5,姓名是:张5
id是:4,姓名是:张4
id是:3,姓名是:张3
id是:2,姓名是:张2
id是:1,姓名是:张1
id是:10,姓名是:张10
-----------------
id是:9,姓名是:张9
id是:10,姓名是:张10
id是:8,姓名是:张8
id是:7,姓名是:张7
id是:6,姓名是:张6
id是:5,姓名是:张5
id是:4,姓名是:张4
id是:3,姓名是:张3
id是:2,姓名是:张2
id是:1,姓名是:张1
-----------------

代码优化

ArrayFunc类

package stuinfotwo;

import java.util.Scanner;

public class ArrayFunc {

	// 初始化先设置容量Capacity == 5个
	int size = 0;
	Student[] allStu = new Student[5];

	/**
	 * 根据传入的学生id查找对应的学生信息
	 * 
	 * @param id 学生 目标id
	 * @return 考虑增加return的范围,把打印功能独立。。。。。。。。。。。。。。
	 */
	public void find(int id) {

		int temp = -1;
		for (int i = 0; i < size; i++) {
			if (id == allStu[i].getId()) {
				temp = i;
				break;
			}
		}

		if (temp < 0) {
			System.out.println("你要查找的id不存在");
		} else {
			System.out.println("学生id:" + allStu[id].getId() + ",姓名:" + allStu[id].getName() + ",年龄:"+ allStu[id].getAge() + ",性别:" + allStu[id].getGender() + ",成绩:" + allStu[id].getScore());
		}

	}

	/**
	 * 生成学生对象
	 */

	public void gene() {
		Scanner sc = new Scanner(System.in);

		System.out.println("请输入姓名:");
		String name = sc.next();

		System.out.println("请输入年龄:");
		int age = sc.nextInt();
		System.out.println("请输入性别:");
		char gender = sc.next().charAt(0);
		System.out.println("请输入分数:");
		float score = sc.nextFloat();

		Student stuTemp = new Student(name, age, gender, score);

		insert(stuTemp);
	}

	/**
	 * 传入一个学生元素,插入到对应数组中 插入数组之前调用check函数判断数组是否满员,check返回true就直接插入,返回false则调用扩容;
	 * 
	 * @param student Student类型的元素
	 */

	public void insert(Student student) {
		boolean flag = this.check();// this指代当前对象,调用该对象的check方法

		if (flag) {
			allStu[size] = student;
			student.setId(size);
			size++;
		}
	}

	/**
	 * 判断数组当前有效元素长度是否小于数组长度;
	 * 
	 * @return 确认当前有效元素长度小于数组长度后返回true,否则扩容后返回true
	 */

	public boolean check() {
		boolean flag = false;
		if (size < allStu.length) {
			flag = true;
		} else {
			expand();
			flag = true;
		}
		return flag;
	}

	/**
	 * 数组扩容
	 * 
	 */
	public void expand() {
		Student[] newStu = new Student[allStu.length * 2];
		System.arraycopy(allStu, 0, newStu, 0, allStu.length);

		allStu = newStu;

	}

	/**
	 * 删除指定id的元素
	 * 
	 * @param delId 要删除的指定id
	 */

	public void del(int delId) {

		int delIndex = size; // 要删除的id在数组中的下标
		boolean flag = true; // 标记数组中是否有该下标元素

		for (int i = 0; i < size; i++) {
			if (delId == allStu[i].getId()) {
				delIndex = i; // 找到该元素在数组中的下标
				flag = false; // 设为false,说明找到了
				size--; // 数组元素减1
				break; // 跳出循环
			}
		}

		// 输出元素不存在
		if (flag) {
			System.out.println("你要删除的id:" + delId + "不存在");
			return;
		}

		// 元素存在,置换位置
		for (int i = delIndex; i < size; i++) {
			allStu[i] = allStu[i + 1];
		}
		allStu[size] = null;
	}

	/**
	 * 展示数组
	 */
	public void show() {
		for (int i = 0; i < size; i++) {
			System.out.println("学生id:" + allStu[i].getId() + ",姓名:" + allStu[i].getName() + ",年龄:" + allStu[i].getAge()+ ",性别:" + allStu[i].getGender() + ",成绩:" + allStu[i].getScore());
		}
	}

	/**
	 * 根据成员分数排序算法
	 * 
	 */

	public void sort() {
		for (int i = 0; i < size - 1; i++) {
			for (int j = i + 1; j < size; j++) {
				if (allStu[i].getScore() < allStu[j].getScore()) {
					Student temp = allStu[i];
					allStu[i] = allStu[j];
					allStu[j] = temp;
				}
			}
		}
	}

}

Demo主类

package stuinfotwo;

import java.util.Scanner;
//import java.util.Arrays;
//System.out.println(Arrays.toString(ns))

/**
 * @author GGGXXC
 *
 */
public class Demo {
	public static void main(String[] args) {

		ArrayFunc arrFunc = new ArrayFunc();
		Student student = new Student("张0", 18, '男', 90F);

		arrFunc.insert(student);

		arrFunc.insert(new Student("张1", 26, '男', 91F));
		arrFunc.insert(new Student("张2", 26, '男', 92F));
		arrFunc.insert(new Student("张3", 26, '男', 93F));
		arrFunc.insert(new Student("张4", 26, '男', 94F));
		arrFunc.insert(new Student("张5", 26, '男', 95F));
		arrFunc.insert(new Student("张6", 26, '男', 96F));
		arrFunc.insert(new Student("张7", 26, '男', 97F));
		arrFunc.insert(new Student("张8", 26, '男', 98F));
		arrFunc.insert(new Student("张9", 26, '男', 99F));

		Scanner sc = new Scanner(System.in);
		int choice = -1;
		do {
			System.out.println();
			System.out.println("1.插入  2.查找  3.删除  4.排序  5.展示  6.退出");
			System.out.print("请输入你的操作:");
			choice = sc.nextInt();
			switch (choice) {
			case 1:
				System.out.println("插入");
				arrFunc.gene();
				break;
			case 2:
				System.out.println("查找");
				System.out.print("请输入你要查找的id:");
				int id = sc.nextInt();

				arrFunc.find(id);
				System.out.println("-----------------");

				break;
			case 3:
				System.out.println("删除");
				System.out.print("请输入你要删除的id:");
				id = sc.nextInt();
				arrFunc.del(id);
				arrFunc.show();
				break;
			case 4:
				System.out.println("排序");
				arrFunc.sort();// 这里加入allStu为什么报错?
				arrFunc.show();
				System.out.println("-----------------");
				break;
			case 5:
				System.out.println("展示");
				arrFunc.show();
				System.out.println("-----------------");
				break;
			case 6:
				System.out.println("退出");
				return;
			default:
				System.out.println("输入错误");
				break;
			}
		} while (choice != 6);

	}
}


1.插入  2.查找  3.删除  4.排序  5.展示  6.退出
请输入你的操作:5
展示
学生id:0,姓名:张0,年龄:18,性别:男,成绩:90.0
学生id:1,姓名:张1,年龄:26,性别:男,成绩:91.0
学生id:2,姓名:张2,年龄:26,性别:男,成绩:92.0
学生id:3,姓名:张3,年龄:26,性别:男,成绩:93.0
学生id:4,姓名:张4,年龄:26,性别:男,成绩:94.0
学生id:5,姓名:张5,年龄:26,性别:男,成绩:95.0
学生id:6,姓名:张6,年龄:26,性别:男,成绩:96.0
学生id:7,姓名:张7,年龄:26,性别:男,成绩:97.0
学生id:8,姓名:张8,年龄:26,性别:男,成绩:98.0
学生id:9,姓名:张9,年龄:26,性别:男,成绩:99.0
-----------------

1.插入  2.查找  3.删除  4.排序  5.展示  6.退出
请输入你的操作:1
插入
请输入姓名:
张15
请输入年龄:
15
请输入性别:
男
请输入分数:
12

1.插入  2.查找  3.删除  4.排序  5.展示  6.退出
请输入你的操作:5
展示
学生id:0,姓名:张0,年龄:18,性别:男,成绩:90.0
学生id:1,姓名:张1,年龄:26,性别:男,成绩:91.0
学生id:2,姓名:张2,年龄:26,性别:男,成绩:92.0
学生id:3,姓名:张3,年龄:26,性别:男,成绩:93.0
学生id:4,姓名:张4,年龄:26,性别:男,成绩:94.0
学生id:5,姓名:张5,年龄:26,性别:男,成绩:95.0
学生id:6,姓名:张6,年龄:26,性别:男,成绩:96.0
学生id:7,姓名:张7,年龄:26,性别:男,成绩:97.0
学生id:8,姓名:张8,年龄:26,性别:男,成绩:98.0
学生id:9,姓名:张9,年龄:26,性别:男,成绩:99.0
学生id:10,姓名:张15,年龄:15,性别:男,成绩:12.0
-----------------

1.插入  2.查找  3.删除  4.排序  5.展示  6.退出
请输入你的操作:4
排序
学生id:9,姓名:张9,年龄:26,性别:男,成绩:99.0
学生id:8,姓名:张8,年龄:26,性别:男,成绩:98.0
学生id:7,姓名:张7,年龄:26,性别:男,成绩:97.0
学生id:6,姓名:张6,年龄:26,性别:男,成绩:96.0
学生id:5,姓名:张5,年龄:26,性别:男,成绩:95.0
学生id:4,姓名:张4,年龄:26,性别:男,成绩:94.0
学生id:3,姓名:张3,年龄:26,性别:男,成绩:93.0
学生id:2,姓名:张2,年龄:26,性别:男,成绩:92.0
学生id:1,姓名:张1,年龄:26,性别:男,成绩:91.0
学生id:0,姓名:张0,年龄:18,性别:男,成绩:90.0
学生id:10,姓名:张15,年龄:15,性别:男,成绩:12.0
-----------------

1.插入  2.查找  3.删除  4.排序  5.展示  6.退出
请输入你的操作:6
退出

posted @ 2020-05-01 18:50  路漫漫兮其修远  阅读(377)  评论(0编辑  收藏  举报