面向对象案例-学生信息管理系统V1.1
1.学生类
package com.qfedu.student.entity; /**
* 学生类实体
*
* @author GGGXXC
*
*/
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;
} /**
*
* 先留着??????????????
* 使用System.out.println打印展示Student类对象时
* 是直接自动调用toString方法,展示该方法返回String字符串内容
*/ @Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", gender=" + gender + ", score=" + score
+ "]";
} }
StudentManager类
package com.qfedu.student.manager;
import java.util.Scanner;
import com.qfedu.student.entity.Student;
public class StudentManager {
/**
* 私有化学生数组,数组不能对外任意公开,有且只针对于当前管理类使用 提供了两种构造方法,有参,无参 因暂时不能确定数组容量大小,暂时将数组初始化为null
*/
private Student[] allStus = null;
/**
* 设置Student类的默认容量大小 类内调用 设为private属性 常量 设为final属性
*
* 后期可能会创建较多的StudentManager对象,如果不加static每个对象就都会创建这个变量 加上static后所有对象共享这一个
*
* private static final int DEFAULT_CAPACITY = 10;
*/
private static final int DEFAULT_CAPACITY = 10;
/**
* 设置数组最大容量值,
*
* private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
*/
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
/**
* 当前数组中学生个数 采用成员变量修饰:因为后期可能涉及多个对象,这个变量不能让所有对象共有
* 采用private修饰符修饰,因为只在类内操作,避免不必要的访问
*/
private int size = 0;
Scanner sc = new Scanner(System.in);
boolean flagSort = false;
/**
* 学生id计数变量
*/
private int count = 0;// 学生初始数量
/**
* 无参构造方法创建学生数组,如果用户未指定数组大小,则设为DEFAULT_CAPACITY
*/
public StudentManager() {
allStus = new Student[DEFAULT_CAPACITY];
}
/**
* 有参构造方法,用用户传入的数组设置数组初始大小 对用户输入的数据进行检查,如果用户输入的初始大小小于0,或者大于int的最大值减去8,则报错
*
* 现阶段针对异常先退出程序????????????????
*
* @param initCapacity
*/
public StudentManager(int initCapacity) {
if (initCapacity < 0 || initCapacity > DEFAULT_CAPACITY) {
/* 此处异常先退出程序 */
System.exit(0);
}
allStus = new Student[DEFAULT_CAPACITY];
}
// 创建增加学生方法add
// 权限修饰符采用public 方式 因为增加学生的操作要在类外进行
// 传入学生对象
// 返回值采用boolean类型,如果增加成功则返回true,否则返回false
public boolean add(Student student) {
// 在增加之前先判断数组大小
if (size == allStus.length) {
grow(size + 1);
}
student.setId(count + 1);
count += 1;
allStus[size] = student;
size += 1;
return true;
}
private void grow(int minCapacity) {
// 1.获取原数组的大小
int oldCapacity = allStus.length;
// 2.计算得到新数组的大小,按1.5倍计算
int newCapacity = oldCapacity + oldCapacity / 2;
// 3.判断新数组长度是否满足最小容量
if (minCapacity > newCapacity) {
newCapacity = minCapacity;
}
// 4.判断当前容量是否超出MAX_ARRAY_SIZE
// 超出的话目前先报异常
if (newCapacity > MAX_ARRAY_SIZE) {
System.exit(0);
}
// 5.创建新数组
Student[] temp = new Student[newCapacity];
// 6.数据拷贝
for (int i = 0; i < oldCapacity; i++) {
temp[i] = allStus[i];
}
// 7.使用新数组保存就数组
allStus = temp;
}
/**
* 根据id查找对应学生
*
* @param id 要查找的学生id
* @return 如果找到返回学生对象,否则返回null
*/
public Student get(int id) {
int index = findIndexById(id);
return index > 0 ? allStus[index] : null;
}
/**
* 类内私有方法,用于根据指定id查找对应下标位置
*
* @param id 指定的id号
* @return 返回对应下标,返回大于等于0表示已经找到,返回-1表示未找到
*/
private int findIndexById(int id) {
int temp = -1;
for (int i = 0; i < size; i++) {
if (id == allStus[i].getId()) {
temp = i;
break;
}
}
return temp;
}
/**
* 删除对应id的学生
*
* @param id要删除的学生id
* @return 删除成功返回true,否则返回false
*/
public boolean remove(int id) {
int index = findIndexById(id);
if (-1 == index) {
System.out.println("Not Found");
return false;
}
/*
* 如果id的值不是-1,则表示找到了需要删除的元素
*
*/
for (int i = index; i < size - 1; i++) {
allStus[i] = allStus[i + 1];
}
// 最后一个元素赋值为null
allStus[size - 1] = null;
// 有效元素-1
size -= 1;
return true;
}
/**
* 展示学生数组
*/
public void showAllStudents() {
for (int i = 0; i < size; i++) {
System.out.println(allStus[i]);
}
}
// 0509;
public void modify(int id) {
Student stu = get(id);
System.out.println("请输入要修改的内容:");
System.out.println("1.修改姓名 2.修改年龄 3.修改性别 4.修改分数 5.退出");
int choice = sc.nextInt();
sc.nextLine();// 是不是所有的nextInt处都要加?
while (choice != 5) {
switch (choice) {
case 1:
System.out.println("请输入学生姓名");
String name = sc.next();
stu.setName(name);
break;
case 2:
System.out.println("请输入学生年龄");
int age = sc.nextInt();
stu.setAge(age);
break;
case 3:
System.out.println("请输入学生性别");
char gender = sc.next().charAt(0);
stu.setGender(gender);
break;
case 4:
System.out.println("请输入学生分数");
float score = sc.nextFloat();
stu.setScore(score);
break;
case 5:
System.out.println("退出");
break;
default:
System.out.println("输入错误");
break;
}
System.out.println("请输入要修改的内容:");
System.out.println("1.修改姓名 2.修改年龄 3.修改性别 4.修改分数 5.退出");
choice = sc.nextInt();
}
}
/**
* 根据学生对象的成绩进行排序
*/
public void sortSelect() {
System.out.println("请选择: \n1.升序排序\t2.降序排序");
int choice = sc.nextInt();
switch (choice) {
case 1:
flagSort = true;
break;
case 2:
flagSort = false;
break;
}
sort(flagSort);
}
/**
* 排序算法
*
* @param flagSort true升序排序,false降序排序
*/
private void sort(boolean flagSort) {
Student[] stusTemp = new Student[size];
for (int i = 0; i < size; i++) {
stusTemp[i] = allStus[i];
}
for (int i = 0; i < size - 1; i++) {
int index = i;
for (int j = i + 1; j < size; j++) {
if (flagSort) {
if (stusTemp[index].getScore() > stusTemp[j].getScore()) {
index = j;
}
} else {
if (stusTemp[index].getScore() < stusTemp[j].getScore()) {
index = j;
}
}
}
if (i != index) {
Student temp = stusTemp[i];
stusTemp[i] = stusTemp[index];
stusTemp[index] = temp;
}
}
for (int i = 0; i < size; i++) {
System.out.println(stusTemp[i]);
}
}
/**
* 提示用户输入信息,生成学生对象
*/
public void gene() {
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);
add(stuTemp);
}
/**
* 展示菜单 简化主方法界面
*/
public void showMenu() {
Scanner sc = new Scanner(System.in);
int choice = -1;
do {
System.out.println();
System.out.println("1.插入学生 2.删除学生 3.修改学生 4.查找学生 5.分数排序 6.展示学生 7.退出");
System.out.print("请输入你的操作:");
choice = sc.nextInt();
switch (choice) {
case 1:
gene();
break;
case 2:
System.out.print("请输入你要删除的id:");
int id = sc.nextInt();
remove(id);
break;
case 3:
System.out.print("请输入你要修改的id:");
id = sc.nextInt();
modify(id);
break;
case 4:
System.out.print("请输入你要查找的id:");
id = sc.nextInt();
Student stu = get(id);
System.out.println(stu);
break;
case 5:
sortSelect();
break;
case 6:
showAllStudents();
break;
case 7:
System.out.println("退出");
sc.close();
return;
default:
System.out.println("输入错误");
break;
}
} while (choice != 7);
}
/**
* 初始化学生信息
*/
public void initial() {
Student student = new Student("张0", 18, '男', 90F);
add(student);
add(new Student("张1", 26, '男', 91F));
add(new Student("张2", 26, '男', 92F));
add(new Student("张3", 26, '男', 93F));
add(new Student("张4", 26, '男', 94F));
add(new Student("张5", 26, '男', 95F));
add(new Student("张6", 26, '男', 96F));
add(new Student("张7", 26, '男', 97F));
add(new Student("张8", 26, '男', 98F));
add(new Student("张9", 26, '男', 99F));
}
}
MainProject类
package com.qfedu.student.mainproject; import java.util.Scanner;
//import java.util.Arrays;
//System.out.println(Arrays.toString(ns)) import com.qfedu.student.entity.Student;
import com.qfedu.student.manager.StudentManager; /**
* @author GGGXXC
*
*/
public class MainProject {
public static void main(String[] args) { StudentManager stuMeth = new StudentManager(); stuMeth.initial(); stuMeth.showMenu(); }
}
面向对象案例-学生信息管理系统V1.1的更多相关文章
- 面向对象案例 - 学生信息管理系统V1.0
学生管理系统项目[所有知识点整合] 1. 学生管理系统项目 尝试完成以下功能 实体类: 学生类: id, 姓名,年龄,性别,成绩 需要使用数组保存学生信息 Student[] allStu 需要完成的 ...
- 面向对象案例-学生信息管理系统V0.6
更新版本 面向对象案例 - 学生信息管理系统V1.0 项目要求: 实体类: 学生类: id, 姓名,年龄,性别,成绩 需要使用数组保存学生信息 Student[] allStu 需要完成的方法 1. ...
- 学生信息管理系统v1.0
昨天一个教师朋友找到我,告诉我现在学期末他工作比较忙.需要统计处理很多学生信息,想让我帮他做一个管理系统.实现的功能就是把WPS表格转化成Word文档,将每一个学生的信息都能够分开,并且要根据名字找到 ...
- PHP案例:学生信息管理系统
-- Database: test -- 表的结构 message CREATE TABLE `message` ( `id` tinyint(1) NOT NULL PRIMARY KEY AUTO ...
- python 04 学生信息管理系统
今天任务不多,做了学生信息管理系统1.0,使用字典存储学生个体信息,列表存储学生字典.注意dict定义要在循环体内,若定义成全局变量或循环体外,则旧数据会被新数据覆盖.dict属于可变类型数据,内容改 ...
- Android(java)学习笔记195:学生信息管理系统案例(SQLite + ListView)
1.首先说明一个知识点,通常我们显示布局文件xml都是如下: setContentView(R.layout.activity_main): 其实每一个xml布局文件就好像一个气球,我们可以使用Vie ...
- Android(java)学习笔记188:学生信息管理系统案例(SQLite + ListView)
1.首先说明一个知识点,通常我们显示布局文件xml都是如下: setContentView(R.layout.activity_main): 其实每一个xml布局文件就好像一个气球,我们可以使用Vie ...
- Python基础案例练习:制作学生信息管理系统
一.前言 学生信息管理系统,相信大家或多或少都有做过 最近看很多学生作业都是制作一个学生信息管理系统 于是,今天带大家做一个简单的学生信息管理系统 二.开发环境: 我用到的开发环境 Python 3. ...
- Py学生信息管理系统 案例(优化版)
# 第一题:设计一个全局变量,来保存很多个学生信息:学生(学号, 姓名,年龄):思考要用怎样的结构来保存:# 第二题:在第一题基础上,完成:让用户输入一个新的学生信息(学号,姓名,年龄):你将其保存在 ...
随机推荐
- 网络流--最大流--hlpp(预流推进)模板
//500ms 秒掉洛谷推流问题 #include <algorithm> #include <iostream> #include <cstring> #incl ...
- 树上倍增法求LCA
我们找的是任意两个结点的最近公共祖先, 那么我们可以考虑这么两种种情况: 1.两结点的深度相同. 2.两结点深度不同. 第一步都要转化为情况1,这种可处理的情况. 先不考虑其他, 我们思考这么一个问题 ...
- unittest(生成测试报告)
1.先导入HTMLTestRunner模块 见上篇HTMLTestRunner模块生成文档 2.实例如下 (1)单用例文件执行且生成报告 import unittest import HTMLTest ...
- 一个epoll的简单例子
epoll事件机制的触发方式有两种:LT(电平触发)和ET(边沿触发) EPOLLIN事件: 内核中的socket接收缓冲区 为空(低电平) 内核中的socket接受缓冲区 不为空(高电平) EPOL ...
- Java——SSM整合所需的Maven配置文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- 前端——Vue.js学习总结一
一.什么是Vue.js 1.Vue.js 是目前最火的一个前端框架,React是最流行的一个前端框架 2.Vue.js 是前端的主流框架之一,和Angular.js.React.js 一起,并成为前端 ...
- Spring 循环引用(三)源码深入分析版
@ 目录 前言 正文 分析 doGetBean 为什么Prototype不可以 createBean doCreateBean getEarlyBeanReference getSingleton b ...
- FOC 电流采样方案对比(单电阻/双电阻/三电阻)
如果本文帮到了你,帮忙点个赞: 如果本文帮到了你,帮忙点个赞: 如果本文帮到了你,帮忙点个赞: 创作不易 谢谢支持 文章目录 1 电流采样的作用 2 硬件架构 3 采样关键 4 采样方案 5 三电阻采 ...
- Java并发编程实战 04死锁了怎么办?
Java并发编程文章系列 Java并发编程实战 01并发编程的Bug源头 Java并发编程实战 02Java如何解决可见性和有序性问题 Java并发编程实战 03互斥锁 解决原子性问题 前提 在第三篇 ...
- Power BI:社保、公积金增减
本月和上月比较,社保.公积金有增减,拓展开来,每两个相邻月份比较,社保.公积金有增减. 数据放在SQL Server Express,有公司.姓名.分类.个人缴费金额.单位缴费金额.年月等字段,uni ...