宠物管理系统: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;
        }
    }
}
相关推荐
Crossoads8 分钟前
【汇编语言】端口 —— 「从端口到时间:一文了解CMOS RAM与汇编指令的交汇」
android·java·汇编·深度学习·网络协议·机器学习·汇编语言
老马啸西风9 分钟前
NLP 中文拼写检测纠正论文-02-2019-SOTA FASPell Chinese Spell Checke github 源码介绍
java
向宇it10 分钟前
【从零开始入门unity游戏开发之——C#篇26】C#面向对象动态多态——接口(Interface)、接口里氏替换原则、密封方法(`sealed` )
java·开发语言·unity·c#·游戏引擎·里氏替换原则
@菜鸟进阶记@14 分钟前
java根据Word模板实现动态填充导出
java·开发语言
卖芒果的潇洒农民16 分钟前
Lecture 6 Isolation & System Call Entry
java·开发语言
SomeB1oody37 分钟前
【Rust自学】6.1. 定义枚举
开发语言·后端·rust
SomeB1oody43 分钟前
【Rust自学】5.3. struct的方法(Method)
开发语言·后端·rust
Amarantine、沐风倩✨1 小时前
设计一个监控摄像头物联网IOT(webRTC、音视频、文件存储)
java·物联网·音视频·webrtc·html5·视频编解码·七牛云存储
Kisorge2 小时前
【C语言】指针数组、数组指针、函数指针、指针函数、函数指针数组、回调函数
c语言·开发语言
路在脚下@2 小时前
spring boot的配置文件属性注入到类的静态属性
java·spring boot·sql