JavaSE实现学生管理系统
一.错误的方式(面向过程开发)
1.实体类(Student)
public class Student {
private int stuID;
private String stuName;
private String sex;
private double JavaScore;
private double PythonScore;
public Student() {
}
public Student(int stuID, String stuName, String sex, double javaScore, double pythonScore) {
this.stuID = stuID;
this.stuName = stuName;
this.sex = sex;
JavaScore = javaScore;
PythonScore = pythonScore;
}
public int getStuID() {
return stuID;
}
public void setStuID(int stuID) {
this.stuID = stuID;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public double getJavaScore() {
return JavaScore;
}
public void setJavaScore(double javaScore) {
JavaScore = javaScore;
}
public double getPythonScore() {
return PythonScore;
}
public void setPythonScore(double pythonScore) {
PythonScore = pythonScore;
}
@Override
public String toString() {
return "学生的信息为:\n" +
"学生学号:" + stuID +
"\t学生姓名:" + stuName +
"\t学生性别:" + sex +
"\n学生Java成绩:" + JavaScore +
"\t学生Python成绩:" + PythonScore +
"\t学生总成绩:" + (JavaScore+PythonScore) +
"\t学生平均成绩:" + ((JavaScore+PythonScore)/2) + "\n";
}
}
2.主方法类
import com.sai.pojo.Student;
import java.util.ArrayList;
import java.util.Scanner;
public class DoMain {
public static void main(String[] args) {
ArrayList<Student> studentArrayList = new ArrayList<>();
Student stu1 = new Student(1,"小明","男",45,40);
Student stu2 = new Student(2,"小亮","男",55,75);
Student stu3 = new Student(3,"小红","女",88,90);
Student stu4 = new Student(4,"张三","男",80,55);
studentArrayList.add(stu1);
studentArrayList.add(stu2);
studentArrayList.add(stu3);
studentArrayList.add(stu4);
menu();
doMenu(studentArrayList);
}
//菜单函数
public static void menu(){
System.out.println("=======================================欢迎来到学生管理系统=======================================");
System.out.println("===1.查询全部学生的信息===");
System.out.println("===2.添加学生的信息===");
System.out.println("===3.删除学生信息===");
System.out.println("===4.修改学生信息===");
System.out.println("===5.根据学生ID查询学生信息===");
System.out.println("===6.退出学生管理系统===");
System.out.println("==============================================================================================\n\n");
}
//执行菜单操作
public static void doMenu(ArrayList<Student> list){
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("请输入您要选择的操作(1~6):");
while (true) {
Scanner scannerCase = new Scanner(System.in);
int i = scannerCase.nextInt();
switch (i) {
case 1:
selectAllStu(list);
break;
case 2:
addStu(list);
break;
case 3:
delStu(list);
break;
case 4:
updateStu(list);
break;
case 5:
selectStuByStuID(list);
break;
case 6:
System.out.println("感谢您的使用~我们下次再见!");
System.exit(0);
break;
}
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
menu();
}
}
//查询全部学生信息
public static void selectAllStu(ArrayList<Student> list){
for (Student student:list){
System.out.println(student.toString());
}
}
//根据学生id查询学生信息
public static void selectStuByStuID(ArrayList<Student> list){
Scanner stuID = new Scanner(System.in);
System.out.println("输入要查询的学生ID:");
int id = stuID.nextInt();
for (Student student:list){
if (student.getStuID() == id){
System.out.println("查询成功,查询的学生信息如下:");
System.out.println(student.toString());
}else {
System.out.println("不存在该学号的学生!!!");
}
}
}
//添加学生信息的方法,传入的值是学生数组
public static void addStu(ArrayList<Student> list){
Student stu = new Student();
Scanner scan = new Scanner(System.in);
System.out.println("输入学生的学号:");
stu.setStuID(scan.nextInt());
System.out.println("输入学生的姓名:");
stu.setStuName(scan.next());
System.out.println("输入学生的性别:");
stu.setSex(scan.next());
System.out.println("输入学生的Java成绩:");
stu.setJavaScore(scan.nextDouble());
System.out.println("输入学生的Python成绩:");
stu.setPythonScore(scan.nextDouble());
list.add(stu);
}
//删除学生信息
public static void delStu(ArrayList<Student> list){
System.out.println("请输入您要删除的学生的id:");
Scanner scanner = new Scanner(System.in);
int id = scanner.nextInt();//要删除的学生的ID
for (int i = 0;i < list.size();i++){//循环遍历Student类型的数组的下标
Student del = list.get(i);//得到要删除的 以Student为类型 的学生 的下标
if (del.getStuID() == id){
list.remove(del);
System.out.println("删除学生成功");
}else {
System.out.println("不存在该学号的学生!!!");
}
}
}
//修改学生的信息
//修改时也应该进行选择:修改哪一项
public static void updateStu(ArrayList<Student> list){
System.out.println("输入要修改的学生的ID:");
Scanner scanner = new Scanner(System.in);
int id = scanner.nextInt();
for (int i = 0;i < list.size();i++){
Student update = list.get(i);
if (update.getStuID() == id){
System.out.println("已找到该学生,该学生的信息为:");
System.out.println(update.toString());
//用switch实现精确修改
System.out.println("输入要修改的信息:id/name/sex/javaScore/pythonScore:");
Scanner exact = new Scanner(System.in);
String updateData = exact.next();
switch (updateData){
case "id":
System.out.println("输入新的ID:");
Scanner newID = new Scanner(System.in);
int changeId = newID.nextInt();
update.setStuID(changeId);
break;
case "name":
System.out.println("输入新的stuName:");
Scanner newName = new Scanner(System.in);
String changeName = newName.next();
update.setStuName(changeName);
break;
case "sex":
System.out.println("输入新的性别");
Scanner newSex = new Scanner(System.in);
String changeSex = newSex.next();
update.setSex(changeSex);
break;
case "javaScore":
System.out.println("输入要新的Java成绩:");
Scanner newJavaScore = new Scanner(System.in);
double javaScore = newJavaScore.nextDouble();
update.setJavaScore(javaScore);
break;
case "pythonScore":
System.out.println("输入要新的Python成绩:");
Scanner newPythonScore = new Scanner(System.in);
double pythonScore = newPythonScore.nextDouble();
update.setPythonScore(pythonScore);
break;
}
}else {
System.out.println("不存在该学号的学生!!!");
}
}
}
}
二.使用接口
1.实体类
与面向过程相同
2.主方法类
import com.sai.dao.StuDaoImpl;
import com.sai.pojo.Student;
import com.sai.util.MenuUtil;
import java.util.ArrayList;
public class MyTest {
//主方法类
public static void main(String[] args) {
//创建students的数组测试
ArrayList<Student> studentArrayList = new ArrayList<>();
// Student stu1 = new Student(1,"小明","男",45,40);
// Student stu2 = new Student(2,"小亮","男",55,75);
// Student stu3 = new Student(3,"小红","女",88,90);
// Student stu4 = new Student(4,"张三","男",80,55);
// studentArrayList.add(stu1);
// studentArrayList.add(stu2);
// studentArrayList.add(stu3);
// studentArrayList.add(stu4);
MenuUtil myMenu = new MenuUtil();
myMenu.menu();
myMenu.doMenu(studentArrayList);
}
}
在测试的时候用到了StuDaoImpl类
3.dao层
3.1StuDao接口
import com.sai.pojo.Student;
import java.util.ArrayList;
public interface StuDao {
//接口
//查询全部学生
public void selAllStu(ArrayList<Student> list);
//根据id查询学生
public void selStuByID(ArrayList<Student> list);
//添加学生信息的方法,传入的值是学生数组
public void addStu(ArrayList<Student> list);
//删除学生信息
public void delStu(ArrayList<Student> list);
//修改学生的信息
public void updateStu(ArrayList<Student> list);
//检查数组是否为空
public boolean isEmpty(ArrayList<Student> list);
//遍历循环学生数组,返回学生id
public int isExist(ArrayList<Student> list, int IEid);
}
3.2StuDaoImpl类实现接口中的方法
import com.sai.pojo.Student;
import java.util.ArrayList;
import java.util.Scanner;
public class StuDaoImpl implements StuDao{
//查询全部学生
@Override
public void selAllStu(ArrayList<Student> list) {
if (isEmpty(list)){
System.out.println("暂无学生信息,请先添加!!!");
return;
}else {
for (Student student:list){
System.out.println(student.toString());
}
}
}
//根据id查询学生
public void selStuByID(ArrayList<Student> list){
if (isEmpty(list)){
System.out.println("暂无学生信息,请先添加!!!");
return;
}
Scanner stuID = new Scanner(System.in);
System.out.println("输入要查询的学生ID:");
int id = stuID.nextInt();
Student student = new Student();
if (isExist(list,id) == -1){
System.out.println("不存在该学号的学生!!!");
}else{
System.out.println("查询成功,查询的学生信息如下:");
System.out.println(student.toString());
}
}
//添加学生信息的方法,传入的值是学生数组
public void addStu(ArrayList<Student> list){
Student stu = new Student();
Scanner scan = new Scanner(System.in);
System.out.println("输入学生的学号:");
stu.setStuID(scan.nextInt());
System.out.println("输入学生的姓名:");
stu.setStuName(scan.next());
System.out.println("输入学生的性别:");
stu.setSex(scan.next());
System.out.println("输入学生的Java成绩:");
stu.setJavaScore(scan.nextDouble());
System.out.println("输入学生的Python成绩:");
stu.setPythonScore(scan.nextDouble());
list.add(stu);
}
//删除学生信息
public void delStu(ArrayList<Student> list){
System.out.println("请输入您要删除的学生的id:");
Scanner scanner = new Scanner(System.in);
int id = scanner.nextInt();//要删除的学生的ID
if (isExist(list,id) == -1){
System.out.println("不存在该学号的学生!!!");
}else{
Student del = list.get(isExist(list,id));//得到要删除的 以Student为类型 的学生 的下标
list.remove(del);
System.out.println("删除学生成功");
}
}
//修改学生的信息
//修改时也应该进行选择:修改哪一项
public void updateStu(ArrayList<Student> list){
System.out.println("输入要修改的学生的ID:");
Scanner scanner = new Scanner(System.in);
int id = scanner.nextInt();
if (isExist(list,id) == -1){
System.out.println("不存在该学号的学生!!!");
}else{
Student update = list.get(isExist(list,id));
System.out.println("已找到该学生,该学生的信息为:");
System.out.println(update.toString());
//用switch实现精确修改
System.out.println("输入要修改的信息:id/name/sex/javaScore/pythonScore:");
Scanner exact = new Scanner(System.in);
String updateData = exact.next();
switch (updateData){
case "id":
System.out.println("输入新的ID:");
Scanner newID = new Scanner(System.in);
int changeId = newID.nextInt();
update.setStuID(changeId);
break;
case "name":
System.out.println("输入新的stuName:");
Scanner newName = new Scanner(System.in);
String changeName = newName.next();
update.setStuName(changeName);
break;
case "sex":
System.out.println("输入新的性别");
Scanner newSex = new Scanner(System.in);
String changeSex = newSex.next();
update.setSex(changeSex);
break;
case "javaScore":
System.out.println("输入要新的Java成绩:");
Scanner newJavaScore = new Scanner(System.in);
double javaScore = newJavaScore.nextDouble();
update.setJavaScore(javaScore);
break;
case "pythonScore":
System.out.println("输入要新的Python成绩:");
Scanner newPythonScore = new Scanner(System.in);
double pythonScore = newPythonScore.nextDouble();
update.setPythonScore(pythonScore);
break;
}
}
}
//检查数组是否为空
public boolean isEmpty(ArrayList<Student> list){
if (list.size() == 0){
return true;
}else {
return false;
}
}
//循环便利学生数组,有效防止多次输出不存在该学生
public int isExist(ArrayList<Student> list, int IEid){
int index = -1;
for (int i = 0; i < list.size(); i++){
Student student = list.get(i);
if (student.getStuID() == IEid){
index = i;
break;
}
}
if (index == -1){
return -1;
}else {
return index;
}
}
}
4.MenuUtil类实现菜单
import com.sai.dao.StuDaoImpl;
import com.sai.pojo.Student;
import java.util.ArrayList;
import java.util.Scanner;
public class MenuUtil { //菜单函数
public void menu(){
System.out.println("=======================================欢迎来到学生管理系统=======================================");
System.out.println("===1.查询全部学生的信息===");
System.out.println("===2.添加学生的信息===");
System.out.println("===3.删除学生信息===");
System.out.println("===4.修改学生信息===");
System.out.println("===5.根据学生ID查询学生信息===");
System.out.println("===6.退出学生管理系统===");
System.out.println("==============================================================================================\n\n");
}
//执行菜单操作
public void doMenu(ArrayList<Student> list){
StuDaoImpl stuDao = new StuDaoImpl();
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("请输入您要选择的操作(1~6):");
while (true) {
Scanner scannerCase = new Scanner(System.in);
int i = scannerCase.nextInt();
switch (i) {
case 1:
stuDao.selAllStu(list);
break;
case 2:
stuDao.addStu(list);
break;
case 3:
stuDao.delStu(list);
break;
case 4:
stuDao.updateStu(list);
break;
case 5:
stuDao.selStuByID(list);
break;
case 6:
System.out.println("感谢您的使用~我们下次再见!");
System.exit(0);
break;
}
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
menu();
}
}
}
三.过程中学习到的东西
1.接口(网上搜集的)
接口和抽象类的区别:
接口与抽象类的比较:
关键字不同:接口的关键字为“interface”,抽象类的关键字为“abstract”
方法形式不同:接口中的方法只能是没有带方法体的抽象方法,抽象类中的方法既可以是抽象方法,也可以是带有方法体的普通方法;
属性不同:接口中的属性只能是常量(列如:接口中定义的 int i=0;),抽象方法中的属性没有限制;
使用不同:当各子类都存在一个共同的方法特征,但各自的具体实现又不太一样时,常使用接口。
抽象类实现接口时,接口中的抽象方法可以全部实现,也可以部分实现,甚至都不实现,并且这些未被实现的接口方法不需要在抽象类中重新显式声明为抽象方法,但是抽象类的子类必须全部实现抽象类中没有实现的抽象方法,以及来自接口而未在抽象类中实现的方法。