销户操作的基本要求
- 销户就是从系统中删除当前账户,销户的要求:
- 首先要询问用户是否确定销户,否,返回操作界面
1.询问用户是否确定要销户
System.out.println("是否确定要注销当前账户?y/n");
String command = sc.next();
switch (command) {
case "y":
2. 如果确定,要判断用户的帐户中是否有钱,有,不允许销户,返回操作页面;
判断账户中是否有钱
if (loginAccount.getMoney() == 0) {
} else {
System.out.println("账户存在余额,不能注销");
return false;
}
3. 没钱,完成销户,回到欢迎页
if (loginAccount.getMoney() == 0) {
accounts.remove(loginAccount);
System.out.println("注销成功");
return true;
}
4. 完整代码
private boolean deleteAccount() {
System.out.println("----注销当前账户----");
System.out.println("是否确定要注销当前账户?y/n");
String command = sc.next();
switch (command) {
case "y":
if (loginAccount.getMoney() == 0) {
accounts.remove(loginAccount);
System.out.println("注销成功");
return true;
} else {
System.out.println("账户存在余额,不能注销");
return false;
}
default:
System.out.println("您的账户被保留");
return false;
}
}
密码修改
- 就是更改账户密码,修改密码的要求:
- 需要先认证用户当前的密码
2.认证当前密码是否正确
if (loginAccount.getPassword().equals(password)) {
2. 认证通过后,需要让用户输入2次新密码
while (true) {
System.out.println("请输入新密码:");
String newPassword = sc.next();
System.out.println("请输入确认密码:");
String confirmNewPassword = sc.next();
3. 两次密码一样,则更新账户密码,应返回欢迎界面
if (newPassword.equals(confirmNewPassword)) {
loginAccount.setPassword(newPassword);
System.out.println("密码修改成功");
return;
} else {
System.out.println("两次密码不一致");
}
4. 完整代码
private void updatePassword() {
System.out.println("--修改密码--");
while (true) {
System.out.println("请输入当前密码:");
String password = sc.next();
if (loginAccount.getPassword().equals(password)) {
while (true) {
System.out.println("请输入新密码:");
String newPassword = sc.next();
System.out.println("请输入确认密码:");
String confirmNewPassword = sc.next();
if (newPassword.equals(confirmNewPassword)) {
loginAccount.setPassword(newPassword);
System.out.println("密码修改成功");
return;
} else {
System.out.println("两次密码不一致");
}
}
} else {
System.out.println("您当前输入的密码错误");
}
}
}