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的更多相关文章

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

    学生管理系统项目[所有知识点整合] 1. 学生管理系统项目 尝试完成以下功能 实体类: 学生类: id, 姓名,年龄,性别,成绩 需要使用数组保存学生信息 Student[] allStu 需要完成的 ...

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

    更新版本 面向对象案例 - 学生信息管理系统V1.0 项目要求: 实体类: 学生类: id, 姓名,年龄,性别,成绩 需要使用数组保存学生信息 Student[] allStu 需要完成的方法 1. ...

  3. 学生信息管理系统v1.0

    昨天一个教师朋友找到我,告诉我现在学期末他工作比较忙.需要统计处理很多学生信息,想让我帮他做一个管理系统.实现的功能就是把WPS表格转化成Word文档,将每一个学生的信息都能够分开,并且要根据名字找到 ...

  4. PHP案例:学生信息管理系统

    -- Database: test -- 表的结构 message CREATE TABLE `message` ( `id` tinyint(1) NOT NULL PRIMARY KEY AUTO ...

  5. python 04 学生信息管理系统

    今天任务不多,做了学生信息管理系统1.0,使用字典存储学生个体信息,列表存储学生字典.注意dict定义要在循环体内,若定义成全局变量或循环体外,则旧数据会被新数据覆盖.dict属于可变类型数据,内容改 ...

  6. Android(java)学习笔记195:学生信息管理系统案例(SQLite + ListView)

    1.首先说明一个知识点,通常我们显示布局文件xml都是如下: setContentView(R.layout.activity_main): 其实每一个xml布局文件就好像一个气球,我们可以使用Vie ...

  7. Android(java)学习笔记188:学生信息管理系统案例(SQLite + ListView)

    1.首先说明一个知识点,通常我们显示布局文件xml都是如下: setContentView(R.layout.activity_main): 其实每一个xml布局文件就好像一个气球,我们可以使用Vie ...

  8. Python基础案例练习:制作学生信息管理系统

    一.前言 学生信息管理系统,相信大家或多或少都有做过 最近看很多学生作业都是制作一个学生信息管理系统 于是,今天带大家做一个简单的学生信息管理系统 二.开发环境: 我用到的开发环境 Python 3. ...

  9. Py学生信息管理系统 案例(优化版)

    # 第一题:设计一个全局变量,来保存很多个学生信息:学生(学号, 姓名,年龄):思考要用怎样的结构来保存:# 第二题:在第一题基础上,完成:让用户输入一个新的学生信息(学号,姓名,年龄):你将其保存在 ...

随机推荐

  1. POJ 2136 Vertical Histogram(当时写的比较恶心,优化一下)

    Vertical Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21223 Accepted: 10048 ...

  2. 洛谷P3360偷天换日(树形DP)

    题目背景 神偷对艺术馆内的名画垂涎欲滴准备大捞一把. 题目描述 艺术馆由若干个展览厅和若干条走廊组成.每一条走廊的尽头不是通向一个展览厅,就 是分为两个走廊.每个展览厅内都有若干幅画,每副画都有一个价 ...

  3. 图论--最小生成树--Kruscal 模板

    #include<iostream> #include<queue> #include<algorithm> #include<set> #includ ...

  4. DP 60题 -2 HDU1025 Constructing Roads In JGShining's Kingdom

    Problem Description JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which ...

  5. ubuntu 15.04 的安装遇到的问题及其解决方法

    在Ubuntu15.04 的安装(U盘)中 遇到的问题1:安装后设置电脑从U盘启动,启动失败,屏幕上显示:Failed to load ldlinux.c32 解决方法:当时是参考这篇文章 http: ...

  6. Java——SSM整合所需的Maven配置文件

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  7. js 箭头函数不适用的场景

    箭头函数虽然方便但也不是每个地方都适用, 箭头函数在开发中可以十分方便的干预 this的指向,在一些情况下,是不需要对this的指向进行干预的,也就不适用箭头函数 1.构造函数的原型方法上 例如:Pe ...

  8. 网络流 + 欧拉回路 = B - Sightseeing tour POJ - 1637

    B - Sightseeing tour POJ - 1637 https://blog.csdn.net/qq_36551189/article/details/80905345 首先要了解一下欧拉 ...

  9. Day_13【IO流】扩展案例2_统计键盘录入字符在指定项目文件中出现的次数

    分析以下需求,并用代码实现 键盘录入一个字符(以字符串的形式录入) 判断当前字符在info3.txt当中是否存在 如果不存在, 给出提示 如果存在, 请统计出该字符出现的次数 Info3.txt内容如 ...

  10. CF#358 D. Alyona and Strings DP

    D. Alyona and Strings 题意 给出两个字符串s,t,让找出最长的k个在s,t不相交的公共子串. 思路 看了好几个题解才搞懂. 代码中有注释 代码 #include<bits/ ...