宠物管理系统:Service层

LoginService

java 复制代码
package com.wzb.service;

import com.wzb.bean.User;

public interface LoginService {
    User login();
    boolean register();
}

LoginServiceImpl

java 复制代码
package com.wzb.service.impl;

import com.wzb.bean.User;
import com.wzb.dao.UserDao;
import com.wzb.dao.impl.UserDaoImpl;
import com.wzb.service.LoginService;
import com.wzb.utils.captcha.CaptchaUtils;
import com.wzb.utils.md5.MD5Util;

import java.util.ArrayList;
import java.util.Scanner;

public class LoginServiceImpl implements LoginService {
    private final Scanner sc = new Scanner(System.in);
    private final UserDao userDao = new UserDaoImpl();

    @Override
    public User login() {
        ArrayList<User> userList = userDao.getAllUser();
        if (userList.isEmpty()) {
            System.out.println("user is not exist");
            return null;
        }
        System.out.println("Welcome login!");
        System.out.println("please input username");
        String username = sc.nextLine();
        for (User user : userList) {
            if (user.getUsername().equals(username)) {
                System.out.println("please input password");
                String password = sc.nextLine();
                String realPassword = MD5Util.md5(password);
                if (realPassword.equals(user.getPassword())) {
                    String captcha = CaptchaUtils.generateCode(6);
                    System.out.println("captcha is: " + captcha);
                    System.out.println("please input captcha: ");
                    String s = sc.nextLine();
                    if (s.equalsIgnoreCase(captcha)) {
                        System.out.println("login successfully");
                        return user;
                    } else {
                        System.out.println("error captcha");
                        return null;
                    }
                } else {
                    System.out.println("error password");
                    return null;
                }
            }
        }
        System.out.println("username not found");
        return null;
    }

    public boolean register() {
        System.out.println("Welcome to register");
        //TODO 日后需要检查用户名和密码的合法性
        System.out.println("please input your username");
        String username = sc.nextLine();
        System.out.println("please input your password");
        String password = sc.nextLine();
        String realPassword = MD5Util.md5(password);
        System.out.println("please input your phoneNumber");
        String phoneNumber = sc.nextLine();
        User user = new User(userDao.getNextId(), username, realPassword, phoneNumber);
        userDao.insert(user);
        return true;
    }
}

UserService

java 复制代码
package com.wzb.service;

import com.wzb.bean.User;

/**
 * 用户服务
 */
public interface UserService {
    User userLogin();

    boolean userRegister();

    void showUser(User user);

    boolean reCharge(User user);

    boolean changePassword(User user);

    boolean changeAddress(User user);

    boolean deleteMyself(User user);
}

UserServiceImpl

java 复制代码
package com.wzb.service.impl;

import com.wzb.bean.Pet;
import com.wzb.bean.User;
import com.wzb.dao.UserDao;
import com.wzb.dao.impl.UserDaoImpl;
import com.wzb.service.LoginService;
import com.wzb.service.PetService;
import com.wzb.service.UserService;
import com.wzb.utils.captcha.CaptchaUtils;
import com.wzb.utils.md5.MD5Util;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Scanner;

public class UserServiceImpl implements UserService {
    private final LoginService login = new LoginServiceImpl();
    private final PetService petService = new PetServiceImpl();
    private final UserDao userDao = new UserDaoImpl();
    private final Scanner sc = new Scanner(System.in);

    @Override
    public User userLogin() {
        return login.login();
    }

    @Override
    public boolean userRegister() {
        return login.register();
    }

    @Override
    public void showUser(User user) {
        System.out.println("当前登录用户信息为:");
        System.out.println("用户名:" + user.getUsername());
        System.out.println("账户余额为:" + user.getMoney());
        System.out.println("地址为:" + user.getAddress());
        ArrayList<Pet> petList = new ArrayList<>();
        ArrayList<Integer> petIdList = user.getPetList();
        for (Integer id : petIdList) {
            petList.add(petService.getById(id));
        }
        System.out.println("该用户的宠物有:" + petList);
        System.out.println("该用户的订单有:" + user.getOrderList());
        System.out.println();
    }

    @Override
    public boolean reCharge(User user) {
        System.out.println(user.getUsername() + "的账户余额是" + user.getMoney());
        System.out.println("请输入想要充值的金额-->");
        int money = Integer.parseInt(sc.nextLine());
        user.addMoney(money);
        System.out.println("充值成功");
        user.setUpdateTime(LocalDateTime.now());
        System.out.println(user.getUsername() + "的账户余额是" + user.getMoney());
        userDao.saveUpdatedUserById(user);  // 更新后立刻保存
        return true;
    }

    @Override
    public boolean changePassword(User user) {
        System.out.println("修改密码,若非本人操作,请立即冻结账号");
        System.out.println("请输入手机号码");
        String phoneNumber = sc.nextLine();
        if (phoneNumber.equals(user.getPhoneNumber())) {
            String captcha = CaptchaUtils.generateCode(6);
            System.out.println("验证码是:" + captcha);
            System.out.println("请输入验证码");
            String s = sc.nextLine();
            if (s.equalsIgnoreCase(captcha)) {
                System.out.println("请输入新密码");
                String password = sc.nextLine();
                System.out.println("请确认密码");
                String p = sc.nextLine();
                if (p.equals(password)) {
                    String realPassword = MD5Util.md5(password);
                    user.setPassword(realPassword);
                    user.setUpdateTime(LocalDateTime.now());
                    userDao.saveUpdatedUserById(user);  // 更新后立刻保存
                    return true;

                } else {
                    System.out.println("两次密码不一致,修改失败");
                    return false;
                }
            } else {
                System.out.println("验证码不一致,修改密码失败");
                return false;
            }
        } else {
            System.out.println("电话不匹配,修改密码失败");
            return false;
        }
    }

    @Override
    public boolean changeAddress(User user) {
        System.out.println("请输入用户的地址");
        String address = sc.nextLine();
        user.setAddress(address);
        user.setUpdateTime(LocalDateTime.now());
        System.out.println("用户" + user.getUsername() + "的地址是" + user.getAddress());
        userDao.saveUpdatedUserById(user);  // 更新后立刻保存
        return true;
    }

    @Override
    public boolean deleteMyself(User user) {
        System.out.println("你确定要注销本账户吗?");
        System.out.println("该操作无法复原!");
        System.out.println("请输入"我想注销 + 用户名"继续");
        String answer = "我想注销" + user.getUsername();
        String input = sc.nextLine();
        if (answer.equals(input)) {
            userDao.deleteUserById(user.getId());  // 删除后立刻保存
            return true;
        } else {
            return false;
        }
    }
}
相关推荐
咖啡教室5 小时前
java日常开发笔记和开发问题记录
java
咖啡教室5 小时前
java练习项目记录笔记
java
鱼樱前端5 小时前
maven的基础安装和使用--mac/window版本
java·后端
RainbowSea6 小时前
6. RabbitMQ 死信队列的详细操作编写
java·消息队列·rabbitmq
RainbowSea6 小时前
5. RabbitMQ 消息队列中 Exchanges(交换机) 的详细说明
java·消息队列·rabbitmq
我不会编程5558 小时前
Python Cookbook-5.1 对字典排序
开发语言·数据结构·python
李少兄8 小时前
Unirest:优雅的Java HTTP客户端库
java·开发语言·http
此木|西贝8 小时前
【设计模式】原型模式
java·设计模式·原型模式
可乐加.糖8 小时前
一篇关于Netty相关的梳理总结
java·后端·网络协议·netty·信息与通信
无名之逆8 小时前
Rust 开发提效神器:lombok-macros 宏库
服务器·开发语言·前端·数据库·后端·python·rust