下面会分享我在做这个练习时的一些方法以及程序代码 供大家参考(最后附上完整的项目代码)。
首本人只是个初学Java的小白,可能项目中有许多地方使用不规范的以及代码的格式问题会引起各路大佬阅读的不适,还请 “ 手下留情 ” 🙏🙏🙏,当然欢迎大家指出问题所在,然后一起进步!!!
我把整个项目分成了Student .Java、Main.Java、FunctionModule.java、Extents.Java 四个部分
学生类
首先创建学生类和构造方法
代码如下:
package com.studentmodule;
/**
* @author wenjie
*/
public class Student {
private int stuId;
private int grade;
private String name;
private int age;
private String address;
public Student() {
}
public Student(int stuId, int grade, String name, int age, String address) {
this.stuId = stuId;
this.grade = grade;
this.name = name;
this.age = age;
this.address = address;
}
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
main方法
接下来就从 main 方法开始 “ 运行 ” 吧!
package com.studentmodule;
import java.awt.*;
import java.util.ArrayList;
import java.util.Scanner;
/**
* @author wenjie
*/
public class Main {
static final int COUNT = 2;
static final String MYSID = "admin";
static final String MYPASSWD = "666666";
public static void main(String[] args) throws AWTException {
interFace();
}
首界面登录
首先运行程序进入到以下首界面进行登录选择,选择 1 进入登录界面,选择 0 则退出程序,所有的选择程序都有错误判断,以下选择判断都一样,输入错误则提示重新输入。
代码如下:
public static void interFace() throws AWTException {
System.out.println(">首界面\n");
System.out.println("\t*****************************************************************");
System.out.println("\t 首界面 ");
System.out.println("\t ---------------------------------------------------------------");
System.out.println("\t 开始登录 请输入1 ");
System.out.println("\t ---------------------------------------------------------------");
System.out.println("\t 退出 请输入0 ");
System.out.println("\t*****************************************************************");
Scanner sc = new Scanner(System.in);
System.out.print("\n请输入您的选择: ");
while (true) {
int choose = sc.nextInt();
if (choose == 1) {
register();
break;
} else if (choose == 0) {
System.out.println("退出成功!");
System.exit(0);
} else {
System.out.print("看清选项! 再给你一次机会:");
}
}
}
通过键盘录入正确的用户名以及密码进行登录,你有三次输入的机会,如果三次都输入错误则程序自动退出。
代码如下:
public static void register() throws AWTException {
for (int i = COUNT; i >= 0; i--) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入您的用户名: ");
String loginSid = sc.nextLine();
System.out.print("请输入您的密码: ");
String loginPassWd = sc.nextLine();
if (loginSid.equals(MYSID) && loginPassWd.equals(MYPASSWD)) {
Extents.clearConsole();
System.out.println("欢迎登录! 用户:" + MYSID + "\n\n");
menu();
break;
} else {
if (i == 0) {
System.out.println("你是不是傻!");
System.exit(0);
}
System.out.println("错了错了, 你还有 " + i + " 次机会");
}
}
}
功能选择界面
输入正确后进入以下功能选择界面,输入对应的序号分别对学生信息进行(增、删、改、查)四个操作,另外输入 ‘ q ’ 则返回上级,输入 ’ p‘ 则退出程序。
代码如下:
public static void menu() throws AWTException {
ArrayList<Student> stu = new ArrayList<>();
while (true) {
System.out.println(">首界面>功能界面\n");
System.out.println("\t*****************************************************************");
System.out.println("\t 欢迎来到学生管理系统! ");
System.out.println("\t ---------------------------------------------------------------");
System.out.println("\t 1.添加学生信息 ");
System.out.println("\t ---------------------------------------------------------------");
System.out.println("\t 2.删除学生信息 ");
System.out.println("\t ---------------------------------------------------------------");
System.out.println("\t 3.修改学生信息 ");
System.out.println("\t ---------------------------------------------------------------");
System.out.println("\t 4.查看学生信息 ");
System.out.println("\t ---------------------------------------------------------------");
System.out.println("\t q:返回上级菜单 p:退出管理系统 ");
System.out.println("\t******************************************************************");
Scanner sc = new Scanner(System.in);
System.out.print("\n请输入您的选择:");
String choose = sc.nextLine();
switch (choose) {
case "1":
FunctionalBlock.addStu(stu);
break;
case "2":
Extents.clearConsole();
FunctionalBlock.deleteStu(stu);
break;
case "3":
Extents.clearConsole();
FunctionalBlock.updateStu(stu);
break;
case "4":
Extents.clearConsole();
FunctionalBlock.showStu(stu);
Extents.isShow(sc);
break;
case "q":
Extents.clearConsole();
interFace();
return;
case "p":
System.out.println("退出成功!");
System.exit(0);
default:
Extents.clearConsole();
System.out.println("这都错!看清选项再选\n\n\n");
break;
}
}
}
添加学生信息
如果还没有添加学生信息(学生数量为 0 )就进行删除、修改、查看等操作时就会提示 “ 数据为空,请添加数据后操作!” ,此时输入 1 进行添加学生,输入要添加的学生信息,输入完后会打印出来并询问是否添加,输入 1 则添加该学生,反之输入 0 则取消学生添加,添加一个学生信息后同样会打印出所有学生信息,最后可以选择继续添加或者返回上级。这里最后显示会后一个学号,因为学号每个学生都是唯一的,所以我让它每添加一个学生后进行自增 1 并不需要手动录入。
代码如下:
public static void addStu(ArrayList<Student> stu) throws AWTException {
while (true) {
Extents.clearConsole();
System.out.println(">首界面>功能界面>添加学生信息\n\n");
Scanner sc = new Scanner(System.in);
System.out.print("请输入学生班级(例:3): ");
int grade = sc.nextInt();
System.out.print("请输入学生姓名(例:张三): ");
String name = sc.next();
System.out.print("请输入学生年龄(例:18): ");
int age = sc.nextInt();
System.out.print("请输入学生地址(例:深圳): ");
String address = sc.next();
System.out.println("\n\n-----------------------------------------------------");
System.out.println("班级: " + grade + "\t\t姓名: " + name + "\t\t年龄: " + age + "\t\t地址: " + address);
System.out.print("\n\n是否添加该学生信息? [Yes(1) / No(0)] :");
Extents.isAdd(stu, sc, grade, name, age, address);
System.out.println("\n\n\n>首界面>功能界面>添加学生信息\n");
System.out.println("\t 继续添加 请输入1 ");
System.out.println();
System.out.println("\t 返回上级 请输入0 ");
System.out.println("\t ---------------------------------------------------------------");
System.out.print("\n请输入您的选择:");
while (true) {
int choose = sc.nextInt();
if (choose == 1) {
break;
} else if (choose == 0) {
Extents.clearConsole();
return;
} else {
System.out.print("看清选项! 再给你一次机会: ");
}
}
}
}
public static void isAdd(ArrayList<Student> stu, Scanner sc, int grade, String name, int age, String address) throws AWTException {
while (true) {
int is = sc.nextInt();
if (is == 0) {
Extents.clearConsole();
System.out.println("取消成功!");
break;
} else if (is == 1) {
Student s = new Student(stuId, grade, name, age, address);
stu.add(s);
stuId += 1;
Extents.clearConsole();
System.out.println("添加信息成功!\n\n");
FunctionalBlock.showStu(stu);
break;
}
System.out.print("\n输入错误!请重新输入:");
}
}
删除学生信息
添加了学生信息后我们输入 0 返回上级菜单然后选择 2 进行删除学生操作,首先会打印出所有学生信息,然后输入你想要删除的学生信息的学号,如果该学号不存在会进行提示并重新输入,最后确认是否删除该学生信息,选择 1 删除,选择 0 则取消删除,删除后会再次打印操作后的所有学生信息,会发现此时学生数量由 3 变成了 2 ,以及刚删除的信息则不见了,你可以选择继续删除以及返回上级菜单。
代码如下:
public static void deleteStu(ArrayList<Student> stu) throws AWTException {
Scanner sc = new Scanner(System.in);
showStu(stu);
while (true) {
System.out.print("\n请输入要删除的学生学号:");
int sid = sc.nextInt();
sc.nextLine();
int flag = Extents.getFlag(stu, sid);
if (flag == -1) {
System.out.print("\n该学号不存在,请重新输入\n");
} else {
System.out.print("\n是否删除学号为:" + sid + " 的学生信息? [Yes(1) / No(0)] :");
Extents.isDlete(stu, sc, flag);
System.out.println("\n\n\n>首界面>功能界面>删除学生信息\n");
System.out.println("\t 继续删除 请输入1 ");
System.out.println();
System.out.println("\t 返回上级 请输入0 ");
System.out.println("\t ----------------------------------------------------------------");
System.out.print("\n请输入您的选择: ");
while (true) {
int choose = sc.nextInt();
if (choose == 1) {
break;
} else if (choose == 0) {
Extents.clearConsole();
return;
} else {
System.out.print("看清选项! 再给你一次机会: ");
}
}
}
}
}
public static void isDlete(ArrayList<Student> stu, Scanner sc, int flag) throws AWTException {
while (true) {
int is = sc.nextInt();
if (is == 0) {
Extents.clearConsole();
System.out.println("取消成功!");
break;
} else if (is == 1) {
stu.remove(flag);
Extents.clearConsole();
System.out.println("删除学生信息成功!\n\n");
FunctionalBlock.showStu(stu);
break;
}
System.out.print("\n输入错误!请重新输入:");
}
}
修改学生信息
删除完学号为 2 的学生信息后我们输入 0 返回上级菜单,然后输入 3 进行修改操作,同样先打印出所有学生信息,选择你想要修改的学生信息学号,如果学号不存在会进行提示并重新输入,然后根据提示依次输入要修改的学生信息,输入完后会打印出修改前和修改后的学生信息让你进行对比,最后输入 1 修改输入 0 则取消修改,修改完后再次打印所有学生的信息进行查看确认。
代码如下:
public static void updateStu(ArrayList<Student> stu) throws AWTException {
Scanner sc = new Scanner(System.in);
while (true) {
showStu(stu);
System.out.print("\n\n请输入要修改信息的学生学号:");
int sidUpdate = sc.nextInt();
int flag = Extents.getFlag(stu, sidUpdate);
Extents.clearConsole();
if (flag == -1) {
System.out.print("该学号不存在,请重新输入\n\n\n ");
} else {
System.out.println(">首界面>功能界面>修改学生信息\n\n");
System.out.print("请输入学生班级: ");
int grade = sc.nextInt();
System.out.print("请输入学生姓名: ");
String name = sc.next();
System.out.print("请输入学生年龄: ");
int age = sc.nextInt();
System.out.print("请输入学生地址: ");
String address = sc.next();
Extents.clearConsole();
Extents.getFlag(stu, sidUpdate);
Student s1 = stu.get(flag);
System.out.println(">首界面>功能界面>修改学生信息");
System.out.println("\n\n------------------------------------------------------------------");
System.out.println("修改前——>\n");
System.out.println("学号:" + s1.getStuId() + "\t\t班级: " + s1.getGrade() + "\t\t姓名: "
+ s1.getName() + "\t\t年龄: " + s1.getAge() + "\t\t地址: " + s1.getAddress());
System.out.println("\n\n------------------------------------------------------------------");
System.out.println("修改后——>\n");
System.out.println("学号:" + sidUpdate + "\t\t班级: " + grade + "\t\t姓名: " + name + "\t\t年龄: "
+ age + "\t\t地址: " + address);
System.out.print("\n\n是否修改该学生信息? [Yes(1) / No(0)] :");
Extents.isUpdata(stu, sc, sidUpdate, flag, grade, name, age, address);
System.out.println("\n\n\n>首界面>功能界面>修改学生信息\n");
System.out.println("\t 继续修改 请输入1 ");
System.out.println();
System.out.println("\t 返回上级 请输入0 ");
System.out.println("\t --------------------------------------------------------------");
System.out.print("\n请输入您的选择:");
while (true) {
int choose = sc.nextInt();
if (choose == 1) {
Extents.clearConsole();
break;
} else if (choose == 0) {
Extents.clearConsole();
return;
} else {
System.out.print("看清选项! 再给你一次机会: ");
}
}
}
}
}
public static void isUpdata(ArrayList<Student> stu, Scanner sc, int sidUpdate, int flag, int grade, String name, int age, String address) throws AWTException {
while (true) {
int is = sc.nextInt();
if (is == 0) {
Extents.clearConsole();
System.out.println("取消成功!");
break;
} else if (is == 1) {
Student newStu = new Student(sidUpdate, grade, name, age, address);
stu.set(flag, newStu);
Extents.clearConsole();
System.out.println("修改学生信息成功!\n\n");
FunctionalBlock.showStu(stu);
break;
}
System.out.print("\n输入错误!请重新输入:");
}
}
查看学生信息
最后就是我们的查看学生了,其实在之前的添加、删除、修改中都有频繁的调用查找功能来打印出所有学生信息,但还是讲一下吧,输入 0 返回上级菜单后再输入 4 进行查看所有学生信息,输入 0 就可以返回上级菜单了,最后可以输入 ’ q ‘ 退出程序。
代码如下:
public static void showStu(ArrayList<Student> stu) throws AWTException {
if (stu.size() == 0) {
Extents.clearConsole();
System.out.println("数据为空,请添加数据后操作!\n\n");
Main.menu();
} else {
System.out.println(">学生信息显示\n");
System.out.println("学生个数:"+stu.size() + "人\n");
System.out.println("\t ----------------------------------------------------------------");
System.out.println("\t 学号\t\t" + " 班级\t\t" + " \t姓名\t" + "\t\t年龄" + " \t\t\t地址");
System.out.println("\t --------------------------------------------------------------");
/*for (int i = 0; i < stu.size(); i++) {
Student s = stu.get(i);*/
for (Student s : stu) {
System.out.println("\t\t" + s.getStuId() + "\t\t\t" + s.getGrade() + " \t\t\t" + s.getName()
+ " \t\t\t" + s.getAge() + " \t\t\t" + s.getAddress() + "\n");
System.out.println("\t --------------------------------------------------------------");
}
}
}
public static void isShow(Scanner sc) throws AWTException {
System.out.println("\n\n\n>首界面>功能界面>查看学生信息\n\n");
System.out.println("\t 返回上级 请输入0 ");
System.out.println("\t ---------------------------------------------------------------");
System.out.print("\n请输入您的选择: ");
while (true) {
int choose1 = sc.nextInt();
if (choose1 == 0) {
Extents.clearConsole();
break;
} else {
System.out.print("看清选项! 再给你一次机会: ");
}
}
}
学号遍历 和 清空控制台方法
下面这两个方法一个是用来进行学号遍历的,找到对应学生的学号获得该学生信息然后对该学号的学生信息进行操作
还有一个就是清空控制台的方法,这个方法在我上一篇文章中有详细的说明,想了解的可以点击该链接进行查看
链接:
public static int getFlag(ArrayList<Student> stu, int sid) {
int flag = -1;
for (int i = 0; i < stu.size(); i++) {
Student s = stu.get(i);
if (s.getStuId() == sid) {
flag = i;
break;
}
}
return flag;
}
public static void clearConsole() throws AWTException {
Robot r = new Robot();
// 按下Ctrl键
r.keyPress(KeyEvent.VK_CONTROL);
// 按下R键
r.keyPress(KeyEvent.VK_R);
// 释放R键
r.keyRelease(KeyEvent.VK_R);
// 释放Ctrl键
r.keyRelease(KeyEvent.VK_CONTROL);
r.delay(50);
}
好了,程序运行到这里也差不多完了,所有的功能都有基本介绍一遍,就是基本的 ( 增、删、改、查 ) 四个操作。
附:学生管理系统代码
最后附上完整的学生管理系统项目的代码
Student .Java
package com.studentmodule;
/**
* @author wenjie
*/
public class Student {
private int stuId;
private int grade;
private String name;
private int age;
private String address;
public Student() {
}
public Student(int stuId, int grade, String name, int age, String address) {
this.stuId = stuId;
this.grade = grade;
this.name = name;
this.age = age;
this.address = address;
}
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Main.Java
package com.studentmodule;
import java.awt.*;
import java.util.ArrayList;
import java.util.Scanner;
/**
* @author wenjie
*/
public class Main {
static final int COUNT = 2;
static final String MYSID = "admin";
static final String MYPASSWD = "666666";
public static void main(String[] args) throws AWTException {
interFace();
}
public static void interFace() throws AWTException {
System.out.println(">首界面\n");
System.out.println("\t*****************************************************************");
System.out.println("\t 首界面 ");
System.out.println("\t ---------------------------------------------------------------");
System.out.println("\t 开始登录 请输入1 ");
System.out.println("\t ---------------------------------------------------------------");
System.out.println("\t 退出 请输入0 ");
System.out.println("\t*****************************************************************");
Scanner sc = new Scanner(System.in);
System.out.print("\n请输入您的选择: ");
while (true) {
int choose = sc.nextInt();
if (choose == 1) {
register();
break;
} else if (choose == 0) {
System.out.println("退出成功!");
System.exit(0);
} else {
System.out.print("看清选项! 再给你一次机会:");
}
}
}
public static void register() throws AWTException {
for (int i = COUNT; i >= 0; i--) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入您的用户名: ");
String loginSid = sc.nextLine();
System.out.print("请输入您的密码: ");
String loginPassWd = sc.nextLine();
if (loginSid.equals(MYSID) && loginPassWd.equals(MYPASSWD)) {
Extents.clearConsole();
System.out.println("欢迎登录! 用户:" + MYSID + "\n\n");
menu();
break;
} else {
if (i == 0) {
System.out.println("你是不是傻!");
System.exit(0);
}
System.out.println("错了错了, 你还有 " + i + " 次机会");
}
}
}
public static void menu() throws AWTException {
ArrayList<Student> stu = new ArrayList<>();
while (true) {
System.out.println(">首界面>功能界面\n");
System.out.println("\t*****************************************************************");
System.out.println("\t 欢迎来到学生管理系统! ");
System.out.println("\t ---------------------------------------------------------------");
System.out.println("\t 1.添加学生信息 ");
System.out.println("\t ---------------------------------------------------------------");
System.out.println("\t 2.删除学生信息 ");
System.out.println("\t ---------------------------------------------------------------");
System.out.println("\t 3.修改学生信息 ");
System.out.println("\t ---------------------------------------------------------------");
System.out.println("\t 4.查看学生信息 ");
System.out.println("\t ---------------------------------------------------------------");
System.out.println("\t q:返回上级菜单 p:退出管理系统 ");
System.out.println("\t******************************************************************");
Scanner sc = new Scanner(System.in);
System.out.print("\n请输入您的选择:");
String choose = sc.nextLine();
switch (choose) {
case "1":
FunctionalBlock.addStu(stu);
break;
case "2":
Extents.clearConsole();
FunctionalBlock.deleteStu(stu);
break;
case "3":
Extents.clearConsole();
FunctionalBlock.updateStu(stu);
break;
case "4":
Extents.clearConsole();
FunctionalBlock.showStu(stu);
Extents.isShow(sc);
break;
case "q":
Extents.clearConsole();
interFace();
return;
case "p":
System.out.println("退出成功!");
System.exit(0);
default:
Extents.clearConsole();
System.out.println("这都错!看清选项再选\n\n\n");
break;
}
}
}
}
FunctionModule.java
package com.studentmodule;
import java.awt.*;
import java.util.ArrayList;
import java.util.Scanner;
/**
* @author wenjie
*/
public class FunctionalBlock {
public static void addStu(ArrayList<Student> stu) throws AWTException {
while (true) {
Extents.clearConsole();
System.out.println(">首界面>功能界面>添加学生信息\n\n");
Scanner sc = new Scanner(System.in);
System.out.print("请输入学生班级(例:3): ");
int grade = sc.nextInt();
System.out.print("请输入学生姓名(例:张三): ");
String name = sc.next();
System.out.print("请输入学生年龄(例:18): ");
int age = sc.nextInt();
System.out.print("请输入学生地址(例:深圳): ");
String address = sc.next();
System.out.println("\n\n-----------------------------------------------------");
System.out.println("班级: " + grade + "\t\t姓名: " + name + "\t\t年龄: " + age + "\t\t地址: " + address);
System.out.print("\n\n是否添加该学生信息? [Yes(1) / No(0)] :");
Extents.isAdd(stu, sc, grade, name, age, address);
System.out.println("\n\n\n>首界面>功能界面>添加学生信息\n");
System.out.println("\t 继续添加 请输入1 ");
System.out.println();
System.out.println("\t 返回上级 请输入0 ");
System.out.println("\t ---------------------------------------------------------------");
System.out.print("\n请输入您的选择:");
while (true) {
int choose = sc.nextInt();
if (choose == 1) {
break;
} else if (choose == 0) {
Extents.clearConsole();
return;
} else {
System.out.print("看清选项! 再给你一次机会: ");
}
}
}
}
public static void deleteStu(ArrayList<Student> stu) throws AWTException {
Scanner sc = new Scanner(System.in);
showStu(stu);
while (true) {
System.out.print("\n请输入要删除的学生学号:");
int sid = sc.nextInt();
sc.nextLine();
int flag = Extents.getFlag(stu, sid);
if (flag == -1) {
System.out.print("\n该学号不存在,请重新输入\n");
} else {
System.out.print("\n是否删除学号为:" + sid + " 的学生信息? [Yes(1) / No(0)] :");
Extents.isDlete(stu, sc, flag);
System.out.println("\n\n\n>首界面>功能界面>删除学生信息\n");
System.out.println("\t 继续删除 请输入1 ");
System.out.println();
System.out.println("\t 返回上级 请输入0 ");
System.out.println("\t ----------------------------------------------------------------");
System.out.print("\n请输入您的选择: ");
while (true) {
int choose = sc.nextInt();
if (choose == 1) {
break;
} else if (choose == 0) {
Extents.clearConsole();
return;
} else {
System.out.print("看清选项! 再给你一次机会: ");
}
}
}
}
}
public static void updateStu(ArrayList<Student> stu) throws AWTException {
Scanner sc = new Scanner(System.in);
while (true) {
showStu(stu);
System.out.print("\n\n请输入要修改信息的学生学号:");
int sidUpdate = sc.nextInt();
int flag = Extents.getFlag(stu, sidUpdate);
Extents.clearConsole();
if (flag == -1) {
System.out.print("该学号不存在,请重新输入\n\n\n ");
} else {
System.out.println(">首界面>功能界面>修改学生信息\n\n");
System.out.print("请输入学生班级: ");
int grade = sc.nextInt();
System.out.print("请输入学生姓名: ");
String name = sc.next();
System.out.print("请输入学生年龄: ");
int age = sc.nextInt();
System.out.print("请输入学生地址: ");
String address = sc.next();
Extents.clearConsole();
Extents.getFlag(stu, sidUpdate);
Student s1 = stu.get(flag);
System.out.println(">首界面>功能界面>修改学生信息");
System.out.println("\n\n------------------------------------------------------------------");
System.out.println("修改前——>\n");
System.out.println("学号:" + s1.getStuId() + "\t\t班级: " + s1.getGrade() + "\t\t姓名: "
+ s1.getName() + "\t\t年龄: " + s1.getAge() + "\t\t地址: " + s1.getAddress());
System.out.println("\n\n------------------------------------------------------------------");
System.out.println("修改后——>\n");
System.out.println("学号:" + sidUpdate + "\t\t班级: " + grade + "\t\t姓名: " + name + "\t\t年龄: "
+ age + "\t\t地址: " + address);
System.out.print("\n\n是否修改该学生信息? [Yes(1) / No(0)] :");
Extents.isUpdata(stu, sc, sidUpdate, flag, grade, name, age, address);
System.out.println("\n\n\n>首界面>功能界面>修改学生信息\n");
System.out.println("\t 继续修改 请输入1 ");
System.out.println();
System.out.println("\t 返回上级 请输入0 ");
System.out.println("\t --------------------------------------------------------------");
System.out.print("\n请输入您的选择:");
while (true) {
int choose = sc.nextInt();
if (choose == 1) {
Extents.clearConsole();
break;
} else if (choose == 0) {
Extents.clearConsole();
return;
} else {
System.out.print("看清选项! 再给你一次机会: ");
}
}
}
}
}
public static void showStu(ArrayList<Student> stu) throws AWTException {
if (stu.size() == 0) {
Extents.clearConsole();
System.out.println("数据为空,请添加数据后操作!\n\n");
Main.menu();
} else {
System.out.println(">学生信息显示\n");
System.out.println("学生个数:"+stu.size() + "人\n");
System.out.println("\t ----------------------------------------------------------------");
System.out.println("\t 学号\t\t" + " 班级\t\t" + " \t姓名\t" + "\t\t年龄" + " \t\t\t地址");
System.out.println("\t --------------------------------------------------------------");
/*for (int i = 0; i < stu.size(); i++) {
Student s = stu.get(i);*/
for (Student s : stu) {
System.out.println("\t\t" + s.getStuId() + "\t\t\t" + s.getGrade() + " \t\t\t" + s.getName()
+ " \t\t\t" + s.getAge() + " \t\t\t" + s.getAddress() + "\n");
System.out.println("\t --------------------------------------------------------------");
}
}
}
}
Extents.Java
package com.studentmodule;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Scanner;
/**
* @author wenjie
*/
public class Extents {
public static int stuId = 1;
public static void isAdd(ArrayList<Student> stu, Scanner sc, int grade, String name, int age, String address) throws AWTException {
while (true) {
int is = sc.nextInt();
if (is == 0) {
Extents.clearConsole();
System.out.println("取消成功!");
break;
} else if (is == 1) {
Student s = new Student(stuId, grade, name, age, address);
stu.add(s);
stuId += 1;
Extents.clearConsole();
System.out.println("添加信息成功!\n\n");
FunctionalBlock.showStu(stu);
break;
}
System.out.print("\n输入错误!请重新输入:");
}
}
public static void isDlete(ArrayList<Student> stu, Scanner sc, int flag) throws AWTException {
while (true) {
int is = sc.nextInt();
if (is == 0) {
Extents.clearConsole();
System.out.println("取消成功!");
break;
} else if (is == 1) {
stu.remove(flag);
Extents.clearConsole();
System.out.println("删除学生信息成功!\n\n");
FunctionalBlock.showStu(stu);
break;
}
System.out.print("\n输入错误!请重新输入:");
}
}
public static void isUpdata(ArrayList<Student> stu, Scanner sc, int sidUpdate, int flag, int grade, String name, int age, String address) throws AWTException {
while (true) {
int is = sc.nextInt();
if (is == 0) {
Extents.clearConsole();
System.out.println("取消成功!");
break;
} else if (is == 1) {
Student newStu = new Student(sidUpdate, grade, name, age, address);
stu.set(flag, newStu);
Extents.clearConsole();
System.out.println("修改学生信息成功!\n\n");
FunctionalBlock.showStu(stu);
break;
}
System.out.print("\n输入错误!请重新输入:");
}
}
public static void isShow(Scanner sc) throws AWTException {
System.out.println("\n\n\n>首界面>功能界面>查看学生信息\n\n");
System.out.println("\t 返回上级 请输入0 ");
System.out.println("\t ---------------------------------------------------------------");
System.out.print("\n请输入您的选择: ");
while (true) {
int choose1 = sc.nextInt();
if (choose1 == 0) {
Extents.clearConsole();
break;
} else {
System.out.print("看清选项! 再给你一次机会: ");
}
}
}
public static int getFlag(ArrayList<Student> stu, int sid) {
int flag = -1;
for (int i = 0; i < stu.size(); i++) {
Student s = stu.get(i);
if (s.getStuId() == sid) {
flag = i;
break;
}
}
return flag;
}
public static void clearConsole() throws AWTException {
Robot r = new Robot();
// 按下Ctrl键
r.keyPress(KeyEvent.VK_CONTROL);
// 按下R键
r.keyPress(KeyEvent.VK_R);
// 释放R键
r.keyRelease(KeyEvent.VK_R);
// 释放Ctrl键
r.keyRelease(KeyEvent.VK_CONTROL);
r.delay(50);
}
}
总结
以上就是整个项目的所有的代码了,因为大都是些 Java 的基本语法,选择结构和循环结构组成的所以就没有加什么注释了,相信学了 Java 前面的一些基本语法后应该都能看出个大概,剩下的可以交给百度。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/69954.html