首先先创建一个student类
public class Student {
private String name;
private String sno;
private String gender;
private int age;
private int score;
public Student ( String name, String sno, String gender, int age, int score) {
super ( ) ;
this . name = name;
this . sno = sno;
this . gender = gender;
this . age = age;
this . score = score;
}
public Student ( ) {
}
@Override
public String toString ( ) {
return "Student [name=" + name + ", sno=" + sno + ", gender=" + gender + ", age=" + age + ", score=" + score
+ "]" ;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public String getSno ( ) {
return sno;
}
public void setSno ( String sno) {
this . sno = sno;
}
public String getGender ( ) {
return gender;
}
public void setGender ( String gender) {
this . gender = gender;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
public int getScore ( ) {
return score;
}
public void setScore ( int score) {
this . score = score;
}
}
然后设一个管理员的类,以便验证登录使用,将其当做一个泛型放入一个集合当中使用
import java. util. * ;
public class Admin {
private String adminName;
private String adminpassword;
public Admin ( String adminName, String adminpassword) {
super ( ) ;
this . adminName = adminName;
this . adminpassword = adminpassword;
}
public String getAdminName ( ) {
return adminName;
}
public void setAdminName ( String adminName) {
this . adminName = adminName;
}
public String getAdminpassword ( ) {
return adminpassword;
}
public void setAdminpassword ( String adminpassword) {
this . adminpassword = adminpassword;
}
}
集体的实现,逻辑可以自己创一个包,然后建两个类,然后把下面的代码放进去,大概的逻辑都有,不懂的可以留言哦!
import java. util. * ;
public class Student_Main {
static List< Student> studentslist = new ArrayList < Student> ( ) ;
static List< Admin> adminList = new ArrayList < Admin> ( ) ;
static boolean flag = false ;
public static void main ( String[ ] args) {
new Student_Main ( ) ;
}
public Student_Main ( ) {
studentslist. add ( new Student ( "张三" , "172056101" , "男" , 18 , 95 ) ) ;
studentslist. add ( new Student ( "李一" , "172056102" , "女" , 21 , 90 ) ) ;
studentslist. add ( new Student ( "黄二" , "172056103" , "男" , 20 , 80 ) ) ;
studentslist. add ( new Student ( "魏五" , "172056104" , "男" , 17 , 100 ) ) ;
studentslist. add ( new Student ( "刘四" , "172056105" , "女" , 22 , 75 ) ) ;
adminList. add ( new Admin ( "admin1" , "123456" ) ) ;
adminList. add ( new Admin ( "admin2" , "456123" ) ) ;
adminList. add ( new Admin ( "admin3" , "654321" ) ) ;
adminList. add ( new Admin ( "admin4" , "123456789" ) ) ;
System. out. println ( "---------------欢迎登录学生管理系统" + "---------------" ) ;
inPutAdmin ( ) ;
}
public static void inPutAdmin ( ) {
System. out. println ( "请输入用户名" ) ;
Scanner sc = new Scanner ( System. in ) ;
String userName = sc. nextLine ( ) ;
for ( int i = 0 ; i < adminList. size ( ) ; i++ ) {
if ( userName. equals ( adminList. get ( i) . getAdminName ( ) ) ) {
System. out. println ( "请输入密码,你有三次机会" ) ;
Scanner sc2 = new Scanner ( System. in ) ;
String userPassworld = sc2. nextLine ( ) ;
for ( int j = 0 ; j < 1 ; j++ ) {
if ( userPassworld. equals ( adminList. get ( i) . getAdminpassword ( ) ) ) {
System. out. println ( "登录成功" ) ;
inPutStudent ( ) ;
break ;
} else {
chessPassworld ( ) ;
}
if ( j == 0 ) {
System. out. println ( "你的密码都输错了,不要乱猜老师的密码了哦!!!" ) ;
System. out. println ( "系统自动退出" ) ;
}
}
flag = true ;
break ;
}
}
if ( ! flag) {
System. out. println ( "用户名不匹配,请重新输入" ) ;
inPutAdmin ( ) ;
}
}
public static String chessPassworld ( ) {
System. out. println ( "密码错误,请重新输入密码" ) ;
Scanner sc3 = new Scanner ( System. in ) ;
String userPassworld = sc3. nextLine ( ) ;
return userPassworld;
}
public static void inPutStudent ( ) {
System. out. println ( ) ;
System. out. println ( "请选择需要的业务" ) ;
System. out. println ( "-------------------------------------" ) ;
System. out. println ( "--------- 1.添加学生" + " ----------" ) ;
System. out. println ( "--------- 2.查询学生" + " ----------" ) ;
System. out. println ( "--------- 3.修改学生" + " ----------" ) ;
System. out. println ( "--------- 4.删除学生" + " ----------" ) ;
System. out. println ( "--------- 5.退出系统" + " ----------" ) ;
fun ( ) ;
}
public static void fun ( ) {
Scanner sc4 = new Scanner ( System. in ) ;
String mm = sc4. nextLine ( ) ;
if ( mm. equals ( "1" ) ) {
addStudent ( ) ;
} else if ( mm. equals ( "2" ) ) {
inquireStudent ( ) ;
} else if ( mm. equals ( "3" ) ) {
amendStudent ( ) ;
} else if ( mm. equals ( "4" ) ) {
removeStudent ( ) ;
} else if ( mm. equals ( "5" ) ) {
exitSystem ( ) ;
} else {
System. out. println ( "你输入的格式有误,请重新输入你想要实现的功能" ) ;
fun ( ) ;
}
}
public static void exit1 ( ) {
System. out. println ( "请问是否继续添加学生信息呢?Y/任意一个键即可返回主页面" ) ;
Scanner scc = new Scanner ( System. in ) ;
String exit = scc. nextLine ( ) ;
if ( exit. equals ( "Y" ) || exit. equals ( "y" ) ) {
addStudent ( ) ;
} else {
inPutStudent ( ) ;
}
}
public static void exit2 ( ) {
System. out. println ( "请问是否继续查询学生信息呢?Y/任意一个键即可返回主页面" ) ;
Scanner scc = new Scanner ( System. in ) ;
String exit = scc. nextLine ( ) ;
if ( exit. equals ( "Y" ) || exit. equals ( "y" ) ) {
inquireStudent ( ) ;
} else {
inPutStudent ( ) ;
}
}
public static void exit3 ( ) {
System. out. println ( "请问是否继续修改学生信息呢?Y/任意一个键即可返回主页面" ) ;
Scanner scc = new Scanner ( System. in ) ;
String exit = scc. nextLine ( ) ;
if ( exit. equals ( "Y" ) || exit. equals ( "y" ) ) {
amendStudent ( ) ;
} else {
inPutStudent ( ) ;
}
}
public static void exit4 ( ) {
System. out. println ( "请问是否继续删除学生信息呢?Y/任意一个键即可返回主页面" ) ;
Scanner scc = new Scanner ( System. in ) ;
String exit = scc. nextLine ( ) ;
if ( exit. equals ( "Y" ) || exit. equals ( "y" ) ) {
removeStudent ( ) ;
} else {
inPutStudent ( ) ;
}
}
public static void addStudent ( ) {
System. out. println ( "请输入该学生的全部信息(请按照下面的格式进行输入):" ) ;
System. out. println ( "请输入想录入系统的学生名字,学号,性别(男,女),年龄,成绩 中间用空格隔开" ) ;
Scanner sc5 = new Scanner ( System. in ) ;
String input = sc5. nextLine ( ) ;
String[ ] strArr= input. split ( " " ) ;
String name = strArr[ 0 ] ;
String sno = strArr[ 1 ] ;
String gender = strArr[ 2 ] ;
int age= Integer. parseInt ( strArr[ 3 ] ) ;
int score= Integer. parseInt ( strArr[ 4 ] ) ;
studentslist. add ( new Student ( name, sno, gender, age, score) ) ;
System. out. println ( "添加成功!" ) ;
exit1 ( ) ;
}
public static void inquireStudent ( ) {
System. out. println ( "你是要查询该系统所有学生还是单个学生(请输入所有学生或者单个学生)" ) ;
Scanner sc9 = new Scanner ( System. in ) ;
String allString = sc9. nextLine ( ) ;
if ( allString. equals ( "所有学生" ) ) {
System. out. println ( "已经查询到已录入该系统的全部学生!" ) ;
for ( Student student : studentslist) {
System. out. println ( student) ;
}
exit2 ( ) ;
} else if ( allString. equals ( "单个学生" ) ) {
System. out. println ( "请输入你想要查询的学生的学号" ) ;
Scanner sc10 = new Scanner ( System. in ) ;
String snoStudent = sc10. nextLine ( ) ;
for ( int i = 0 ; i < studentslist. size ( ) ; i++ ) {
if ( snoStudent. equals ( studentslist. get ( i) . getSno ( ) ) ) {
System. out. println ( "查询成功!" ) ;
System. out. println ( studentslist. get ( i) ) ;
flag = true ;
break ;
}
}
if ( ! flag) {
System. out. println ( "没有找到该学生的信息" ) ;
}
exit2 ( ) ;
} else {
System. out. println ( "您输入的有误,请重新输入" ) ;
inquireStudent ( ) ;
}
}
public static void amendStudent ( ) {
System. out. println ( "请输入你想要修改的学生的学号" ) ;
Scanner sc11 = new Scanner ( System. in ) ;
String snoStudent = sc11. nextLine ( ) ;
for ( int i = 0 ; i < studentslist. size ( ) ; i++ ) {
if ( snoStudent. equals ( studentslist. get ( i) . getSno ( ) ) ) {
System. out. println ( "请输入你想要修改的学生的哪一信息(姓名,学号,年龄,性别,成绩)" ) ;
Scanner scCC = new Scanner ( System. in ) ;
String sccc = scCC. nextLine ( ) ;
System. out. println ( "请输入你重新录入的信息" ) ;
Scanner scCC2 = new Scanner ( System. in ) ;
String sccc2 = scCC2. nextLine ( ) ;
if ( sccc. equals ( "姓名" ) ) {
studentslist. get ( i) . setName ( sccc2) ;
} else if ( sccc. equals ( "学号" ) ) {
studentslist. get ( i) . setSno ( sccc2) ;
} else if ( sccc. equals ( "年龄" ) ) {
int sccc3 = Integer. parseInt ( sccc2) ;
studentslist. get ( i) . setAge ( sccc3) ;
} else if ( sccc. equals ( "性别" ) ) {
studentslist. get ( i) . setGender ( sccc2) ;
} else if ( sccc. equals ( "成绩" ) ) {
int sccc3 = Integer. parseInt ( sccc2) ;
studentslist. get ( i) . setScore ( sccc3) ;
}
flag = true ;
System. out. println ( "修改成功" ) ;
break ;
}
}
if ( ! flag) {
System. out. println ( "没有找到该学生的信息" ) ;
}
exit3 ( ) ;
}
public static void removeStudent ( ) {
System. out. println ( "请输入你想要删除的学生的学号" ) ;
Scanner sc12 = new Scanner ( System. in ) ;
String snoStudent = sc12. nextLine ( ) ;
for ( int i = 0 ; i < studentslist. size ( ) ; i++ ) {
if ( snoStudent. equals ( studentslist. get ( i) . getSno ( ) ) ) {
studentslist. remove ( i) ;
System. out. println ( "删除成功!" ) ;
flag = true ;
break ;
}
}
if ( ! flag) {
System. out. println ( "没有找到该学生的信息" ) ;
}
exit4 ( ) ;
}
public static void exitSystem ( ) {
for ( int i = 0 ; i < 1 ; i++ ) {
System. out. println ( "退出系统请按N,其他键为返回主页面" ) ;
Scanner sc13 = new Scanner ( System. in ) ;
String string = sc13. nextLine ( ) ;
if ( string. equals ( "N" ) || string. equals ( "n" ) ) {
System. out. println ( "当前系统已经退出,欢迎下次登录" ) ;
break ;
}
}
}
}
有什么不懂的,可以加qq1638580498私聊