package com.xingyun.studentdao;
import com.xingyun.bean.StudentBean;
import com.xingyun.bean.UserBean;
public interface StudentDao {
void Menu(); //菜单
void registered(); //注册
void login(); //登录
void add(); //新增信息
void getAll(); //查询(根据ID、姓名查询、也可以查询全部)
void UpDate(); //修改(根据ID、姓名修改)
void delete(); //删除(根据ID、姓名删除)
}
package com.xingyun.bean;
public class UserBean {
private String user_name; //账号
private String password; //密码
private int grade; //等级 1、老师 2、学生
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public UserBean(String user_name, String password, int grade) {
this.user_name = user_name;
this.password = password;
this.grade = grade;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public UserBean() {
}
@Override
public String toString() {
return "UserBean [user_name=" + user_name + ", password=" + password + ", grade=" + grade + "]";
}
}
package com.xingyun.bean;
import java.util.Arrays;
public class StudentBean {
private int s_id; //学号
private String name; //姓名
private int age; //年龄
private String sex; //性别
private String id_card; //身份证号
private String addres; //地址
private String Date; //出生日期
private String mailbox; //邮箱(qq邮箱和163邮箱)
private StringBuilder Personal_profile; //个人简介
//私有属性的封装
public String getMailbox() {
return mailbox;
}
public void setMailbox(String mailbox) {
this.mailbox = mailbox;
}
public int getS_id() {
return s_id;
}
public void setS_id(int s_id) {
this.s_id = s_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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getId_card() {
return id_card;
}
public void setId_card(String id_card) {
this.id_card = id_card;
}
public String getAddres() {
return addres;
}
public void setAddres(String addres) {
this.addres = addres;
}
public String getDate() {
return Date;
}
public void setDate(String date) {
Date = date;
}
public StringBuilder getPersonal_profile() {
return Personal_profile;
}
public void setPersonal_profile(StringBuilder personal_profile) {
Personal_profile = personal_profile;
}
//有参构造方法
//无参构造方法
public StudentBean() {
}
public StudentBean(int s_id, String name, int age, String sex, String id_card, String addres, String date,
String mailbox, StringBuilder personal_profile) {
super();
this.s_id = s_id;
this.name = name;
this.age = age;
this.sex = sex;
this.id_card = id_card;
this.addres = addres;
this.Date = date;
this.mailbox = mailbox;
this.Personal_profile = personal_profile;
}
@Override
public String toString() {
return " 学号:" + s_id + "\t姓名:" + name + "\t 年龄:" + age + "\t性别:" + sex + "\n身份证:"
+ id_card + "\t地址:" + addres + "\n出生日期:" + Date + "\t邮箱:" + mailbox + "\n个人简介:"
+ Personal_profile ;
}
}
package com.xingyun.test;
import com.xingyun.bean.StudentBean;
import com.xingyun.bean.UserBean;
import com.xingyun.studentdaoimp.StudentDaoImp;
public class StudentTest {
public static void main(String[] args) {
StudentDaoImp sti=new StudentDaoImp();
sti.Menu();
}
}
public void Menu() {
//向上转型,来调用自身重写父类的方法
StudentDao stu = new StudentDaoImp();
boolean bool = true;
while (bool) {
System.out.println("1、注册账号");
System.out.println("2、直接登录");
System.out.println("3、退出");
System.out.println("*******************");
int exchange = input.nextInt();
switch (exchange) {
case 1:
//使用对象调用注册方法
stu.registered();
break;
case 2:
//使用对象调用登录方法
stu.login();
break;
default:
//输入其他就会结束程序运行
bool = false;
break;
}
} // 最外层while循环的
}
@Override
public void registered() {
//在方法内创建账号密码的对象,方便每一次调用此方法的时候都是新数据存在us里面,然后方便存入集合中,以达到数据不冲突
UserBean us = new UserBean();
boolean bool4 = true;
tiaozhuan: while (bool4) {
//账号有学生和老师两种不同权限
System.out.println("请选择你的身份\t1、老师\t 2、学生");
int exchange = input.nextInt();
switch (exchange) {
case 1:
System.out.println("请输入教职工验证码:");
//要想注册教职工账号,需要完成验证码验证
String api = input.next();
if (api.equals(API)) {
//验证成功会给账号对象的等级属性赋值为1,1代表教职工权限,2代表学生权限
us.setGrade(1);
System.out.println("验证成功:");
break tiaozhuan;
} else {
//验证失败就会返回主界面
System.out.println("验证失败!请重新选择:");
//跳出本次输出,进入下一次输入
continue tiaozhuan;
}
default:
//输入其他的就默认为学生身份注册,会给等级属性赋值为2
us.setGrade(2);
//结束注册界面的 操作,进入注册界面
break tiaozhuan;
}
}
System.out.println("\t\t\t\t !注意,账号需要是你的手机号");
jieshu1: while (true) {
//存储键盘输入的账号
String user_name;
boolean bool = true;
jieshu: while (bool) {
System.out.print("注册账号:");
user_name = input.next();
//将字符串转为字符数组,方便判断是否为纯数字
char[] charArray2 = user_name.toCharArray();
//进行手机号验证------要求:11位纯数字数字,并且开头前两位为13、15、18方可满足标准
if (user_name.length() == 11 && (user_name.substring(0, 2).equals("13")
|| user_name.substring(0, 2).equals("15") || user_name.substring(0, 2).equals("18"))) {
//判断是否为纯数字
for(int e=0;e='0'&&charArray2[e]<='9') {
}else {
System.out.println("含有非法字符!请重新输入:");
continue jieshu;
}
}
//判断该账号是否已经存在
Iterator iterator = list_us.iterator();
while (iterator.hasNext()) {
if (user_name.equals(iterator.next().getUser_name())) {
System.out.println("该账号已经存在!请重新输入:");
break jieshu;
}
}
System.out.println("账号格式正确!");
} else {
System.out.println("账号格式不标准!请重新输入:");
break jieshu;
}
//密码要求:6-10位之间,首字母大写,含数字、小写字母、
System.out.print("注册密码:");
System.out.println("\t\t\t\t !注意,密码首字母大写,还需要包含小写字母和数字,位数在6-10之间");
String password = null;
//用来计数相当于
int j = 0, k = 0;
loop: while (true) {
password = input.next();
//判断密码位数是否为6-10位
if (password.length() >= 6 && password.length() <= 10) {
j = k = 0;
//是否首字母大写
if (password.charAt(0) >= 'A' && password.charAt(0) <= 'Z') {
char[] charArray = password.toCharArray();
boolean bol = true, bol1 = true;
j = k = 0;
//是否含有数字
for (int i = 0; i < charArray.length; i++) {
if (charArray[i] >= '0' && charArray[i] <= '9') {
bol = false;
} else { //不满足条件就重新输入
j++;
if (j == charArray.length) {
System.out.println("密码缺少数字,请重新输入:");
continue loop;
}
}
if (charArray[i] >= 'a' && charArray[i] <= 'z') {
bol1 = false;
} else {
k++;
if (k == charArray.length) {
System.out.println("密码缺少小写字母,请重新输入:");
continue loop;
}
}
if (!bol && !bol1) {
System.out.println("密码格式正确!");
break loop;
}
}
} else {
System.out.println("密码首字母非大写:请重新输入");
continue loop;
}
} else {
System.out.println("密码位数不对!请重新输入:");
continue loop;
}
}
//确认密码和前面注册的一样,只是为了加强用户的记忆
System.out.println("确认密码:");
boolean bol = true;
int i = 1;
while (i < 4) {
//验证成功就返回主界面
String pwd = input.next();
if (password.equals(pwd)) {
System.out.println("验证成功!");
bol = false;
break;
} else {
//如果输入确认密码超过三次,就会返回主界面,重新注册
if (3 - i != 0) {
System.out.println("你还有" + (3 - i) + "次机会!");
} else {
System.out.println("你没有机会了!");
}
//计数器
i++;
}
}
if (bol) {
System.out.println("验证错误!请重新注册");
break jieshu;
}
//能走到这一步说明满足前面所有条件,就可以通过对象的形式存入到集合中了
us.setUser_name(user_name);
us.setPassword(password);
list_us.add(us);
System.out.println("注册成功!");
break jieshu1;
}
}
}
@Override
public void login() {
boolean boo = true;
loop: while (boo) {
boolean bol = true;
System.out.print("账号:");
String user_name = input.next();
System.out.print("密码:");
String password = input.next();
//验证码--随机函数实现
//random类的随机数
boolean lop=true;
while(lop) {
Random rd=new Random();
//产生一个指定范围内的随机数,类型可以根据自己需求改变
int nextint=rd.nextInt(9);
int nextint1=rd.nextInt(9);
int nextint2=rd.nextInt(9);
int nextint3=rd.nextInt(9);
//拼接为字符串型
String num=""+nextint+""+nextint1+""+nextint2+""+nextint3;
System.out.println(num);
System.out.print("请输入验证码:");
String Code=input.next();
if(num.equals(Code)) {
System.out.println("验证成功");
break;
}else {
System.out.println("验证失败!");
continue;
}
}
//遍历集合
Iterator iterator = list_us.iterator();
while (iterator.hasNext()) {
//接受集合返回的对象
UserBean next = iterator.next();
boolean bool = true;
loop0: while (bool) {
//比较账号密码是否正确
if (user_name.equals(next.getUser_name()) && password.equals(next.getPassword())) {
bol = false;
//确定出此账号的权限
if (next.getGrade() == 1) {
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、退出");
System.out.println("-------------");
int exchange1 = input.nextInt();
switch (exchange1) {
case 1:
//添加学生信息
add();
continue loop0;
case 2:
//删除学生信息
delete();
continue loop0;
case 3:
//修改学生信息
UpDate();
continue loop0;
case 4:
//查找学生信息
getAll();
continue loop0;
default:
//输入其他的返回主界面
boo = false;
break loop;
}
}
//学生权限
if (next.getGrade() == 2) {
System.out.println("同学你好!");
//只能进行查询操作
getAll();
boo = false;
break loop;
}
}
break;
}
}
if (boo) {
System.out.println("账号密码错误:1、继续输入 \t 2、离开去注册");
int exchange = input.nextInt();
switch (exchange) {
case 1:
//重新登录
continue loop;
default:
//返回主界面去注册账号
break loop;
}
}
// 登录成功的功能:
}
}
@Override
public void add() {
System.out.println("*****************");
System.out.println("开始添加学生信息:");
System.out.println("*****************");
boolean bool = true;
loop: while (bool) {
//创建学生对象来存储每一次添加的数据
StudentBean st = new StudentBean();
//学生的id自增的实现。与上面的 全局属性对应
st.setS_id(id);
id = id + 1;
System.out.print("姓名:");
String name = input.next();
st.setName(name);
System.out.print("身份证号码:");
String input_id = input.next();
//将身份证号码转为字符数组,方便进行判断操作 ----------------------标准:18位数字,前17位必须是数字,最后一位可以是数字,也可以是字母,字母只能为X
char[] charArray = input_id.toCharArray();
//判断位数是否为18位
if (input_id.length() != 18) {
System.out.println("位数不对!");
//不满足则出现输入
continue loop;
}
//判断是否为数字组成
for (int i = 0; i < charArray.length; i++) {
//精确到前17位
if (i < charArray.length - 1) {
if (charArray[i] >= '0' && charArray[i] <= '9') {
} else {
System.out.println("不是标准身份证格式!请重新输入:");
continue loop;
}
//最后一位可以为数字,或者X
} else if (charArray[i] == 'X' || (charArray[i] >= '0' && charArray[i] <= '9')) {
System.out.println("输入格式正确!");
//输入正确之后判断此身份证是否已经添加过
Iterator iterator1 = list_st.iterator();
while (iterator1.hasNext()) {
StudentBean next = iterator1.next();
//身份证号码不可能会出现重复的情况
if (input_id.equals(next.getId_card())) {
System.out.println("此人已经存在,请重新输入:");
continue loop;
}
}
//执行到这里说明身份证号码满足要求,便可放入对象中
st.setId_card(input_id);
}
}
// 地址
int nativePlaceCode = Integer.parseInt(input_id.substring(0, 6)); // 身份证前六位
// 调用NativePlace.getNativePlace方法,将nativePlaceCode参数传递进去,进行地域判断
String nativePlace = NativePlace.getNativePlace(nativePlaceCode); // 地址
st.setAddres(nativePlace);
String substring = input_id.substring(6, 10); // 出生年份
int i = Integer.parseInt(substring);
// 获取当前年份 得出最新年龄
Calendar cl = Calendar.getInstance();
int year = cl.get(Calendar.YEAR);
int age = year - i;
st.setAge(age);
//获取身份证上面的日期信息
String brith_m = input_id.substring(10, 12); // 出生日期
String brith_d = input_id.substring(12, 14);
//利用拼接来改变成我们想要的格式
String brith = substring + "年" + brith_m + "月" + brith_d + "日";
st.setDate(brith);
// 性别----------倒数第二位奇数为男,偶数为女
int s = Integer.parseInt(input_id.substring(16, 17));
if (s % 2 == 0) {
st.setSex("女");
} else {
st.setSex("男");
}
boolean key = true;
//邮箱格式----qq-后缀为@qq.com,并且前面不含数字,-----163---后缀为@163.com
tiaozhuan1: while (key) {
System.out.println("*********************************");
System.out.println("邮箱:\t\t 1、qq邮箱 \t 2、163邮箱");
int exchange2 = input.nextInt();
switch (exchange2) {
case 1:
tiaozhuan: while (true) {
System.out.print("qq邮箱:");
String mailbox = input.next();
//判断后缀是否为@qq.com
if (mailbox.endsWith("@qq.com")) {
char[] charArray2 = mailbox.toCharArray();
//判断除去后缀剩余的数字里面是否含有字符
for (int j = 0; j < charArray2.length - 7; j++) {
if (charArray2[j] >= '0' && charArray2[j] <= '9') {
System.out.println("添加成功!");
st.setMailbox(mailbox);
key = false;
break tiaozhuan1;
} else {
System.out.println("格式不对,请重新输入:");
continue tiaozhuan;
}
}
} else {
System.out.println("格式不对,请重新输入:");
continue tiaozhuan;
}
}
case 2:
tiaozhuan: while (true) {
System.out.print("163邮箱:");
String mailbox = input.next();
//判断后缀是否为@163.com
if (mailbox.endsWith("@163.com")) {
char[] charArray2 = mailbox.toCharArray();
//判断除去后缀剩余的是否为纯数字
for (int j = 0; j < charArray2.length - 8; j++) {
if (charArray2[j] >= '0' && charArray2[j] <= '9') {
System.out.println("添加成功!");
st.setMailbox(mailbox);
key = false;
break tiaozhuan1;
} else {
System.out.println("格式不对,请重新输入:");
continue tiaozhuan;
}
}
} else {
System.out.println("格式不对,请重新输入:");
continue tiaozhuan;
}
}
default:
continue tiaozhuan1;
}
}
System.out.println("请输入您的个人简介:(\t 以“.”结束)");
//利用可变的StringBuilder来存储个人简介
StringBuilder sbuild = new StringBuilder();
do {
String people = input.next();
//在满足添加的情况下,每一次的输入都添加到StringBuilder对象里面
sbuild.append(people);
//最后输入.的时候,结束此次输入
} while (!(sbuild.substring(sbuild.length() - 1, sbuild.length()).equals(".")));
st.setPersonal_profile(sbuild);
System.out.println("是否继续添加学生信息:1、继续 \t\t 2、退出");
int exchange2 = input.nextInt();
switch (exchange2) {
case 1:
list_st.add(st);
continue loop;
default:
list_st.add(st);
bool = false;
break loop;
}
}
}
@Override
public void getAll() {
boolean bool = true;
loop: while (bool) {
System.out.println("1、查询全部学生信息\t\t 2、按姓名查询学生信息 \t\t 3、返回上一级");
int exchange1 = input.nextInt();
switch (exchange1) {
case 1:
//使用迭代器来遍历集合
Iterator iterator1 = list_st.iterator();
while (iterator1.hasNext()) {
//接受集合返回的的对象并进行输出
StudentBean next = iterator1.next();
System.out.println("************************************************");
System.out.println(next);
}
System.out.println("************************************************");
continue loop;
case 2:
//根据输入的姓名查找学生信息
Iterator iterator2 = list_st.iterator();
System.out.println("请输入要查找的学生信息的姓名:");
String name1 = input.next();
while (iterator2.hasNext()) {
StudentBean next = iterator2.next();
if (name1.equals(next.getName())) {
System.out.println("************************************************");
System.out.println(next);
System.out.println("************************************************");
}
}
continue loop;
default:
bool = false;
break;
}
}
}
@Override
public void UpDate() {
StudentBean st = new StudentBean();
int w = 0;// 用来计算要修改元素在集合中的下标
boolean bol = true;
System.out.println("请输入你要修改的学生的姓名:");
boolean bool = true;
loop: while (bool) {
String name = input.next();
//判断是否含有该名字的学生
Iterator iterator1 = list_st.iterator();
while (iterator1.hasNext()) {
StudentBean next = iterator1.next();
if (name.equals(next.getName())) {
System.out.println(next);
bol = false;
}
}
if (bol) {
System.out.println("没有此人!(1、重新输入\t\t2、返回上一级)");
int exchange4 = input.nextInt();
switch (exchange4) {
case 1:
continue loop;
default:
break loop;
}
}
//当含有此学生时,!bol才会成立
while (!bol) {
System.out.println("输入你要修改的学生id:-----------------------------(ID不可修改)");
int s_id = input.nextInt();
Iterator iterator2 = list_st.iterator();
while (iterator2.hasNext()) {
w += 1;
StudentBean next = iterator2.next();
if (s_id == next.getS_id()) {
System.out.println("************************************************");
//学生ID不可修改,所以保留原有ID
st.setId_card(next.getId_card());
System.out.println(next);
System.out.println("************************************************");
System.out.print("姓名:");
String name1 = input.next();
st.setName(name1);
boolean bool_1 = true;
//进行身份证的修改操作,大部分操作与添加中的一样,此处不做介绍
loop_1: while (bool_1) {
System.out.print("身份证号码:");
String input_id = input.next();
char[] charArray = input_id.toCharArray();
if (input_id.length() != 18) {
System.out.println("位数不对!");
continue loop_1;
}
for (int i = 0; i < charArray.length; i++) {
if (i < charArray.length - 1) {
if (charArray[i] >= '0' && charArray[i] <= '9') {
} else {
System.out.println("不是标准身份证格式!请重新输入:");
continue loop_1;
}
} else if (charArray[i] == 'X' || (charArray[i] >= '0' && charArray[i] <= '9')) {
System.out.println("输入格式正确!");
Iterator iterator3 = list_st.iterator();
while (iterator3.hasNext()) {
StudentBean next2 = iterator3.next();
if (input_id.equals(next2.getId_card())) {
System.out.println("此人已经存在,请重新输入:");
continue loop_1;
}
}
st.setId_card(input_id);
}
}
// 地址
int nativePlaceCode = Integer.parseInt(input_id.substring(0, 6)); // 身份证前六位
// 调用NativePlace.getNativePlace方法,将nativePlaceCode参数传递进去,进行地域判断
String nativePlace = NativePlace.getNativePlace(nativePlaceCode); // 地址
st.setAddres(nativePlace);
String substring = input_id.substring(6, 10); // 出生年份
int i = Integer.parseInt(substring);
// 获取当前年份 得出最新年龄
Calendar cl = Calendar.getInstance();
int year = cl.get(Calendar.YEAR);
// System.out.println(year);
int age = year - i;
st.setAge(age);
String brith_m = input_id.substring(10, 12); // 出生日期
String brith_d = input_id.substring(12, 14);
String brith = substring + "年" + brith_m + "月" + brith_d + "日";
st.setDate(brith);
// 性别
int s = Integer.parseInt(input_id.substring(16, 17));
if (s % 2 == 0) {
st.setSex("女");
} else {
st.setSex("男");
}
break;
}
System.out.println("邮箱:");
// ------------------------邮箱验证--------------------------------
//与添加中的代码大致一样,不做介绍
boolean key = true;
tiaozhuan1: while (key) {
System.out.println("*********************************");
System.out.println("邮箱:\t\t 1、qq邮箱 \t 2、163邮箱");
int exchange2 = input.nextInt();
switch (exchange2) {
case 1:
tiaozhuan: while (true) {
System.out.print("qq邮箱:");
String mailbox = input.next();
if (mailbox.endsWith("@qq.com")) {
char[] charArray2 = mailbox.toCharArray();
for (int j = 0; j < charArray2.length - 7; j++) {
if (charArray2[j] >= '0' && charArray2[j] <= '9') {
System.out.println("添加成功!");
st.setMailbox(mailbox);
key = false;
break tiaozhuan1;
} else {
System.out.println("格式不对,请重新输入:");
continue tiaozhuan;
}
}
} else {
System.out.println("格式不对,请重新输入:");
continue tiaozhuan;
}
}
case 2:
tiaozhuan: while (true) {
System.out.print("163邮箱:");
String mailbox = input.next();
if (mailbox.endsWith("@163.com")) {
char[] charArray2 = mailbox.toCharArray();
for (int j = 0; j < charArray2.length - 8; j++) {
if (charArray2[j] >= '0' && charArray2[j] <= '9') {
System.out.println("添加成功!");
st.setMailbox(mailbox);
key = false;
break tiaozhuan1;
} else {
System.out.println("格式不对,请重新输入:");
continue tiaozhuan;
}
}
} else {
System.out.println("格式不对,请重新输入:");
continue tiaozhuan;
}
}
default:
continue tiaozhuan1;
}
}
// --------------------------------个人信息简介--------------------------------------
System.out.println("请输入您的个人简介:(\t 以“.”结束)");
StringBuilder sbuild = new StringBuilder();
do {
String people = input.next();
sbuild.append(people);
} while (!(sbuild.substring(sbuild.length() - 1, sbuild.length()).equals(".")));
st.setPersonal_profile(sbuild);
// ----------------------------------------------------------------------------------
list_st.set(w - 1, st);
System.out.println("修改完成!");
break loop;
}
}
}
} // 大while的
}
@Override
public void delete() {
boolean bol=true;
int w = 0;// 记录是否遍历到集合的最后一个元素
int k = 0;// 记录删除学生的位置
int i = 0;//记录删除学生的位置
Iterator iterator2 = list_st.iterator();
System.out.println("请输入要删除的学生信息的姓名:");
String name1 = input.next();
//遍历集合
while (iterator2.hasNext()) {
boolean bool = true;
w += 1;
//比较是否含有该name学生
StudentBean next = iterator2.next();
if (name1.equals(next.getName())) {
bool = false;
}
//如果存在的话就输出,出现一个同名的输出一次
if (!bool) {
System.out.println(next);
}
//当遍历完集合之后,根据ID删除学生信息,可能有多个同名的学生,但是ID是不一样的
if (w == list_st.size()) {
System.out.print("输入要删除学生的ID:");
int st_id = input.nextInt();
//再次遍历集合,寻找需要删除的学生的ID
Iterator iterator3 = list_st.iterator();
while (iterator3.hasNext()) {
StudentBean next2 = iterator3.next();
if (st_id == next2.getS_id()) {
//记录要删除学生的ID
i = k;
bol=false;
}
k += 1;
}
}
}
//妈祖此条件,说明找到ID对应的学生了,此时便可以根据下标删除学生了
if (!bol) {
list_st.remove(i);
} else {
System.out.println("不存在此ID!");
}
}
这是部分操作的实现,里面用到了架包,来实现身份证前六位得到地址。
思想:用集合来存储学生和账号对象,打破了数组长度不可变的局限。