基于字符页面的收银台系统与测试
程序员文章站
2022-04-02 10:29:02
...
该项目是基于字符页面的收银台系统,可以实现用户和管理员的登录与注册,商品管理,浏览,下单,支付功能
使用了JDBC技术,集合框架,stream流,MySQL,Java的反射和注解等
该系统采用分层设计:
在本系统中选用DateSource进行数据库连接,而非DriverManger
通过输入命令缩写进行操作
系统使用注解和反射的方式完成对各项操作的分类,令管理端和客户端完成不同功能
公共端:
进入系统后,首先显示公共端信息:
import com.omega.command.AbstractCommand;
import com.omega.command.Command;
import com.omega.command.Commands;
import com.omega.command.Subject;
import com.omega.command.annotation.AdminCommand;
import com.omega.command.annotation.CommandMate;
import com.omega.command.annotation.CustomerCommand;
import com.omega.command.annotation.EntranceCommand;
import com.omega.entity.Account;
import java.util.*;
@CustomerCommand
@EntranceCommand
@AdminCommand
@CommandMate(
name = "帮助信息",
desc = "BZXX",
command = "公共命令"
)
public class HelpCommand extends AbstractCommand {
@Override
public void excute(Subject subject) {
Account account = subject.getAccount();
if (account == null){
entrance();
}else {
switch (account.getAccountType()){
case CUSTOMER:customer();
break;
case ADMIN:admin();
break;
default:
}
}
}
public static void entrance(){
design("欢迎使用",Commands.ENTRANCE_COMMANDS.values());
}
public static void customer(){
design("欢迎客户",Commands.CUSTOMER_COMMANDS.values());
}
public static void admin(){
design("欢迎管理员",Commands.ADMIN_COMMANDS.values());
}
public static void design(String title, Collection<Command> collection){
System.out.println("===================="+title+"====================");
Map<String,List<String>> helpInfo = new HashMap<>();
for (Command command : collection){
CommandMate commandMate = command.getClass().getDeclaredAnnotation(CommandMate.class);
String group = commandMate.command();
List<String> fun = helpInfo.get(group);
if (fun == null){
fun = new ArrayList<>();
helpInfo.put(group,fun);
}
fun.add(commandMate.name()+"("+commandMate.desc()+")");
}
int i = 0;
for (Map.Entry<String,List<String>> entry : helpInfo.entrySet()){
i++;
System.out.println(i+"."+entry.getKey());
int j = 0;
for (String string : entry.getValue()){
j++;
System.out.println("\t"+i+"."+j+" "+string);
}
}
System.out.println("输入菜单括号后面的编号(忽略大小写),进行下一步操作");
System.out.println("=========================================================");
}
}
进入公共端后,可以选择注册账号或是登录账号
注册账号:
import com.omega.command.AbstractCommand;
import com.omega.command.Subject;
import com.omega.command.annotation.AdminCommand;
import com.omega.command.annotation.CommandMate;
import com.omega.command.annotation.CustomerCommand;
import com.omega.command.annotation.EntranceCommand;
import com.omega.entity.Account;
import com.omega.type.AccountStatus;
import com.omega.type.AccountType;
import javax.sql.rowset.spi.SyncResolver;
@CustomerCommand
@EntranceCommand
@AdminCommand
@CommandMate(
name = "注册",
desc = "ZC",
command = "入口命令"
)
public class RegisterCommand extends AbstractCommand {
@Override
public void excute(Subject subject) {
final Account account = new Account();
System.out.println("请输入用户名:");
String username = scanner.nextLine();
System.out.println("请输入密码:");
String password = scanner.nextLine();
System.out.println("请确认密码:");
String rePassword = scanner.nextLine();
if (!password.equals(rePassword)){
System.out.println("密码不一致,注册失败!");
return;
}
System.out.println("请输入姓名:");
String name = scanner.nextLine();
System.out.println("请选择账户类型:1.管理员 2.客户");
int type = scanner.nextInt();
AccountType accountType = AccountType.valueof(type);
System.out.println("请选择账号状态:1.启用 2.启停");
int status = scanner.nextInt();
AccountStatus accountStatus = AccountStatus.valueof(status);
account.setUsername(username);
account.setPassword(password);
account.setName(name);
account.setAccountType(accountType);
account.setAccountStatus(accountStatus);
boolean effct = this.accountService.register(account);
if (effct){
System.out.println("注册成功!!!");
}else {
System.out.println("注册失败!!!");
}
}
}
登录账号:
import com.omega.command.AbstractCommand;
import com.omega.command.Subject;
import com.omega.command.annotation.AdminCommand;
import com.omega.command.annotation.CommandMate;
import com.omega.command.annotation.CustomerCommand;
import com.omega.command.annotation.EntranceCommand;
import com.omega.entity.Account;
import com.omega.type.AccountStatus;
@CustomerCommand
@EntranceCommand
@AdminCommand
@CommandMate(
name = "登录",
desc = "DL",
command = "入口命令"
)
public class LoginCommand extends AbstractCommand {
@Override
public void excute(Subject subject) {
Account account = subject.getAccount();
if (account != null){
System.out.println("已登录!!!");
return;
}
System.out.println("请输入用户名:");
String username = scanner.nextLine();
System.out.println("请输入密码:");
String password = scanner.nextLine();
account = this.accountService.login(username,password);
if (account != null && account.getAccountStatus() == AccountStatus.UNLOCK){
System.out.println(account.getAccountType().getDesc()+"登录成功!");
subject.setAccount(account);
}else {
System.out.println("登录失败,用户名或密码错误!");
}
}
}
登录管理员账号后
管理端:
在管理端可以进行如下操作:
启停账号:
import com.omega.command.AbstractCommand;
import com.omega.command.Subject;
import com.omega.command.annotation.AdminCommand;
import com.omega.command.annotation.CommandMate;
import com.omega.entity.Account;
@AdminCommand
@CommandMate(
name = "启停账号",
desc = "QTZH",
command = "账号信息"
)
public class AccountStatusCommand extends AbstractCommand {
@Override
public void excute(Subject subject) {
System.out.println("请输入需要改变状态的账号名:");
String accountName = scanner.next();
Account account = this.accountService.getAccountByName(accountName);
if (account == null){
System.out.println("没有该用户");
}else {
System.out.println("请输入需要改变的状态:");
int status = scanner.nextInt();
if (status == account.getAccountStatus().getFlag()){
System.out.println("状态未改变");
}else {
boolean effect = this.accountService.changeStatus(accountName,status);
if (effect){
System.out.println("状态改变成功");
}else {
System.out.println("状态改变失败");
}
}
}
}
}
修改密码:
import com.omega.command.AbstractCommand;
import com.omega.command.Subject;
import com.omega.command.annotation.AdminCommand;
import com.omega.command.annotation.CommandMate;
import com.omega.entity.Account;
import org.apache.commons.codec.digest.DigestUtils;
@AdminCommand
@CommandMate(
name = "重置密码",
desc = "CZMM",
command = "账号信息"
)
public class AccountPasswordCommand extends AbstractCommand {
@Override
public void excute(Subject subject) {
System.out.println("===========修改密码============");
System.out.println("请输入需要修改密码的账号:");
String accountName = scanner.next();
Account account = this.accountService.getAccountByName(accountName);
if (account == null){
System.out.println("没有该用户");
}else {
System.out.println("请输入原始密码:");
String password = scanner.next();
if (DigestUtils.md5Hex(password).equals(account.getPassword())){
System.out.println("请输入修改后的密码:");
String rePassword = scanner.next();
if (DigestUtils.md5Hex(rePassword).equals(account.getPassword())){
System.out.println("与原密码相等");
}else {
System.out.println("请确认密码:");
String repsd = scanner.next();
if (rePassword.equals(repsd)) {
boolean effect = this.accountService.atlerPassword(accountName, rePassword);
if (effect) {
System.out.println("修改密码成功");
} else {
System.out.println("修改密码失败");
}
}
}
}else {
System.out.println("与该账户原始密码不符");
}
}
}
}
查看账号:
import com.omega.command.AbstractCommand;
import com.omega.command.Subject;
import com.omega.command.annotation.AdminCommand;
import com.omega.command.annotation.CommandMate;
import com.omega.entity.Account;
import java.util.List;
@AdminCommand
@CommandMate(
name = "查看账号",
desc = "CKZH",
command = "账号信息"
)
public class AccountBrowerCommand extends AbstractCommand {
@Override
public void excute(Subject subject) {
System.out.println("账户浏览");
List<Account> accountList = this.accountService.queryAllAccount();
if(accountList.isEmpty()) {
System.out.println("暂且没有账号存在");
}else {
System.out.println("------------------账号信息列表---------------------");
System.out.println("| 编号 | 姓名 | 账号 | 密码 | 类型 | 状态 |");
for (Account account: accountList ) {
String str = new StringBuilder().append("| ").append(account.getId()).append(" ")
.append("| ").append(account.getName()).append(" ")
.append("| ").append(account.getUsername()).append(" ")
.append("| ").append("******").append(" ")
.append("| ").append(account.getAccountType().getDesc()).append(" ")
.append("| ").append(account.getAccountStatus().getDesc()).append(" ")
.append("| ").toString();
System.out.println(str);
}
System.out.println("--------------------------------------------");
}
}
}
浏览商品:
import com.omega.command.AbstractCommand;
import com.omega.command.Subject;
import com.omega.command.annotation.AdminCommand;
import com.omega.command.annotation.CommandMate;
import com.omega.command.annotation.CustomerCommand;
import com.omega.entity.Goods;
import java.util.List;
@AdminCommand
@CustomerCommand
@CommandMate(
name = "浏览商品",
desc = "LLSP",
command = "商品信息"
)
public class BrowserCommand extends AbstractCommand {
@Override
public void excute(Subject subject) {
List<Goods> goodsList = this.goodsService.quarryAllGoods();
if (goodsList == null){
System.out.println("商品为空");
}else {
for (Goods goods : goodsList){
System.out.println(goods);
}
}
}
}
上架商品:
import com.omega.command.AbstractCommand;
import com.omega.command.Subject;
import com.omega.command.annotation.AdminCommand;
import com.omega.command.annotation.CommandMate;
import com.omega.entity.Goods;
@AdminCommand
@CommandMate(
name = "上架商品",
desc = "SJSP",
command = "商品信息"
)
public class PutAwayCommand extends AbstractCommand {
@Override
public void excute(Subject subject) {
System.out.println("***************上架商品***************");
System.out.println("请输入商品名称:");
String name = scanner.nextLine();
System.out.println("请输入商品简介:");
String introduce = scanner.nextLine();
System.out.println("请输入商品库存:");
int stock = Integer.parseInt(scanner.nextLine());
System.out.println("请输入商品单位:");
String unit = scanner.nextLine();
System.out.println("请输入商品价格:");
int price = new Double(100*scanner.nextDouble()).intValue();
System.out.println("请输入商品折扣:");
int discount = scanner.nextInt();
Goods goods = new Goods();
goods.setName(name);
goods.setIntroduce(introduce);
goods.setStock(stock);
goods.setUnit(unit);
goods.setPrice(price);
goods.setDiscount(discount);
boolean effect = this.goodsService.putAwayGoods(goods);
if (effect){
System.out.println("商品上架成功!!!");
}else {
System.out.println("商品上架失败!!!");
}
}
}
更新商品:
import com.omega.command.AbstractCommand;
import com.omega.command.Subject;
import com.omega.command.annotation.AdminCommand;
import com.omega.command.annotation.CommandMate;
import com.omega.entity.Goods;
@AdminCommand
@CommandMate(
name = "更新商品",
desc = "GXSP",
command = "商品信息"
)
public class UpdateCommand extends AbstractCommand {
@Override
public void excute(Subject subject) {
System.out.println("请输入需要更新商品的id:");
int id = scanner.nextInt();
Goods goods = this.goodsService.getGoods(id);
if (goods == null) {
System.out.println("商品不存在");
} else {
System.out.println(goods);
System.out.println("请输入要更新的简介:");
String introduce = scanner.next();
System.out.println("请输入要更新的库存:");
int stock = scanner.nextInt();
System.out.println("请输入要更新的单位:");
String unit = scanner.next();
System.out.println("请输入要更新的价格:");
int price = new Double(100 * scanner.nextDouble()).intValue();
System.out.println("请输入要更新的折扣:");
int discount = scanner.nextInt();
System.out.println("确认是否更新该商品:y.是 n.否");
String flg = scanner.next();
System.out.println(flg);
if (flg.equalsIgnoreCase("y")){
goods.setIntroduce(introduce);
goods.setStock(stock);
goods.setUnit(unit);
goods.setPrice(price);
goods.setDiscount(discount);
boolean effect = this.goodsService.upDateGoods(goods);
if (effect) {
System.out.println("商品更新成功");
} else {
System.out.println("商品更新失败");
}
}
}
}
}
下架商品:
import com.omega.command.AbstractCommand;
import com.omega.command.Subject;
import com.omega.command.annotation.AdminCommand;
import com.omega.command.annotation.CommandMate;
import com.omega.entity.Goods;
@AdminCommand
@CommandMate(
name = "下架商品",
desc = "XJSP",
command = "商品信息"
)
public class SoldOutCommand extends AbstractCommand {
@Override
public void excute(Subject subject) {
System.out.println("请选择需要下架商品的id:");
int id = scanner.nextInt();
Goods goods = this.goodsService.getGoods(id);
if (goods == null){
System.out.println("商品不存在!");
}else {
System.out.println("商品信息如下:");
System.out.println(goods);
System.out.println("确认是否下架该商品:是.y 否.n");
String flg = scanner.next();
if ("y".equalsIgnoreCase(flg)){
boolean effect = this.goodsService.soldOutGoods(id);
if (effect){
System.out.println("商品下架成功");
}else {
System.out.println("商品下架失败");
}
}
}
}
}
登录顾客账号后
客户端:
在客户端可进行如下操作:
支付订单:
import com.omega.command.AbstractCommand;
import com.omega.command.Subject;
import com.omega.command.annotation.CommandMate;
import com.omega.command.annotation.CustomerCommand;
import com.omega.entity.Goods;
import com.omega.entity.Order;
import com.omega.entity.OrderItem;
import com.omega.type.OrderStatus;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@CustomerCommand
@CommandMate(
name = "支付订单",
desc = "ZFDD",
command = "订单信息"
)
public class PayOrderCommand extends AbstractCommand {
@Override
public void excute(Subject subject) {
System.out.println("请输入想要购买的物品,包括id和数量");
System.out.println("格式id-数量,例如3-4,8-6");
String string = scanner.nextLine();
//将string以,隔开
String[] strings = string.split(",");
List<Goods> goodsList = new ArrayList<>();
for (String goodString : strings){
String[] str = goodString.split("-");
Goods goods = this.goodsService.getGoods(Integer.parseInt(str[0]));
if (goods == null){
System.out.println("不存在该商品,无法购买");
continue;
}
goods.setBuyGoodsNum(Integer.parseInt(str[1]));
goodsList.add(goods);
}
Order order = new Order();
order.setId(String.valueOf(System.currentTimeMillis()));
order.setAccountId(subject.getAccount().getId());
order.setAccountName(subject.getAccount().getName());
order.setCreateTime(LocalDateTime.now());
int totalMoney = 0;
int actualMoney = 0;
for (Goods goods : goodsList){
OrderItem orderItem = new OrderItem();
orderItem.setOrderId(order.getId());
orderItem.setGoodsId(goods.getId());
orderItem.setGoodsName(goods.getName());
orderItem.setGoodsIntroduce(goods.getIntroduce());
orderItem.setGoodsNum(goods.getBuyGoodsNum());
orderItem.setUnit(goods.getUnit());
orderItem.setPrice(goods.getPrice());
orderItem.setDiscount(goods.getDiscount());
order.orderItemsList.add(orderItem);
int money = goods.getBuyGoodsNum()*goods.getPrice();
totalMoney += money;
actualMoney = totalMoney*goods.getDiscount()/100;
}
order.setTotalMoney(totalMoney);
order.setActualMoney(actualMoney);
order.setOrderStatus(OrderStatus.PLAYING);
System.out.println(order);
System.out.println("请输入是否支付订单:确认输入:zf");
String confirm = scanner.next();
if ("zf".equalsIgnoreCase(confirm)){
order.setFinishTime(LocalDateTime.now());
order.setOrderStatus(OrderStatus.FINISH);
boolean effect = this.orderService.payOrder(order);
if (effect) {
for (Goods goods : goodsList) {
boolean isUpdate = this.goodsService.updateAfterPay(goods,goods.getBuyGoodsNum());
if (isUpdate){
System.out.println("订单支付成功");
}else {
System.out.println("订单支付失败");
}
}
}
}else {
System.out.println("订单未成功支付");
}
}
}
查看订单:
import com.omega.command.AbstractCommand;
import com.omega.command.Subject;
import com.omega.command.annotation.CommandMate;
import com.omega.command.annotation.CustomerCommand;
import com.omega.entity.Order;
import java.util.List;
@CustomerCommand
@CommandMate(
name = "查看订单",
desc = "CKDD",
command = "订单信息"
)
public class BrowserOrderCommand extends AbstractCommand {
@Override
public void excute(Subject subject) {
System.out.println("我的订单:");
List<Order> orderList = this.orderService.queryOrderByAccount(subject.getAccount().getId());
if (orderList.isEmpty()){
System.out.println("您的订单为空!");
}else {
System.out.println("您的订单为:");
for (Order order: orderList){
System.out.println("*******************************");
System.out.println(order);
}
}
}
}
对系统进行测试:
上一篇: 决策树实战2-使用决策树预测隐形眼镜类型
下一篇: 三分钟快速了解typeScrit声明文件
推荐阅读