public class AmountOfMoneyMissMatchException extends RuntimeException {
public AmountOfMoneyMissMatchException() {
super();
}
public AmountOfMoneyMissMatchException(String message) {
super(message);
}
}
public class AmountOfMoneyOutOfBoundException extends RuntimeException {
public AmountOfMoneyOutOfBoundException() {
super();
}
public AmountOfMoneyOutOfBoundException(String message) {
super(message);
}
}
public class NoMatchUserException extends RuntimeException {
public NoMatchUserException() {
super();
}
public NoMatchUserException(String message) {
super(message);
}
}
public class PasswordNotMatchException extends RuntimeException {
public PasswordNotMatchException() {
super();
}
public PasswordNotMatchException(String message) {
super(message);
}
}
业务层接口:
public interface UserService {
User login(Scanner scanner);
void getMoney(User user,Scanner scanner);
void saveMoney(User user,Scanner scanner);
void update(User user,Scanner scanner);
}
接口实现类:
public class UserServiceImpl implements UserService {
@Override
public User login(Scanner scanner) {
User user = new User();
System.out.println("请输入您的用户名:");
user.setName(scanner.next());
System.out.println("请输入您的密码:");
user.setPass(scanner.next());
if (!(UserInfor12.USER_NAME.equals(user.getName())) || !(UserInfor12.USER_PWD.equals(user.getPass()))) {
throw new NoMatchUserException("用户名或密码不匹配");
}
user.setTime(System.currentTimeMillis());
return user;
}
@Override
public void getMoney(User user, Scanner scanner) {
int num = scanner.nextInt();
if (num % 100 != 0) throw new AmountOfMoneyMissMatchException("输入金额不合法");
else if (num > 5000) throw new AmountOfMoneyOutOfBoundException("输入金额超出最大金额");
else if (num > user.getBalance()) throw new AmountOfMoneyOutOfBoundException("余额不足");
System.out.println("取款成功");
user.setBalance(user.getBalance() - num);
}
@Override
public void saveMoney(User user, Scanner scanner) {
int num = scanner.nextInt();
if (num % 100 != 0) throw new AmountOfMoneyMissMatchException("输入金额不合法");
else if (num > 10000) throw new AmountOfMoneyOutOfBoundException("输入金额超出最大金额");
System.out.println("存款成功");
user.setBalance(user.getBalance() + num);
}
@Override
public void update(User user, Scanner scanner) {
System.out.println("请输入原密码:");
String pass;
int n = 3;
while (n > 0) {
n--;
if (n == 0) throw new PasswordNotMatchException("密码输入错误超过三次,已返回上一级");
pass = scanner.next();
if (!UserInfor12.USER_PWD.equals(pass)) System.out.println("输入密码不正确");
if (UserInfor12.USER_PWD.equals(pass)) {
System.out.println("请输入修改后的密码");
pass = scanner.next();
if (UserInfor12.USER_PWD.equals(pass)) throw new PasswordNotMatchException("修改前后密码不能一致");
}
}
}
}
标签:scanner,方式,User,num,user,new,异常,public,自定义
From: https://www.cnblogs.com/Liku-java/p/16919520.html