宠物管理系统(2):utils
这篇文章介绍写这个系统中使用到的工具类。
验证码生成器
package com.wzb.utils.captcha;
import java.util.Random;
public class CaptchaUtils {
// 定义包含数字和大小写字母的字符集
private static final String CHAR_POOL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
// 生成指定长度的验证码
public static String generateCode(int length) {
StringBuilder code = new StringBuilder();
Random random = new Random();
// 从字符池中随机选取字符
for (int i = 0; i < length; i++) {
int index = random.nextInt(CHAR_POOL.length());
code.append(CHAR_POOL.charAt(index));
}
return code.toString();
}
}
md5密码加密工具
package com.wzb.utils.md5;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Util {
// MD5加密方法
public static String md5(String input) {
try {
// 创建 MessageDigest 实例,指定 MD5 算法
MessageDigest md = MessageDigest.getInstance("MD5");
// 更新 MessageDigest 实例,提供待加密的数据
md.update(input.getBytes());
// 进行加密,得到字节数组
byte[] digest = md.digest();
// 转换为十六进制字符串
StringBuilder hexString = new StringBuilder();
for (byte b : digest) {
// 将每个字节转为两位的十六进制数
String hex = Integer.toHexString(0xFF & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
// 返回加密后的结果
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null; // 如果出现异常,返回 null
}
}
}
控制台等待工具
package com.wzb.utils.wait;
public class Wait {
public static void waitMoments() throws InterruptedException {
for (int i = 0; i < 6; i++) {
System.out.print(".");
Thread.sleep(500);
}
System.out.println();
}
}
各种显示的菜单
主菜单
package com.wzb.utils.menu;
public class MainMenu {
public static void showMainMenu() {
System.out.println("\t\t\t\t\tWelcome to pet system");
System.out.println("\t\t\t************************************");
System.out.println("\t\t\t\t\t 请输入数字选择:");
System.out.println("\t\t\t\t\t 1.用户操作");
System.out.println("\t\t\t\t\t 2.宠物操作");
System.out.println("\t\t\t\t\t 0.退出系统");
System.out.println("输入你的选择-->");
}
}
用户登录菜单
package com.wzb.utils.menu.user;
public class UserLoginMenu {
public static void showUserLoginMenu() {
System.out.println("\t\t\t\t\tWelcome to user login system");
System.out.println("\t\t\t*******************************************");
System.out.println("\t\t\t\t\t 请输入数字选择:");
System.out.println("\t\t\t\t\t 1.登录");
System.out.println("\t\t\t\t\t 2.注册");
System.out.println("\t\t\t\t\t 0.返回上级");
System.out.println("输入你的选择-->");
}
}
用户主菜单
package com.wzb.utils.menu.user;
public class UserMenu {
public static void showUserMenu() {
System.out.println("\t\t\t\t\tWelcome to User system");
System.out.println("\t\t\t************************************");
System.out.println("\t\t\t\t\t 请输入数字选择:");
System.out.println("\t\t\t\t\t 1.个人信息");
System.out.println("\t\t\t\t\t 2.浏览宠物");
System.out.println("\t\t\t\t\t 3.账户充值");
System.out.println("\t\t\t\t\t 4.修改密码");
System.out.println("\t\t\t\t\t 5.配送地址");
System.out.println("\t\t\t\t\t 6.注销账号");
System.out.println("\t\t\t\t\t 0.退出登录");
System.out.println("输入你的选择-->");
}
}
宠物主菜单
package com.wzb.utils.menu.pet;
public class PetMenu {
public static void showPetMenu() {
System.out.println("\t\t\t\t\tWelcome to pet system");
System.out.println("\t\t\t************************************");
System.out.println("\t\t\t\t\t 请输入数字选择:");
System.out.println("\t\t\t\t\t 1.查看所有宠物");
System.out.println("\t\t\t\t\t 2.新增一个宠物");
System.out.println("\t\t\t\t\t 3.删除一个宠物");
System.out.println("\t\t\t\t\t 4.修改一个宠物");
System.out.println("\t\t\t\t\t 5.查看一个宠物");
System.out.println("\t\t\t\t\t 0.返回上一级");
System.out.println("输入你的选择-->");
}
}
商店菜单
package com.wzb.utils.menu.shop;
public class ShopMenu {
public static void showShopMenu() {
System.out.println("\t\t\t\t\tWelcome to shop system");
System.out.println("\t\t\t************************************");
System.out.println("\t\t\t\t\t 请输入数字选择:");
System.out.println("\t\t\t\t\t 1.查看所有宠物");
System.out.println("\t\t\t\t\t 2.添加购物车");
System.out.println("\t\t\t\t\t 3.查看购物车");
System.out.println("\t\t\t\t\t 4.结算");
System.out.println("\t\t\t\t\t 0.返回上一级");
System.out.println("输入你的选择-->");
}
}
管理员菜单
package com.wzb.utils.menu.admin;
public class AdminMenu {
public static void showAdminMenu() {
System.out.println("\t\t\t\t\tWelcome to admin system");
System.out.println("\t\t\t************************************");
System.out.println("\t\t\t\t\t 请输入数字选择:");
System.out.println("\t\t\t\t\t 1.查看所有用户");
System.out.println("\t\t\t\t\t 2.删除一个用户");
System.out.println("\t\t\t\t\t 3.禁用一个用户");
System.out.println("\t\t\t\t\t 4.修改一个用户");
System.out.println("\t\t\t\t\t 5.查看一个用户");
System.out.println("\t\t\t\t\t 6.查看管理员信息");
System.out.println("\t\t\t\t\t 0.退出登录");
System.out.println("输入你的选择-->");
}
}