//注册用户
public static void register() {
System.out.println("请输入你的账号名");
//String account用于接受用户输入的账号
String account = sc.next();
System.out.println("请输入密码");
//String password用于接受用户输入的密码
String password = sc.next();
//储存用户的账号密码
UserPeople user = new UserPeople(account, password);
list.add(user);
//提示注册成功
System.out.println("注册成功!");
serialize();
}
实现注册功能时有以下几点需注意:1,输入信息后需要有一个集合来储存,这样就有利于其他功能的实现(如:修改,添加,删除,登录等),注意如果使用多维数组来接收不便于实现(如:修改,添加,删除,登录等)
2,最后需要序列化(其实就是保存数据),如果不序列化,一旦关闭系统(或关闭窗口),所有的数据都将消失.就相当于没有保存数据,电脑回收站自动清理数据.
//登录用户
public static void login() {
for (int i = 0; i < 3; i++) {
LoginRegusterSystem2 log=new LoginRegusterSystem2();
System.out.println("请输入帐号");
//Account用于接受用户输入的账号
log.account = sc.next();
System.out.println("请输入密码");
//Password用于接受用户输入的密码
log.password = sc.next();
//账号密码匹配
UserPeople user = new UserPeople();
boolean ss=GetIndex(log.account, log.password);
if (ss) {
System.out.println("登录成功");
break;
} else {
if (i == 2) {
System.out.println("输入错误次数太多,请联系管理人员!");
} else {
System.out.println("登陆失败,还剩" + (3 - i - 1) + "次机会!");
}
}
}
}
public static boolean GetIndex(String Account,String Password) {
for (int i = 0; i <= list.size() - 1; i++) {
UserPeople user = list.get(i);
if ( Account.equals(user.getAccount())&&Password.equals(user.getPassword())) {
return true;
}
}
return false;
}
实现登录功能注意事项:1,Sting类型进行equals,需要重写equals,因为String对比的是内存的地址,所以不能用"==",而int可以用"==",这是Stingh 和int类型对比的区别,
//更改用户信息
//4,定义一个方法来获取某一个学生在集合中的索引
public static int getIndex(String Account) {
int index = -1;
for (int i = 0; i <= list.size() - 1; i++) {
UserPeople user = list.get(i);
if ( Account.equals(user.getAccount())) {
index = i;
}
}
return index;
}
public static void update() {
System.out.println("欢迎进入修改页面");
System.out.println("请输入账号");
String account=sc.next();
int index=getIndex(account);
if(index==-1){
System.out.println("===========该账号不存在=========");
}else{
System.out.println("===========请输入新密码!==========");
String password=sc.next();
UserPeople peo=new UserPeople(account,password);
list.set(index,peo);
System.out.println("=======密码修改成功!========");
}
}
//删除用户信息
public static void delete() {
System.out.println("==========欢迎进入删除页面==========");
System.out.println("==========请输入删除账号=========");
String account=sc.next();
int index=getIndex(account);
if(index==-1){
System.out.println("==============该账号不存在,请重新输入==========");
}else{
list.remove(index);
System.out.println("============账号删除成功!=============");
}
}
修改和删除注意事项:1,需要找到需要删除修改用户信息在集合位置下的索引,需要定义一个方法来获取索引,通过索引来进行定位从而进行修改和删除
2,String类型记得用重写equals后的方法
int类型用"=="
3.利用集合中的 remove(index)来删除对应索引的信息(前提是知道索引)
//序列化,保存数据
public static void serialize() {
System.out.println("=================进行内存到硬盘的输入,序列化===================");
try {
ObjectOutputStream ob = new ObjectOutputStream(new FileOutputStream(path));
ob.writeObject(list);
ob.flush();
ob.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println("=============序列化成功!================");
}
//反序列化,读取信息
public static void deserialize () {
System.out.println("==================欢迎进入读取界面,进行反序列化!==================");
try {
ObjectInputStream obs=new ObjectInputStream(new FileInputStream(path));
list = (ArrayList<UserPeople>) obs.readObject();
for (UserPeople user1 : list) {
System.out.println(user1);
}
obs.close();
System.out.println("=============反序列化成功!==============");
} catch (IOException e) {
} catch (ClassNotFoundException e) {
}
}
序列化:ObjectOutputStream ob = new ObjectOutputStream(new FileOutputStream(path));
ob.writeObject(list);
ob.flush();
ob.close();
反序列化:ObjectInputStream obs=new ObjectInputStream(new FileInputStream(path));
list = (ArrayList<UserPeople>) obs.readObject();
for (UserPeople user1 : list) {
System.out.println(user1);
}
obs.close();