宠物管理系统(3):Controller类

embedded/2024/12/23 8:04:14/

        模仿三层架构中的Controller编写的控制器(但是这并非是真的),用于处理不同的情境。

        MainController,用于控制整个主程序:

java">package com.wzb.controller;import com.wzb.utils.menu.MainMenu;
import com.wzb.utils.wait.Wait;import java.util.Scanner;/*** 主程序*/
public class MainController {private static final Scanner sc = new Scanner(System.in);public static void main(String[] args) throws InterruptedException {while (true) {MainMenu.showMainMenu();int choice = Integer.parseInt(sc.nextLine());switch (choice) {case 1: {LoginController.userLogin();break;}case 2: {PetController.modifyPet();break;}case 0: {System.out.println("感谢您使用本系统,欢迎再次使用");Thread.sleep(500);System.out.print("系统正在退出");Wait.waitMoments();System.exit(0);}default: {System.out.println("选择错误,请再次选择");}}}}
}

        LoginController,用于控制模拟用户登录:

java">package com.wzb.controller;import com.wzb.bean.User;
import com.wzb.service.UserService;
import com.wzb.service.impl.UserServiceImpl;
import com.wzb.utils.menu.user.UserLoginMenu;
import com.wzb.utils.wait.Wait;import java.util.Scanner;public class LoginController {private static final Scanner sc = new Scanner(System.in);private static final UserService userService = new UserServiceImpl();public static void userLogin() throws InterruptedException {boolean flag = true;while (flag) {UserLoginMenu.showUserLoginMenu();int choice = Integer.parseInt(sc.nextLine());switch (choice) {case 1: {User user = userService.userLogin();if (user == null) {System.out.println("用户不存在,登录失败,请重新登录");} else if (user.getId().equals(0)) {System.out.println("管理员:wzb,欢迎登录");AdminController.modifyUser(user);} else {if (user.getStatus()) {System.out.println(user.getUsername() + "欢迎登录");UserController.modifyUser(user);} else {System.out.println("用户被锁定,不可登录");}}break;}case 2: {if (userService.userRegister()) {System.out.println("注册成功");} else {System.out.println("请重新注册");}break;}case 0: {System.out.println("正在返回上一级");Thread.sleep(500);System.out.print("");Wait.waitMoments();System.out.println("成功返回");flag = false;break;}default: {System.out.println("选择错误,请再次选择");}}}}
}

        UserController,用于用户管理:

java">package com.wzb.controller;import com.wzb.bean.User;
import com.wzb.service.UserService;
import com.wzb.service.impl.UserServiceImpl;
import com.wzb.utils.menu.user.UserMenu;
import com.wzb.utils.wait.Wait;import java.util.Scanner;public class UserController {private static final Scanner sc = new Scanner(System.in);private static final UserService userService = new UserServiceImpl();public static void modifyUser(User user) throws InterruptedException {boolean flag = true;while (flag) {UserMenu.showUserMenu();int choice = Integer.parseInt(sc.nextLine());switch (choice) {case 1: {userService.showUser(user);break;}case 2: {ShopController.shop(user);break;}case 3: {if (userService.reCharge(user)) {System.out.println("充值成功");} else {System.out.println("充值失败");}break;}case 4: {if (userService.changePassword(user)) {System.out.println("修改密码成功");} else {System.out.println("修改密码失败");}break;}case 5: {if (userService.changeAddress(user)) {System.out.println("修改地址成功");} else {System.out.println("修改地址失败");}break;}case 6: {if (userService.deleteMyself(user)) {System.out.print("账户已注销,正在回到登录界面");Wait.waitMoments();flag = false;} else {System.out.println("账户注销失败");}break;}case 0: {System.out.print("退出账户" + user.getUsername());Wait.waitMoments();flag = false;}default: {System.out.println("选择错误,请重新选择");}}}}}

        AdminController,用于管理员:

java">package com.wzb.controller;import com.wzb.bean.User;
import com.wzb.service.AdminService;
import com.wzb.service.impl.AdminServiceImpl;
import com.wzb.utils.menu.admin.AdminMenu;
import com.wzb.utils.wait.Wait;import java.util.List;
import java.util.Scanner;public class AdminController {private static final Scanner sc = new Scanner(System.in);private static final AdminService adminService = new AdminServiceImpl();public static void modifyUser(User user) throws InterruptedException {boolean flag = true;while (flag) {AdminMenu.showAdminMenu();int choice = Integer.parseInt(sc.nextLine());switch (choice) {case 1: {List<User> userList = adminService.showAllUser();for (User u : userList) {System.out.println(u);}break;}case 2: {System.out.println("请输入想要删除的用户id");int id = Integer.parseInt(sc.nextLine());if (adminService.deleteUserById(id)) {System.out.println("删除成功");} else {System.out.println("删除失败");}break;}case 3: {System.out.println("请输入想要更改状态的用户id");int id = Integer.parseInt(sc.nextLine());if (adminService.changeUserStatus(id)) {System.out.println("更改状态成功");} else {System.out.println("更改状态失败");}break;}case 4: {System.out.println("请输入想要更新的用户id");int id = Integer.parseInt(sc.nextLine());if (adminService.updateAUser(id)) {System.out.println("用户更新成功");} else {System.out.println("用户更新失败");}break;}case 5: {System.out.println("请输入想要查看的用户id");int id = Integer.parseInt(sc.nextLine());User u = adminService.getById(id);System.out.println(u);break;}case 6: {adminService.showAdmin(user);break;}case 0: {System.out.print("退出管理员账户" + user.getUsername());Wait.waitMoments();flag = false;break;}default: {System.out.println("选择错误,请重新选择");}}}}
}

        PetController,用于宠物管理:

java">package com.wzb.controller;import com.wzb.bean.Pet;
import com.wzb.service.PetService;
import com.wzb.service.impl.PetServiceImpl;
import com.wzb.utils.menu.pet.PetMenu;
import com.wzb.utils.wait.Wait;import java.util.List;
import java.util.Scanner;public class PetController {private static final Scanner sc = new Scanner(System.in);private static final PetService petService = new PetServiceImpl();public static void modifyPet() throws InterruptedException {boolean flag = true;while (flag) {PetMenu.showPetMenu();int choice = Integer.parseInt(sc.nextLine());switch (choice) {case 1: {List<Pet> petList = petService.getAllPet();for (Pet pet : petList) {System.out.println(pet);}break;}case 2: {if (petService.insertPet()) {System.out.println("插入成功");} else {System.out.println("插入失败");}break;}case 3: {System.out.println("请输入想要删除的宠物id");int id = Integer.parseInt(sc.nextLine());if (petService.deletePet(id)) {System.out.println("删除成功");} else {System.out.println("删除失败");}break;}case 4: {System.out.println("请输入想要更新的宠物id");int id = Integer.parseInt(sc.nextLine());if (petService.updatePet(id)) {System.out.println("更新成功");} else {System.out.println("更新失败");}break;}case 5: {System.out.println("请输入想要查看的宠物id");int id = Integer.parseInt(sc.nextLine());Pet pet = petService.getById(id);System.out.println(pet);break;}case 0: {System.out.println("正在返回上一级");Thread.sleep(500);System.out.print("");Wait.waitMoments();System.out.println("成功返回");flag = false;break;}default: {System.out.println("选择错误,请重新选择");}}}}
}

        ShopController,用于商店管理:

java">package com.wzb.controller;import com.wzb.bean.User;
import com.wzb.service.ShopService;
import com.wzb.service.impl.ShopServiceImpl;
import com.wzb.utils.menu.shop.ShopMenu;
import com.wzb.utils.wait.Wait;import java.util.Scanner;public class ShopController {private static final Scanner sc = new Scanner(System.in);private static final ShopService shopService = new ShopServiceImpl();public static void shop(User user) throws InterruptedException {boolean flag = true;while (flag) {ShopMenu.showShopMenu();int choice = Integer.parseInt(sc.nextLine());switch (choice) {case 1: {shopService.showAllPet();break;}case 2: {if (shopService.addShopCar(user)) {System.out.println("添加购物车成功");} else {System.out.println("添加购物车失败");}break;}case 3: {shopService.showShopCar(user);break;}case 4: {if (shopService.pay(user)) {System.out.println("支付成功");} else {System.out.println("支付失败");}break;}case 0: {System.out.println("正在返回上一级");Thread.sleep(500);System.out.print("");Wait.waitMoments();System.out.println("成功返回");flag = false;break;}default: {System.out.println("选择错误,请重新选择");}}}}
}

 

 

 

 

 

        


http://www.ppmy.cn/embedded/148022.html

相关文章

RabbitMQ消息可靠性保证机制7--可靠性分析-rabbitmq_tracing插件

rabbitmq_tracing插件 rabbitmq_tracing插件相当于Firehose的GUI版本&#xff0c;它同样能跟踪RabbitMQ中消息的注入流出情况。rabbitmq_tracing插件同样会对流入流出的消息进行封装&#xff0c;然后将封装后的消息日志存入相应的trace文件中。 # 开启插件 rabbitmq-plugins …

项目配置设置 (芒果头条项目进度2)

一 创建项目 1.准备项⽬代码仓库 源码托管⽹站 码云 https://gitee.com/ (国内)Github https://github.com/ (国外)Gitlab(企业中公司⾃⼰搭建) 1.1 创建源码仓库 2.克隆项⽬代码仓库 1. 进⼊本地项⽬⽬录 $ mkdir ~/Desktop/djprojects $ cd djprojects 2. 克隆仓库 $ …

牛客网 SQL36查找后排序

SQL36查找后排序 select device_id,age from user_profile order by age asc #select [字段1,字段2] from [表名] order by [字段1] [升序(asc)/降序(desc)],[字段2] [升序(asc)/降序(desc)] #select&#xff1a;查询 #order by 排序 每日问题 如何实现对象的克隆&#xff1…

2025年前端面试热门题目——HTML|CSS|Javascript|TS知识

以下是对这些 HTML 面试问题的详细解答&#xff1a; 1. HTML 的 src 和 href 属性有什么区别? src (Source) 属性&#xff1a; 用于嵌入资源&#xff0c;例如图像、脚本或 iframe。加载资源时&#xff0c;当前页面的加载会暂停&#xff0c;直到资源加载完成。常用于 <img&g…

Ubuntu2404部署wikijs(非docker方式)

1、安装Nod.js和PostgreSQL sudo apt update sudo apt install -y nodejs sudo apt install postgresql 2、设置数据库 安装后PostgreSQL服务应该已经自动启动了&#xff0c;可以用以下命令检查服务状态 sudo systemctl status postgresql 如果PostgreSQL没有启动&#…

2024年11月 蓝桥杯青少组 STEMA考试 Scratch真题

2024年11月 蓝桥杯青少组 STEMA考试 Scratch真题&#xff08;选择题&#xff09; 题目总数&#xff1a;5 总分数&#xff1a;50 选择题 第 1 题 单选题 Scratch运行以下程宇后&#xff0c;小兔子会&#xff08; &#xff09;。 A. 变小 B. 变大 C. 变色 D. …

docker 软连接修改存储位置

查看docker路径 默认情况下Docker的存放位置为&#xff1a;/var/lib/docker&#xff0c;也可以通过如下命令查看docker存储路径 docker info | grep "Docker Root Dir" 停掉docker服务 systemctl stop docker 移动docker目录 mv /var/lib/docker /var/sda1/docker_…

WPF依赖属性详解

在 WPF 中&#xff0c;依赖属性&#xff08;Dependency Property&#xff09;是一种特殊的属性类型&#xff0c;它提供了比普通属性更强大的功能。依赖属性是 WPF 数据绑定、样式、动画和模板等功能的基础。理解依赖属性是理解 WPF 复杂功能和性能优化的关键。 1. 依赖属性是什…