鉴于初学者都在用学生管理系统练习业务逻辑,本人也提供一个java写的面向对象版本
技术能力有限,仅供新手学习参考。大神略过。
+++++++++学生类+++++++++++++
package studentmanagementsystem;
/*
学生类:
属性:姓名、年龄、学号(ID)、性别
方法:复写toString()、equals()、compareto()、get()set()等
*/
import java.util.Objects;
public class Student implements Comparable {
private String name;
private int age;
private int ID;
private String sex;
boolean flag=false;//退出判断年龄的循环标记
public Student() {
}
public Student(String name, int age, int ID, String sex) {
this.name = name;
this.age = age;
this.ID = ID;
this.sex = sex;
}
@Override
public String toString() {
return "学生"+"姓名:"+name+"\t\t"+"年龄:"+age+"\t\t"+"学号:"+ID+"\t\t"+"性别:"+sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int ages) {
if (ages > 0 && ages < 80) {
this.age = ages;
flag=true;
} else {
System.out.println("输入年龄错误");
}
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
if (sex.equals("男") || sex .equals("女") ) {
this.sex = sex;
flag=true;
} else {
System.out.println("输入错误");
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass())
return false;
Student student = (Student) o;
return age == student.age &&
ID == student.ID &&
flag == student.flag &&
Objects.equals(name, student.name) &&
Objects.equals(sex, student.sex);
}
@Override
public int hashCode() {
return Objects.hash(ID);
}
@Override
public int compareTo(Object o) {
Student st = (Student) o;
return this.getID()-((Student) o).getID();
}
}
学生容器类
package studentmanagementsystem;
import java.util.*;
public class StuSystem {
//容器类
private Scanner sc = new Scanner(System.in);
ArrayList list = new ArrayList();
//添加学生
private Student st;
public void addstudent (Student st){
//判断id重复
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getID() == st.getID()) {
System.out.println("已注册过该账号");
}
}
list.add(st);
System.out.println("注册成功+++");
}
//设置
public void setInformation(int temp) {
a:
while (true) {
System.out.println("正在查询");//加入sleep等待
if (list.size()==0) {
System.out.println("没有找到该学生");
break a;
}
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getID()==temp) {
System.out.println(list.get(i).toString());
list.remove(i);
list.add(i,getConsoleString());
//把封装好的对象直接给了
System.out.println("\t\t" + "设置成功");
break a;
}
}
if (true) {
System.out.println("没有该学生,请重新输入");
break;
}
}
}
public void referAll() {
for (int i = 0; i < list.size(); i++) {
Collections.sort(list);
System.out.println(list.get(i).toString());
}
}
public void referStudent(int id) {
a:
while (true) {
System.out.println("正在查询");//加入sleep等待
if (list.size()==0) {
System.out.println("没有找到该学生");
break a;
}
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getID()==id) {
System.out.println(list.get(i).toString());
break a;
}
}
if (true) {
System.out.println("没有该学生,请重新输入");
break;
}
}
}
public void removeInformation(int id) {
a:
while (true) {
System.out.println("正在查询");//可加入sleep等待
if (list.size()==0) {
System.out.println("没有找到该学生");
break a;
}
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getID()==id) {
System.out.println(list.get(i).toString());
System.out.println("是否删除,y/n");
String temp=sc.next();
if (temp.equals("y")) {
list.remove(i);
} else if (temp.equals("n")) {
break a;
}
//把封装好的对象直接给了
System.out.println("\t\t" + "删除成功");
break a;
}
}
if (true) {
System.out.println("没有该学生,请重新输入");
break;
}
}
}
/*
抽取控制台
*/
public Student getConsoleString(){
//定义学生对象储存信息
Student xin = new Student();//定义新的对象,来接收新的对象信息
System.out.println("==================================================");
System.out.println("请输入学生信息");
System.out.println("请输入姓名");
try {
String name = sc.next();
xin.setName(name);
} catch (Exception e) {
System.out.println("输入错误");
}
while (true) {
System.out.println("请输入年龄");
try {
int age =sc.nextInt();
xin.setAge(age);
} catch (Exception e) {
System.out.println("输入错误");
break ;
}
if (xin.flag==true) {
break;
}
}
xin.flag=false;
System.out.println("请输入学号");
try {
int id =sc.nextInt();
xin.setID(id);
} catch (Exception e) {
System.out.println("输入错误");
int id =sc.nextInt();
}
while (true) {
System.out.println("请输入性别");
String sex = sc.next();
xin.setSex(sex);
if (xin.flag==true) {
break;
}
}
System.out.println("=================================================");
return xin;
}
}
主方法界面及调用
package studentmanagementsystem;
import javax.sound.midi.Soundbank;
import java.util.*;
/**
* @auther sunguangyuan
*@version 1.0
*
1.添加 2.修改 3.删除 4.按学号查询 5.查询所有学员 6.退出");
2一共三个类,学生类 、系统类(容器类)、主方法类(界面类)
3界面信息也应该封装起来,并暴露接口,为后期拓展方便
*/
public class StudentManageStudent {
public static void main(String[] args) {
StuSystem tank = new StuSystem();
Scanner sc = new Scanner(System.in);
title:
while (true) {
System.out.println("请输入要执行的功能");
System.out.println("1.添加 2.修改 3.删除 4.按学号查询 5.查询所有学员 6.退出");
try {
int temp = sc.nextInt();
switch (temp) {
case 1:
//添加 此处封装性不好
if (tank.list.size() == 0) {
tank.list.add(tank.getConsoleString());
System.out.println("注册成功");
break;
} else if (tank.list.size() >= 1) {
tank.addstudent(tank.getConsoleString());
break;
}
break;
case 2:
//修改 :根据学号确定要设置的对象
System.out.println("请输入你的学号");
int setid = sc.nextInt();
tank.setInformation(setid);
break;
case 3:
//删除
System.out.println("请输入您的学号");
int removeid = sc.nextInt();
tank.removeInformation(removeid);
break;
case 4:
//查询
System.out.println("请输入您的学号");
int referid = sc.nextInt();
tank.referStudent(referid);
break;
case 5:
System.out.println("========================");
System.out.println("========================");
//查询所有学生
tank.referAll();
System.out.println("========================");
System.out.println("========================");
break;
case 6:
//退出
System.out.println("欢迎使用,再见");
System.exit(0);
default:
System.out.println("¥¥¥输入有误,请重新输入¥¥¥");
break;
}
} catch (Exception e) {
System.out.println("输入有误");
break title;
}
}
}
}
功能:常见业务的增删改查,以及根据学号的排序功能等
已知bug: 由于为新手练习用,没有引入io,针对Scanner的异常仅仅是简单的捕获
未作处理,主方法体的title标签为仅为退出用
功能拓展:主界面还应该提供接口,拓展功能
主界面封装不够良好,还可以优化
容器类的arrylist属性应该封装起来,提高安全性(时间紧,没来及改良)
还有名字字符数不等会出现对齐错乱
关于这个主界面的封装和接口的暴露如果有大神浏览,恳请指教,谢谢