总体架构
Student.java
//Student.java
package StudentSys;
public class Student {
private String id;
private String name;
private int age;
private String address;
public Student(String id, String name, int age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public Student() {
}
/**
* 获取
* @return id
*/
public String getId() {
return id;
}
/**
* 设置
* @param id
*/
public void setId(String id) {
this.id = id;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
/**
* 获取
* @return address
*/
public String getAddress() {
return address;
}
/**
* 设置
* @param address
*/
public void setAddress(String address) {
this.address = address;
}
public String toString() {
return "Student{id = " + id + ", name = " + name + ", age = " + age + ", address = " + address + "}";
}
public String toMess(){
return id+" "+name+" "+age+" "+address;
}
}
StudentTest.java
//StudentTest.java
package StudentSys;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentTest {
/*=================================学生管理系统的主界面=================================*/
public static void StartStudentSystem() {
System.out.println("\"----------Welcome to the Student System----------\"");
System.out.println("1:添加学生");
System.out.println("2:删除学生");
System.out.println("3:修改学生");
System.out.println("4:查询学生");
System.out.println("0:退出");
Scanner sc = new Scanner(System.in);
ArrayList<Student> StudentList = new ArrayList<>();
while (true) {
System.out.println("请输入你的选项");
int choice = sc.nextInt();
switch (choice) {
case 1: {
System.out.println("开始添加学生");
StudentAdd(StudentList);
break;
}
case 2: {
System.out.println("开始删除学生");
StudentDel(StudentList);
break;
}
case 3: {
System.out.println("开始修改学生");
StudentSet(StudentList);
break;
}
case 4: {
System.out.println("开始查询学生");
StudentLook(StudentList);
break;
}
case 0: {
System.out.println("退出系统");
return;
}
}
}
}
/*=================================学生管理系统的主界面=================================*/
/*=================================增加学生的方法=================================*/
private static void StudentAdd(ArrayList<Student> StudentList) {
Scanner sc = new Scanner(System.in);
String id;
String name;
int age;
String address;
Student student = new Student();
System.out.println("请输入学生的id");
id = sc.next();
if (OneStudentId(StudentList, id)) {
student.setId(id);
System.out.println("请输入学生的name");
name = sc.next();
student.setName(name);
System.out.println("请输入学生的age");
age = sc.nextInt();
student.setAge(age);
System.out.println("请输入学生的address");
address = sc.next();
student.setAddress(address);
Boolean result = StudentList.add(student);
if (result) {
System.out.println("添加学生信息成功");
System.out.println(student.toString());
} else {
System.out.println("添加失败信息成功");
}
} else {
System.out.println("id不唯一,不能添加");
}
}
/*=================================增加学生的方法=================================*/
/*=================================确保ID唯一的方法=================================*/
private static Boolean OneStudentId(ArrayList<Student> StudentList, String id) {
//集合为空,无重复元素
if (StudentList.size() == 0) {
return true;
}
//遍历集合
for (int i = 0; i < StudentList.size(); i++) {
if (StudentList.get(i).getId().equals(id))
return false;
}
//都没有就返回是唯一
return true;
}
/*=================================确保ID唯一的方法=================================*/
/*=================================通过ID找到此学生在集合中对应的序号的方法=================================*/
private static int StudentFindIndex(ArrayList<Student> StudentList, String id) {
for (int i = 0; i < StudentList.size(); i++) {
if (StudentList.get(i).getId().equals(id)) {
System.out.println("找到目标索引:" + i);
return i;
}
}
System.out.println("找不到目标" );
return -1;
}
/*=================================通过ID找到此学生在集合中对应的序号的方法=================================*/
/*=================================删除学生的方法=================================*/
private static void StudentDel(ArrayList<Student> StudentList) {
System.out.println("请输入删除学生的id");
Scanner sc = new Scanner(System.in);
String id = sc.next();
int index = StudentFindIndex(StudentList, id);
if (index != -1) {
String StudentDelMes= StudentList.get(index).toString();
StudentList.remove(index);
System.out.println("删除成功"+StudentDelMes);
}
else{
System.out.println("找不到该学生");
}
}
/*=================================删除学生的方法=================================*/
/*=================================查看学生的方法=================================*/
private static void StudentLook(ArrayList<Student> StudentList){
if (StudentList.size() == 0) {
System.out.println("没有学生信息");
}
System.out.println("ID name age addre");
for (int i=0;i<StudentList.size();i++){
System.out.println(StudentList.get(i).toMess());
}
}
/*=================================查看学生的方法=================================*/
/*=================================修改学生的方法=================================*/
private static void StudentSet(ArrayList<Student> StudentList){
Scanner sc = new Scanner(System.in);
System.out.println("请输入修改学生的id");
String id = sc.next();
int index = StudentFindIndex(StudentList, id);
if(index != -1) {
System.out.println("1:修改学生id");
System.out.println("2:修改学生name");
System.out.println("3:修改学生age");
System.out.println("4:修改学生address");
System.out.println("5:返回");
System.out.println("请输入你对此学生的操作");
int Choice = sc.nextInt();
switch (Choice) {
case 1: {
System.out.println("请输入修改的id");
String idchange=sc.next();
StudentList.get(index).setId(idchange);
System.out.println("id修改成功"+idchange);
break;
}
case 2: {
System.out.println("请输入修改的name");
String namechange=sc.next();
StudentList.get(index).setName(namechange);
System.out.println("name修改成功"+namechange);
break;
}
case 3: {
System.out.println("请输入修改的age");
int agechange=sc.nextInt();
StudentList.get(index).setAge(agechange);
System.out.println("age修改成功"+agechange);
break;
}
case 4: {
System.out.println("请输入修改的address");
String addresschange=sc.next();
StudentList.get(index).setAddress(addresschange);
System.out.println("address修改成功"+addresschange);
break;
}
case 5: {
System.out.println("退出修改学生操作");
break;
}
}
}
}
/*=================================修改学生的方法=================================*/
}
User.java
//User.java
package StudentSys;
public class User {
String name;
String password;
String phonenumber;
public User() {
}
public User(String name, String password, String phonenumber) {
this.name = name;
this.password = password;
this.phonenumber = phonenumber;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return password
*/
public String getPassword() {
return password;
}
/**
* 设置
* @param password
*/
public void setPassword(String password) {
this.password = password;
}
/**
* 获取
* @return phonenumber
*/
public String getPhonenumber() {
return phonenumber;
}
/**
* 设置
* @param phonenumber
*/
public void setPhonenumber(String phonenumber) {
this.phonenumber = phonenumber;
}
public String toString() {
return "User{name = " + name + ", password = " + password + ", phonenumber = " + phonenumber + "}";
}
public String toMess(){
return name+" "+password+" "+phonenumber;
}
}
UserTest.java
//UserTest.java
package StudentSys;
import java.util.ArrayList;
import java.util.Scanner;
public class UserTest {
public static void main(String[] args) {
ArrayList<Student> StudentList = new ArrayList<>();
Scanner sc=new Scanner(System.in);
ArrayList<User> UsersList=new ArrayList<>();
System.out.println("======欢迎来到登录界面======");
System.out.println("1:登录");
System.out.println("2:注册");
System.out.println("3:查看用户");
System.out.println("4:忘记密码");
System.out.println("0:退出");
while(true) {
System.out.println("请输入操作");
int Choice = sc.nextInt();
switch (Choice) {
case 1: {
System.out.println("选择登录");
UserLogin(UsersList);
break;
}
case 2: {
System.out.println("选择注册");
UserRegister(UsersList);
break;
}
case 3: {
System.out.println("选择查看用户");
UserLook( UsersList);
break;
}
case 4: {
System.out.println("选择忘记密码");
UserForgetPassword(UsersList);
break;
}
case 0: {
System.out.println("选择退出");
System.exit(0);
}
}
}
}
/*========================注册使用到的方法=====================================*/
private static void UserRegister(ArrayList<User> UsersList){
System.out.println("开始注册");
User newUser=new User();
String name;
String password;
String phonenumber;
Scanner sc=new Scanner(System.in);
System.out.println("请输入用户的名字");
name=sc.next();
if(UserRegisterNameLegal(UsersList,name)) {
newUser.setName(name);
System.out.println("请输入用户的密码");
password = sc.next();
newUser.setPassword(password);
System.out.println("请输入用户的手机号码");
phonenumber = sc.next();
newUser.setPhonenumber(phonenumber);
System.out.println(newUser);
UsersList.add(newUser);
}
}
//总的判断是否合法
private static Boolean UserRegisterNameLegal(ArrayList<User> UsersList,String name){
if(UserRegisterNameUnique(UsersList,name)&&UserRegisterNameLength(name)&&UserRegisterNameMixed(name)){
return true;
}
return false;
}
//判断名字是否唯一
private static Boolean UserRegisterNameUnique(ArrayList<User> UsersList,String name){
for (int i = 0; i < UsersList.size(); i++) {
if(UsersList.get(i).getName().equals(name)){
System.out.println("用户名重复");
return false;
}
}
return true;
}
//判断名字长度是否满足条件
private static Boolean UserRegisterNameLength(String name){
if(name.length()>=3&&name.length()<10){
return true;
}
System.out.println("输入的名字长度不合法");
return false;
}
//判断名字组成是不是数字或者字母,且不能全为数字
private static Boolean UserRegisterNameMixed(String name){
if(UserRegisterNameNumChar(name)&&UserRegisterNameLetterone(name)){
return true;
}
return false;
}
//判断名字是不是由数字或者字母组成
private static Boolean UserRegisterNameNumChar(String name){
for(int i=0;i<name.length();i++){
char c=name.charAt(i);
if(UserRegisterCNumChar(c)){
return true;
}
return false;
}
System.out.println("数字字母不合法");
return false;
}
//判断名字的单个字符是不是由数字或者字母
private static Boolean UserRegisterCNumChar(char c){
if(c>='0'&&c<='9'){
return true;
}
else if ((c>='a'&&c<='z')||(c>='A'&&c<='Z')) {
return true;
}
return false;
}
//判断名字有没有字母
private static Boolean UserRegisterNameLetterone(String name){
for(int i=0;i<name.length();i++){
char c=name.charAt(i);
if((c>='a'&&c<='z')||(c>='A'&&c<='Z')){
return true;
}
}
System.out.println("无字母,不合法");
return false;
}
/*========================注册使用到的方法=====================================*/
/*========================查看用户用到的方法=====================================*/
private static void UserLook(ArrayList<User> UsersList){
for(int i=0;i< UsersList.size();i++){
System.out.println("name password phonenumber");
System.out.println(UsersList.get(i).toMess());
}
}
/*========================查看用户用到的方法=====================================*/
/*========================用户登录用到的方法=====================================*/
//登录的主界面
private static void UserLogin(ArrayList<User> UsersList){
Scanner sc=new Scanner(System.in);
System.out.println("欢迎登录");
System.out.println("请输入用户名");
int index;
String name;
String passwordIn;
String password;
name=sc.next();
if(UserFindIndexByName(UsersList,name)!=-1) {
index=UserFindIndexByName(UsersList,name);
System.out.println("请输入密码");
passwordIn = sc.next();
password=UserFindPasswordByIndex(UsersList,index);
if(passwordIn.equals(password)){
System.out.println("登录成功");
StudentTest ss=new StudentTest();
ss.StartStudentSystem();
}
else {
System.out.println("密码错误,登录失败");
}
}
}
//遍历集合,寻找name在集合中对应的序号
private static int UserFindIndexByName(ArrayList<User> UsersList,String name){
String findname;
for(int i=0;i< UsersList.size();i++){
findname=UsersList.get(i).getName();
if(findname.equals(name)){
return i;
}
}
System.out.println("此用户未注册");
return -1;
}
//通过序号找到对应的name
private static String UserFindNameByIndex(ArrayList<User> UsersList,int index){
String findname;
findname=UsersList.get(index).getName();
return findname;
}
//通过序号找对应的密码
private static String UserFindPasswordByIndex(ArrayList<User> UsersList,int index){
String Password;
Password=UsersList.get(index).getPassword();
return Password;
}
/*========================用户登录用到的方法=====================================*/
/*========================用户重置密码用到的方法=====================================*/
//找回密码的主界面
private static void UserForgetPassword(ArrayList<User> UsersList){
System.out.println("请输入你找回密码的用户名");
Scanner sc=new Scanner(System.in);
String name;
String password;
String phonenumber;
String phonenumberIn;
int index;
name=sc.next();
index=UserFindIndexByName(UsersList,name);
phonenumber=UserFindPhonenumberByIndex(UsersList,index);
if(index!=-1){
System.out.println("请输入修改手机号码");
phonenumberIn=sc.next();
if(phonenumberIn.equals(phonenumber)){
System.out.println("核验通过,开始修改用户密码");
password=sc.next();
System.out.println("请输入修改后的密码");
UsersList.get(index).setPassword(password);
return ;
}
else{
System.out.println("核验不通过");
}
}
}
//通过序号找集合中对应的手机号码
private static String UserFindPhonenumberByIndex(ArrayList<User> UsersList,int index){
String Phonenumber;
Phonenumber=UsersList.get(index).getPhonenumber();
return Phonenumber;
}
/*========================用户重置密码用到的方法=====================================*/
}
控制台演示
登录.输入2,先开始注册
登录.输入3,查看已有的用户信息
登录.输入4,忘记密码,进行重置密码
登录.输入1,进行登录
密码错误,重新选择操作
密码正确,进入学生管理系统
学生管理.输入1,添加学生信息
学生管理.输入4,查看学生信息
学生管理.输入3,修改学生信息
选择3后输入修改的学生id为001,再选择修改address,修改为hangzhou
学生管理.输入2,删除学生信息
输入id 002,amy已经被删除了
不足之处
1.某些输入处理不够好
2.数据没能持续存储
先创建两个user
使用leo1进行登录
选择添加学生
输入0退出系统
然后使用leo2进行登录
找不到leo1添加的学生的信息