学生管理系统
Javabean类
java
复制代码
package heima.test;
public class Studnt {
//属性:id、姓名、年龄、家庭住址
private int id;
private String name;
private int age;
private String address;
public Studnt() {
}
public Studnt(int id, String name, int age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
}
Javatest类
java
复制代码
package heima.test;
import com.sun.security.jgss.GSSUtil;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Scanner;
public class Studnttest {
public static void startmain() {
ArrayList<Studnt> studnts = new ArrayList<>();
Scanner sc = new Scanner(System.in);
loop: while (true) {
System.out.println("-------------欢迎来到黑马学生管理系统----------------");
System.out.println("1:添加学生");
System.out.println("2:删除学生");
System.out.println("3:修改学生");
System.out.println("4:查询学生");
System.out.println("5:退出");
System.out.println("请输入您的选择:");
int choose = sc.nextInt();
switch (choose) {
case 1 -> addstudent(studnts);
case 2 -> deletstudent(studnts);
case 3 -> changestudent(studnts);
case 4 -> inquirestudent(studnts);
case 5 -> { System.out.println("5:退出");
break loop;
}
default -> System.out.println("没有此选项");
}
}
printlist(studnts);
}
//添加学生
public static void addstudent(ArrayList<Studnt> studnts){
System.out.println("1:添加学生");
Scanner sc = new Scanner(System.in);
Studnt s = new Studnt();
while (true) {
System.out.println("请输入需要添加的学生id");
int id = sc.nextInt();
boolean flag = judge(studnts,id);
if(flag){
System.out.println("学生信息已存在,请重新录入");
}else {
s.setId(id);
break;
}
}
System.out.println("请输入需要添加的学生名字");
s.setName(sc.next());
System.out.println("请输入需要添加的学生年龄");
s.setAge(sc.nextInt());
System.out.println("请输入需要添加的学生地址");
s.setAddress(sc.next());
studnts.add(s);
System.out.println("学生信息添加成功");
}
//删除学生
public static void deletstudent(ArrayList<Studnt> studnts){
Scanner sc = new Scanner(System.in);
System.out.println("2:删除学生");
System.out.println("请输入想要删除学上的id");
int temp = sc.nextInt();
int index= getindex(studnts,temp);
if(index>=0){
studnts.remove(index);
}else {
System.out.println("id不存在,删除失败");
}
}
//修改
public static void changestudent(ArrayList<Studnt> studnts){
Scanner sc = new Scanner(System.in);
System.out.println("3:修改学生");
System.out.println("请输入要修改的学生id");
int temp = sc.nextInt();
int getindex = getindex(studnts,temp);
if ((getindex<0)){
System.out.println("学生信息不存在,删除失败");
}else{
System.out.println("请输入新的学生名字");
Studnt s = studnts.get(getindex);
String newname = sc.next();
s.setName(newname);
System.out.println("请输入新的学生年龄");
int newage = sc.nextInt();
s.setAge(newage);
System.out.println("请输入新的学生地址");
String newaddress = sc.next();
s.setAddress(newaddress);
System.out.println("学生信息修改成功");
}
}
public static void inquirestudent(ArrayList<Studnt> studnts){
System.out.println("4:查询学生");
if (studnts.size() == 0) {
System.out.println("当前无学生信息,请添加后再查询");
}else {
System.out.println("id\t\t姓名\t年龄\t地址");
for (int i = 0; i < studnts.size(); i++) {
Studnt s = studnts.get(i);
System.out.println(s.getName()+" "+s.getId()+" "+s.getAge()+" "+s.getAddress());
}
}
}
//遍历数组
public static void printlist(ArrayList<Studnt> studnts){
for (int i = 0; i < studnts.size(); i++) {
Studnt s = studnts.get(i);
System.out.println(s.getName()+" "+s.getId()+" "+s.getAge()+" "+s.getAddress());
}
}
//判断唯一性
public static boolean judge(ArrayList<Studnt> studnts,int id){
for (int i = 0; i < studnts.size(); i++) {
if (studnts.get(i).getId() == id){
return true;
}
}
return false;
}
//查询id返回下标
public static int getindex(ArrayList<Studnt> studnts,int id){
for (int i = 0; i < studnts.size(); i++) {
if (studnts.get(i).getId() == id){
return i;
}
}
return -1;
}
}
学生管理系统升级版
Javabean类
Java
复制代码
package heima.test;
public class User {
//属性:用户名、密码、身份证号码、手机号码
private String name;
private String password;
private String personnumber;
private String phonenumber;
public User() {
}
public User(String name, String password, String personnumber, String phonenumber) {
this.name = name;
this.password = password;
this.personnumber = personnumber;
this.phonenumber = phonenumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPersonnumber() {
return personnumber;
}
public void setPersonnumber(String personnumber) {
this.personnumber = personnumber;
}
public String getPhonenumber() {
return phonenumber;
}
public void setPhonenumber(String phonenumber) {
this.phonenumber = phonenumber;
}
}
Javatest类
java
复制代码
package heima.test;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Usertest {
static void main() {
ArrayList<User> list = new ArrayList<>();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("欢迎来到学生管理系统");
System.out.println("请选择操作1登录,2注册,3,忘记密码 4,退出");
int choice = sc.nextInt();
switch (choice) {
case 1-> login(list);
case 2-> register(list);
case 3-> forget(list);
case 4-> {
System.out.println("推出");
return;
}
default -> System.out.println("没有此选项");
}
}
}
public static void login(ArrayList<User> list) {
Scanner sc = new Scanner(System.in);
System.out.println("登录");
for (int i = 0; i < 3; i++) {
System.out.println("请输入用户名");
String name = sc.next();
System.out.println("请输入密码");
String password = sc.next();
boolean flag = unique(list,name);
if (!flag) {
System.out.println("用户不存在,请先注册");
return;
}
while (true) {
System.out.println("请输入验证码");
String usercode = sc.next();
code();
String code = sc.next();
System.out.println(code);
if (!usercode.equalsIgnoreCase(code)) {
System.out.println("验证码错误,请重新输入");
}else{
break;
}
}
User user = new User(name,password,null,null);
Boolean flag1 = cheaklogin(list,user);
if (!flag1) {
System.out.println("用户名或密码错误,请重新输入");
}else if (i==2){
System.out.println("当前用户已被锁定,请联系客服");
}else{
System.out.println("登陆成功");
Studnttest.startmain();
break;
}
}
}
public static void register(ArrayList<User> list) {
String name;
String password;
String personnumber;
String phonenumber;
System.out.println("注册");
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入需要注册的用户名");
name = sc.next();
// 用户名唯一
boolean flag = unique(list, name);
if (flag) {
System.out.println("用户已存在,请重新你输入");
continue;
}
// 用户名长度必须在3~15位之间
if (!(name.length()>3&&name.length()<15)) {
System.out.println("用户名长度必须在3~15位之间");
continue;
}
// //只能是字母加数字的组合,但是不能是纯数字
Boolean flag1 = cheakname(name);
if (!flag1) {
System.out.println("只能是字母加数字的组合,但是不能是纯数字");
}
break;
}
while (true) {
//2,密码键盘输入两次,两次一致才可以进行注册。
System.out.println("请输入密码");
password = sc.next();
System.out.println("请再次输入密码");
String password01 = sc.next();
if (password.equals(password01)) {
break;
}
System.out.println("两次输入的密码不一致,请重新输入");
}
while (true) {
System.out.println("请输入身份证号");
personnumber = sc.next();
boolean flag = cheakpersonnumber(personnumber);
if (!flag) {
System.out.println("身份证格式不对,请重新输入");
}else {
System.out.println("身份证格式正确");
break;
}
}
while (true) {
System.out.println("请输入手机号");
phonenumber = sc.next();
boolean flag = cheakphonenumber(phonenumber);
if (!flag) {
System.out.println("手机号格式不对");
}else{
System.out.println("手机号可用");
break;
}
}
User newUser = new User(name,password,personnumber,phonenumber);
list.add(newUser);
printlist(list);
}
public static void forget(ArrayList<User> list) {
Scanner sc = new Scanner(System.in);
System.out.println("忘记密码");
//,键盘录入用户名,判断当前用户名是否存在,如不存在,直接结束方法,并提示:未注册
System.out.println("请输入用户名");
String name = sc.next();
boolean flag = unique(list, name);
if (!flag) {
System.out.println("用户名不存在,未注册");
return;
}
System.out.println("请输入身份证号");
String peronnumber = sc.next();
System.out.println("请输入手机号");
String phonenumber = sc.next();
int i = getindex(list,name);
User newUser = list.get(i);
if (!(newUser.getPersonnumber().equals(peronnumber) && newUser.getPhonenumber().equals(phonenumber))) {
System.out.println("账号信息不匹配,修改失败");
}else{
while (true) {
System.out.println("请输入新密码");
String password = sc.next();
System.out.println("请再次输入新密码");
String newpassword = sc.next();
if (!(password.equals(newpassword))) {
System.out.println("两次密码不一样,请重新输入");
}else{
newUser.setPassword(newpassword);
System.out.println("修改成功");
break;
}
}
}
}
public static boolean unique(ArrayList<User> list,String number) {
//检查唯一性
for (int i = 0; i < list.size(); i++) {
User u = list.get(i);
if (u.getName().equals(number)) {
return true ;
}
}
return false;
}
public static boolean cheakname(String name){
char[] ch = name.toCharArray();
int count = 0;
for (int i = 0; i < ch.length; i++) {
char c = ch[i];
if(!((c>='A' && c<='Z')||(c>='a' && c<='z')||(c>='0' && c<='9'))) {
return false;
}
else if ((c>='A' && c<='Z')||(c>='a' && c<='z')) {
count++;
break;
}else {
return false;
}
}
return true;
}
public static boolean cheakpersonnumber(String personnumber){
// 3,身份证号码需要验证。
char[] ch = personnumber.toCharArray();
//长度为18位
if (ch.length!=18){
return false;
}
//不能以0为开头
boolean flag = personnumber.startsWith("0");
if (flag) {
return false;
}
//前17位,必须都是数字
for (int i = 0; i < ch.length-1; i++) {
char c = ch[i];
if(!(c>='0' && c<='9')){
return false;
}
}
// 最为一位可以是数字,也可以是大写X或小写x
char c1 = ch[ch.length-1];
if (!((c1>='0' && c1<='9')||(c1=='X')||(c1=='x'))){
return false;
}
return true;
}
public static boolean cheakphonenumber(String phonenumber){
//长度为11位
if(phonenumber.length()!=11){
return false;
}
//不能以0为开头
boolean flag = phonenumber.startsWith("0");
if (flag) {
return false;
}
//必须都是数字
char[] ch = phonenumber.toCharArray();
for (int i = 0; i < ch.length; i++) {
char c = ch[i];
if(!(c>='0' && c<='9')){
return false;
}
}
return true;
}
public static void code(){
ArrayList<Character> list = new ArrayList<>();
for (int i = 0; i < 26; i++) {
list.add((char)(i+'a'));
list.add((char)(i+'A'));
}
Random random = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4; i++) {
int R = random.nextInt(list.size());
char c = list.get(R);
sb.append(c);
}
int R = random.nextInt(10);
sb.append(R);
char [] ch =sb.toString().toCharArray();
for (int i = 0; i < ch.length; i++) {
int R1 = random.nextInt(sb.length());
char temp = ch[R1];
ch[R1] = ch[ch.length-1];
ch[ch.length-1] = temp;
}
System.out.println(new String(ch));
}
public static void printlist(ArrayList<User> list){
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getName()+","+list.get(i).getPersonnumber()+","+list.get(i).getPhonenumber());
}
}
public static boolean cheaklogin(ArrayList<User> list,User newUser) {
for (int i = 0; i < list.size(); i++) {
User u = list.get(i);
if (u.getName().equals(newUser.getName())) {
if (u.getPassword().equals(newUser.getPassword())) {
return true;
}
}
}
return false;
}
public static int getindex(ArrayList<User> list,String name){
for (int i = 0; i < list.size(); i++) {
User u = list.get(i);
if (u.getName().equals(name)) {
return i;
}
}
return -1;
}
}