模仿三层架构中的Controller编写的控制器(但是这并非是真的),用于处理不同的情境。
MainController,用于控制整个主程序:
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,用于控制模拟用户登录:
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,用于用户管理:
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,用于管理员:
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,用于宠物管理:
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,用于商店管理:
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("选择错误,请重新选择");
}
}
}
}
}
标签:管理系统,com,宠物,System,Controller,println,import,wzb,out From: https://blog.csdn.net/Aishangyuwen/article/details/144653801