com.qf.project.mnue
主页面:
package com.qf.project.mnue;
import java.math.MathContext;
import java.util.Random;
import java.util.Scanner;
import com.qf.project.Service.StudentService;
import com.qf.project.StudentSystem.Student;
public class StudentMnue {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
StudentService ss = new StudentService();
// for (int i = 0; i < 5; i++) {
// String name = "阿杰"+i;
// int age = new Random().nextInt(30);
// char sex = '男';
// String className = "11";
// double javaScore = Math.random()*100;
// double htmlScore = Math.random()*100;
// double pythonScore = Math.random()*100;
// Student stu = new Student(name,age,sex,className,javaScore,htmlScore,pythonScore);
// ss.addStudent(stu);
// }
int nums=0;
do{
System.out.println("欢迎来到学院管理系统");
System.out.println("1.查看所有学员");
System.out.println("2.新增学员");
System.out.println("3.查询指定ID的学员信息");
System.out.println("4.根据ID删除学员");
System.out.println("5.根据ID修改学员信息");
System.out.println("6.根据班级名称查询学员");
System.out.println("7.根据姓名(姓/名)查询学员");
System.out.println("8.根据需求排序学员信息");
System.out.println("9.根据需求展示符合条件的学员信息");
System.out.println("0.退出");
System.out.println("请选择:");
nums = sc.nextInt();
switch (nums) {
case 1:
ss.show();
break;
case 2:
ss.addStudent();
break;
case 3:
ss.getStuByID();
break;
case 4:
ss.removeStuByID();
break;
case 5:
ss.modifyByID();
break;
case 6:
ss.getStuByClassName();
break;
case 7:
ss.lastName();
break;
case 8:
ss.stuSort();
break;
case 9:
ss.stuSortPlus();
break;
case 0:
System.out.println("资源正在保存。。。");
ss.output();
return;
default:
break;
}
}while(true);
}
}
com.qf.project.Service
服务区:
package com.qf.project.Service;
import java.util.Scanner;
import com.qf.project.StudentSystem.Student;
import com.qf.project.manager.StudentManager;
public class StudentService {
private StudentManager sdm = null;
Scanner sc = new Scanner(System.in);
public StudentService(){
sdm = new StudentManager();
}
/**
* 查询所有学生对象
*/
public void show(){
sdm.getAll();
}
/**
* 信息初始化
* @param stu
*/
public void addStudent(Student stu){
sdm.add(stu);
}
/**
* 注册学生信息
* 让用户存入信息,并判断信息
*/
public void addStudent(){
System.out.println("请输入姓名:");
String name = sc.next();
int age;
do{
System.out.println("请输入年龄:");
age = sc.nextInt();
}while(age 123);
char sex;
do{
System.out.println("请输入性别:");
sex = sc.next().charAt(0);
}while(sex != '男' && sex != '女');
System.out.println("请输入班级号:");
String className = sc.next();
System.out.println("请输入java成绩(若没有输入0):");
double javaScore = sc.nextDouble();
System.out.println("请输入html成绩(若没有输入0):");
double htmlScore = sc.nextDouble();
System.out.println("请输入python成绩(若没有输入0):");
double pythonScore = sc.nextDouble();
Student stu = new Student(name,age,sex,className,javaScore,htmlScore,pythonScore);
boolean boo = sdm.add(stu);
if(boo == true){
System.out.println("存入成功");
System.out.println("您存入的学生信息为:" + stu);
}
else{
System.out.println("存入失败");
}
}
/**
* 通过学生的学号来查询学生的信息
*/
public void getStuByID(){
int id = checkID();
Student o = sdm.get(id);
if(o != null){
System.out.println("学生信息为:");
System.out.println(o);
}else{
System.out.println("没用此学号的学生");
}
}
/**
* 通过学号来删除学生
*/
public void removeStuByID(){
int id = checkID();
Student stu = sdm.remove(id);
if(stu != null){
System.out.println("删除成功");
System.out.println("被删除的学生为" + stu);
}else{
System.out.println("删除失败");
}
}
/**
* 通过学号来修改学生信息
*/
public void modifyByID(){
int id = checkID();
Student stu = sdm.get(id);
if(stu != null){
int nums;
do{
System.out.println("学生信息为 " + stu);
System.out.println("请输入想要修改的操作:");
System.out.println("1.修改学生姓名 "+ stu.getName());
System.out.println("2.修改学生年龄 "+ stu.getAge());
System.out.println("3.修改学生性别 "+ stu.getSex());
System.out.println("4.修改学生班级名 "+ stu.getClassName());
System.out.println("5.修改学生java成绩 "+ stu.getJavaScore());
System.out.println("6.修改学生html成绩 "+ stu.getHtmlScore());
System.out.println("7.修改学生python成绩 "+ stu.getPythonScore());
System.out.println("0.退出");
nums = sc.nextInt();
switch (nums) {
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 sex = sc.next().charAt(0);
stu.setSex(sex);
break;
case 4:
System.out.println("请输入想修改的班级名");
String className = sc.next();
stu.setClassName(className);
break;
case 5:
System.out.println("请输入想修改的成绩");
double javaScore = sc.nextDouble();
stu.setJavaScore(javaScore);
break;
case 6:
System.out.println("请输入想修改的成绩");
double htmlScore = sc.nextDouble();
stu.setHtmlScore(htmlScore);
break;
case 7:
System.out.println("请输入想修改的成绩");
double pythonScore = sc.nextDouble();
stu.setPythonScore(pythonScore);
break;
case 0:
return;
default:
break;
}
}while(true);
}
}
/**
* 通过班级名来查找学生
*/
public void getStuByClassName(){
System.out.println("请输入班级名");
String className = sc.next();
sdm.get(className);
}
/**
* 通过姓或名字查找学生
*/
public void lastName(){
int nums;
do{
System.out.println("请选择查询的方式:");
System.out.println("1.姓 2.名 0.退出");
nums = sc.nextInt();
switch (nums) {
case 1:
System.out.println("请输入要查询的姓:");
String x = sc.next();
sdm.getLastName(x);
break;
case 2:
System.out.println("请输入要查询的名:");
String m = sc.next();
sdm.getGivenName(m);
break;
case 0:
return;
default:
break;
}
}while(true);
}
/**
* 通过一些条件来排序
*/
public void stuSort(){
int nums;
do{
System.out.println("请输入排序的条件");
System.out.println("1.id 2.年龄 3.总成绩 0.退出");
nums = sc.nextInt();
switch(nums){
case 1:
sdm.sorted((stu1,stu2)->stu1.getId()-stu2.getId());
break;
case 2:
sdm.sorted((stu1,stu2)->stu1.getAge()-stu2.getAge());
break;
case 3:
sdm.sorted((stu1,stu2)->(int)((stu1.getSumScore())*100-(stu2.getSumScore())*100));
break;
case 0:
return;
default :
break;
}
}while(true);
}
/**
* 排序加强版
*/
public void stuSortPlus(){
int nums;
do{
System.out.println("请输入排序的规则:");
System.out.println("1.年龄大于15 2.总分大于150 0.退出");
nums = sc.nextInt();
switch(nums){
case 1:
sdm.sortPlus((e)->e.getAge()>15);
break;
case 2:
sdm.sortPlus((e)->e.getSumScore()>150);
break;
case 0:
return;
default:
break;
}
}while(true);
}
public void output() throws Exception{
sdm.outPutStream();
}
/**
* 验证输入的id保证正确性
* @return
*/
public int checkID(){
int id = 0;
do{
System.out.println("请输入学生id:");
id = sc.nextInt();
if(id <= 0){
System.out.println("请输入正确的学号");
}
}while(id <= 0);
return id;
}
}
com.qf.project.manager:
执行命令的区:
package com.qf.project.util.impl;
import java.util.Arrays;
import com.qf.project.util.MyList;
public class MyArrayList implements MyList{
private Object[] elementData = null;
int size = 0;
private final static int DEFAULT_CAPACITY = 10;
public MyArrayList(){
elementData = new Object[10];
}
public MyArrayList(int init){
if(init > 0 && init > 1)+1;
if(newElementData >= Integer.MAX_VALUE - 8){
throw new OutOfMemoryError("集合溢出了");
}
elementData = Arrays.copyOf(elementData, newElementData);
}
/**
* 1.先进行有效范围的判断
* 2.循环,将下标index和之后的值依次往后移
* 3.将元素e赋值到下标index上
* 4.有效元素个数加1
* 5.返回结果
*/
@Override
public boolean add(int index, Object e) {
checkValue(index);
for(int i = size ; i > index;i--){
elementData[i] = elementData[i-1];
}
elementData[index] = e;
size++;
return true;
}
/**
* 判断index是否在有效范围内,如果不在,因异常而终止
* @param index
*/
public void checkValue(int index){
if(index >= size || index < 0){
throw new ArrayIndexOutOfBoundsException(index);
}
}
/**
* 1.首先判断是否有元素o,如果有的话记录下标,如果没有,返回null
* 2.将元素移除,后面元素依次向前
*/
@Override
public E remove(Object o) {
if(o == null){
return null;
}else{
for (int i = 0; i < size; i++) {
if(elementData[i].equals(o)){
for (int j = i; j < size; j++) {
elementData[j] = elementData[j+1];
}
size--;
return (E) o;
}else{
// return null;
}
}
}
return null;
}
/**
* 1.首先判断index值是否符合规范,不符合,因异常中断
* 2.通过循环覆盖下标index的值
*/
@Override
public E remove(int index) {
checkValue(index);
Object o = elementData[index];
for (int i = index; i < size-1; i++) {
elementData[i] = elementData[i+1];
}
elementData[size-1] = null;
size--;
return (E)o;
}
/**
* 1.首先判断index值是否符合规范,不符合,因异常中断
* 2.将下标index赋值为e;
*/
@Override
public Object set(int index, Object e) {
checkValue(index);
Object o = elementData[index];
elementData[index] = e;
return o;
}
@Override
public int size() {
return this.size;
}
/**
* 1.复制elementData数组
* 2.返回新数组
*/
@Override
public Object[] toArray() {
Object[] os = Arrays.copyOf(elementData, size);
return os;
}
/**
* 当有效元素为0时,就为空
*/
@Override
public boolean isEmpty() {
if(size == 0){
return true;
}
return false;
}
/**
* 1.判断o 是不是null;
* 2.循环看数组中是否有o
* 3.有返回true 没有,返回false
*/
@Override
public boolean contains(Object o) {
if(o == null){
return false;
}
for (int i = 0; i < size; i++) {
if(elementData[i].equals(o)){
return true;
}
}
return false;
}
@Override
public MyList subMyList(int start, int end) {
Object[] o = new Object[end - start];
System.arraycopy(elementData, start, o, start, end - start);
return null;
}
/**
* 循环遍历,找到就返回,没有返回-1
*/
@Override
public int indexOf(Object o) {
for (int i = 0; i 0; i--) {
if(elementData[i].equals(o)){
return i;
}
}
return -1;
}
@Override
public E get(int index) {
return (E)elementData[index];
}
}
com.qf.project.util
工具包:
package com.qf.project.util;
/**
* 增加:
* add(E e)
* add(int index , E e)
* 删除:
* remove(E e)
* remove(int index)
* 修改:
* set(int index,E e);
* 获取
* E get(int index);
* int size;
* Object[] toArray;
* boolean isEmpty;
* boolean comtains(Object o);
* MyArrayList subMyList(int start,int end);
* int indexOf(Object o);
* int lostIndexOf(Object o);
*
* get(int index)
* @author L
*
*/
public interface MyList {
/**
* 将元素插入到尾部
* @param e 元素的泛型
* @return 返回true 成功,返回false失败
*/
boolean add(E e);
/**
*将元素插入到指定的位置
* @param index 插入的下标
* @param e 元素的泛型
* @return 返回true 成功,返回false失败
*/
boolean add(int index,E e);
/**
* 删除一个指定元素
* @param o 指定的元素
* @return 将删除的元素返回
*/
E remove(Object o);
/**
* 删除指定下标的元素
* @param index 元素的下标
* @return 返回删除的元素
*/
E remove(int index);
/**
* 将指定下标的元素更换新的元素
* @param index 指定下标
* @param e 新的元素
* @return 更换的元素
*/
E set(int index,E e);
/**
* 有效元素
* @return
*/
int size();
/**
* 返回一个Object类型的数组
* @return
*/
Object[] toArray();
/**
* 如果这是一个空的集合,返回true,如果不是,返回false
* @return
*/
boolean isEmpty();
/**
* 查找元素是否存在
* @param o 传入的元素
* @return 如果存在返回true ,不存在,返回false
*/
boolean contains(Object o);
/**
* 传入一个起始下标和一个终止下标,得到一个MyList 的子集合MyArrayList;
* @param start 起始下标,要求大于等于0,小于end
* @param end 终止下标,要求小于有效元素,大于start
* @return 返回一个MyList的子集合MyArrayList
*/
MyList subMyList(int start , int end);
/**
* 通过元素得到该元素的第一个下标
* @param o 元素
* @return 下标
*/
int indexOf(Object o);
/**
* 通过元素获得该元素最后一个下标
* @param o 元素
* @return 下标
*/
int lastIndexOf(Object o);
/**
* 通过下标返回该下表的元素
* @param index 下标
* @return 元素
*/
E get(int index);
}
package com.qf.project.util;
/**
* 断言形接口返回的boolean类型作为判断
* @author L
*
* @param
*/
public interface MyFilter {
boolean accept(E e);
}
package com.qf.project.util;
/**
* 排序接口,两个值进行比较,返回正数为升序,返回负数为降序,返回0,不变
* @author L
*
* @param
*/
public interface MyCompartor {
int compar(E e1,E e2);
}
com.qf.project.util.impl
工具实现类
package com.qf.project.util.impl;
import java.util.Arrays;
import com.qf.project.util.MyList;
public class MyArrayList implements MyList{
private Object[] elementData = null;
int size = 0;
private final static int DEFAULT_CAPACITY = 10;
public MyArrayList(){
elementData = new Object[10];
}
public MyArrayList(int init){
if(init > 0 && init > 1)+1;
if(newElementData >= Integer.MAX_VALUE - 8){
throw new OutOfMemoryError("集合溢出了");
}
elementData = Arrays.copyOf(elementData, newElementData);
}
/**
* 1.先进行有效范围的判断
* 2.循环,将下标index和之后的值依次往后移
* 3.将元素e赋值到下标index上
* 4.有效元素个数加1
* 5.返回结果
*/
@Override
public boolean add(int index, Object e) {
checkValue(index);
for(int i = size ; i > index;i--){
elementData[i] = elementData[i-1];
}
elementData[index] = e;
size++;
return true;
}
/**
* 判断index是否在有效范围内,如果不在,因异常而终止
* @param index
*/
public void checkValue(int index){
if(index >= size || index < 0){
throw new ArrayIndexOutOfBoundsException(index);
}
}
/**
* 1.首先判断是否有元素o,如果有的话记录下标,如果没有,返回null
* 2.将元素移除,后面元素依次向前
*/
@Override
public E remove(Object o) {
if(o == null){
return null;
}else{
for (int i = 0; i < size; i++) {
if(elementData[i].equals(o)){
for (int j = i; j < size; j++) {
elementData[j] = elementData[j+1];
}
size--;
return (E) o;
}else{
// return null;
}
}
}
return null;
}
/**
* 1.首先判断index值是否符合规范,不符合,因异常中断
* 2.通过循环覆盖下标index的值
*/
@Override
public E remove(int index) {
checkValue(index);
Object o = elementData[index];
for (int i = index; i < size-1; i++) {
elementData[i] = elementData[i+1];
}
elementData[size-1] = null;
size--;
return (E)o;
}
/**
* 1.首先判断index值是否符合规范,不符合,因异常中断
* 2.将下标index赋值为e;
*/
@Override
public Object set(int index, Object e) {
checkValue(index);
Object o = elementData[index];
elementData[index] = e;
return o;
}
@Override
public int size() {
return this.size;
}
/**
* 1.复制elementData数组
* 2.返回新数组
*/
@Override
public Object[] toArray() {
Object[] os = Arrays.copyOf(elementData, size);
return os;
}
/**
* 当有效元素为0时,就为空
*/
@Override
public boolean isEmpty() {
if(size == 0){
return true;
}
return false;
}
/**
* 1.判断o 是不是null;
* 2.循环看数组中是否有o
* 3.有返回true 没有,返回false
*/
@Override
public boolean contains(Object o) {
if(o == null){
return false;
}
for (int i = 0; i < size; i++) {
if(elementData[i].equals(o)){
return true;
}
}
return false;
}
@Override
public MyList subMyList(int start, int end) {
Object[] o = new Object[end - start];
System.arraycopy(elementData, start, o, start, end - start);
return null;
}
/**
* 循环遍历,找到就返回,没有返回-1
*/
@Override
public int indexOf(Object o) {
for (int i = 0; i 0; i--) {
if(elementData[i].equals(o)){
return i;
}
}
return -1;
}
@Override
public E get(int index) {
return (E)elementData[index];
}
}
com.qf.project.StudentSystem
对象类:
package com.qf.project.StudentSystem;
import java.io.Serializable;
public class Student implements Serializable{
/**
* 私有属性:学号、姓名、年龄、性别、班级号、java成绩、html成绩、python成绩、总成绩
*/
private int id;
private String name;
private int age;
private char sex;
private String className;
private double javaScore;
private double htmlScore;
private double pythonScore;
private double sumScore;
// static int count = 0;//作为学号,每增加一个count+1;
public static int count = 0;
/**
* 无参构造方法
*/
public Student(){}
/**
* 有参构造方法:基本信息
* @param id
* @param name
* @param age
* @param sex
* @param className
*/
public Student(String name,int age,char sex,String className){
count++;
this.id = count;
this.name = name;
this.age = age;
this.sex = sex;
this.className = className;
}
/**
* 有参构造方法:全部信息
* @param id
* @param name
* @param age
* @param sex
* @param className
* @param javaScore
* @param htmlScore
* @param pythonScore
*/
public Student(String name,int age,char sex,String className,double javaScore,double htmlScore,
double pythonScore){
this(name,age,sex,className);
this.javaScore = javaScore;
this.htmlScore = htmlScore;
this.pythonScore = pythonScore;
this.sumScore = this.javaScore + this.htmlScore + this.pythonScore;
}
/**
* id只需要获取,不需要修改,每人一个
* @return
*/
public int getId() {
return 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 getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public double getJavaScore() {
return javaScore;
}
/**
* 1.先拿到除却java成绩的总成绩
* 2.修改java成绩
* 3.将java成绩加到总成绩中去
* @param javaScore
*/
public void setJavaScore(double javaScore) {
this.sumScore = sumScore - this.javaScore;
this.javaScore = javaScore;
this.sumScore = sumScore + this.javaScore;
}
public double getHtmlScore() {
return htmlScore;
}
/**
* 1.先拿到除却html成绩的总成绩
* 2.修改html成绩
* 3.将html成绩加到总成绩中去
* @param htmlScore
*/
public void setHtmlScore(double htmlScore) {
this.sumScore = sumScore - this.htmlScore;
this.htmlScore = htmlScore;
this.sumScore = sumScore + this.htmlScore;
}
public double getPythonScore() {
return pythonScore;
}
/**
* 1.先拿到除却python成绩的总成绩
* 2.修改python成绩
* 3.将python成绩加到总成绩中去
* @param pythonScore
*/
public void setPythonScore(double pythonScore) {
this.sumScore = sumScore - this.pythonScore;
this.pythonScore = pythonScore;
this.sumScore = sumScore + this.pythonScore;
}
public double getSumScore() {
return sumScore;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", className=" + className
+ ", javaScore=" + javaScore + ", htmlScore=" + htmlScore + ", pythonScore=" + pythonScore
+ ", sumScore=" + sumScore + "]";
}
}
作者:S9264L