输入账号登录之后选择要进行的功能
订购商品时首先会显示商品列表,然后输入商品编号,若购买数量超出库存量,则会显示数量不足,且商品的状态是通过库存量来进行确定的
退货后会将已订购的商品退回并且将商品的数量还原
public void login() {
System.out.println("***********欢迎使用商品订单系统***********");
Scanner sc = new Scanner(System.in);
boolean flag;
do {
flag = true;
System.out.print("请输入账号:");
String username = sc.next();
System.out.print("请输入密码:");
String password = sc.next();
Users user = this.login.selectLogin(username, password);
if (user != null) {
new OrderService().showMainMean(user.getId());
} else {
flag = false;
System.out.println("账号或密码错误,请重新输入!");
}
} while (!flag);
}
public Users selectLogin(String username, String password) {
String sql = "select * from users where username=? and password=?";
Users users = null;
try {
users = this.getJdbcTemplate().queryForBean(sql, new Object[] { username, password });
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return users;
}
public void add(String usersId) {
Scanner sc = new Scanner(System.in);
System.out.println("商品列表");
List<Product> list = this.productDao.selectByAll();
if (list != null && !list.isEmpty()) {
System.out.println("商品编号\t商品名称\t单价\t库存量\t状态");
for (Product pro : list) {
System.out.println(pro.getId() + "\t" + pro.getName() + "\t" + pro.getPrice() + "\t" + pro.getStock()
+ "\t" + (pro.getStock() > 0 ? "有货" : "无货"));
}
String productId;// 商品编号
boolean flag;
do {
flag = true;
System.out.print("请输入要订购的商品编号:");
productId = sc.next();
for (Product pro : list) {
if (productId.equals(pro.getId())) {
flag = false;
break;
}
}
if (flag) {
System.out.println("商品编号输入有误,请重新输入!");
}
} while (flag);
int num;// 订购数量
do {
flag = true;
System.out.print("请输入要订购的商品数量:");
num = sc.nextInt();
// 商品数量
int stock = (int) this.productDao.selectByProductIdToStock(productId);
if (num > stock) {
flag = false;
System.out.println("商品数量不足,请重新订购!");
} else {
// 开启事务控制
TxManager.begin();
// 商品表数量更改
this.productDao.updateByProductIdReduceStock(productId, num);
}
} while (!flag);
// 订单号
String orderId = DateUtil.DateToString(new Date(), "yyyyMMddHHmmssSSS");
// 下单时间
String created = DateUtil.DateToString(new Date(), "yyyy-MM-dd HH:mm");
double total = num * ((double) this.productDao.selectByProductIdToPrice(productId));
Orders order = new Orders(orderId, usersId, productId, created, num, total);
int sum = this.orderDao.insert(order);
if (sum > 0) {
// 提交事务
TxManager.commit();
System.out.println("订购成功!");
} else {
// 回滚事务
TxManager.rollback();
System.out.println("订购失败!");
}
} else {
System.out.println("没有商品信息!");
}
}
//查询全部商品信息Dao层
public List<Product> selectByAll() {
String sql = "select * from product";
List<Product> list = null;
try {
list = this.getJdbcTemplate().queryForBeanList(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
//添加订单信息Dao层
public int insert(Orders order) {
String sql = "insert into orders values(?,?,?,?,?,?)";
int num = 0;
try {
num = this.getJdbcTemplate().update(sql, new Object[] { order.getId(), order.getUserId(),
order.getProductId(), order.getCreated(), order.getNum(), order.getTotal() });
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return num;
}
//通过商品编号减少数量Dao层
public int updateByProductIdReduceStock(String productId, int stock) {
String sql = "update product set stock=stock-? where id=?";
int num = 0;
try {
num = this.getJdbcTemplate().update(sql, new Object[] { stock, productId });
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return num;
}
public void findOrder(String usersId) {
List<Map<String, Object>> list = this.orderDao.selectOrders(usersId);
if (list != null && !list.isEmpty()) {
System.out.println("订单编号\t\t\t商品编号\t商品名称\t单价\t订购量\t金额\t下单时间");
for (Map<String, Object> map : list) {
System.out.println(
map.get("id") + "\t" + map.get("product_id") + "\t" + map.get("name") + "\t" + map.get("price")
+ "\t" + map.get("num") + "\t" + map.get("total") + "\t" + map.get("created"));
}
} else {
System.out.println("您没有订购任何商品!");
}
}
public List<Map<String, Object>> selectOrders(String userId) {
String sql = "select orders.id,orders.product_id,product.name,product.price,orders.num,orders.total,orders.created from orders inner join product on orders.product_id=product.id where orders.user_id=? order by orders.created desc";
List<Map<String, Object>> list = null;
try {
list = this.getJdbcTemplate().queryForMapList(sql, new Object[] { userId });
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public void removeOrder(String usersId) {
Scanner sc = new Scanner(System.in);
System.out.println("订单列表");
List<Map<String, Object>> list = this.orderDao.selectOrders(usersId);
if (list != null && !list.isEmpty()) {
System.out.println("订单编号\t\t\t商品编号\t商品名称\t单价\t订购量\t金额\t下单时间");
for (Map<String, Object> map : list) {
System.out.println(
map.get("id") + "\t" + map.get("product_id") + "\t" + map.get("name") + "\t" + map.get("price")
+ "\t" + map.get("num") + "\t" + map.get("total") + "\t" + map.get("created"));
}
String orderId;// 订单编号
boolean flag;
do {
flag = true;
System.out.print("请输入要退货的订单编号:");
orderId = sc.next();
for (Map<String, Object> map : list) {
if (orderId.equals(map.get("id"))) {
flag = false;
break;
}
}
if (flag) {
System.out.println("商品编号输入有误,请重新输入!");
}
} while (flag);
Map<String, Object> map = this.orderDao.selectByOrderId(usersId, orderId);
System.out.println("订单编号:" + map.get("id"));
System.out.println("商品编号:" + map.get("product_id"));
System.out.println("商品名称:" + map.get("name"));
System.out.println("单价:" + map.get("price"));
System.out.println("订购量:" + map.get("num"));
System.out.println("金额:" + map.get("total"));
System.out.println("下单时间:" + map.get("created"));
System.out.print("请问确定退货吗(Y/N):");
String answer = sc.next();
if ("Y".equalsIgnoreCase(answer)) {
TxManager.begin();
int num = this.orderDao.deleteOrder(usersId, orderId);
int sum = this.productDao.update(map.get("product_id").toString(),
Integer.parseInt(map.get("num").toString()));
if (num * sum > 0) {
TxManager.commit();
System.out.println("退货成功!");
} else {
TxManager.rollback();
System.out.println("退货失败!");
}
} else {
System.out.println("操作已取消!");
}
} else {
System.out.println("您没有订购任何商品!");
}
}
//查看自己的订单信息
public List<Map<String, Object>> selectOrders(String userId) {
String sql = "select orders.id,orders.product_id,product.name,product.price,orders.num,orders.total,orders.created from orders inner join product on orders.product_id=product.id where orders.user_id=? order by orders.created desc";
List<Map<String, Object>> list = null;
try {
list = this.getJdbcTemplate().queryForMapList(sql, new Object[] { userId });
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
//删除订单信息
public int deleteOrder(String userId, String orderId) {
String sql = "delete from orders where id=? and user_id=?";
int num = 0;
try {
num = this.getJdbcTemplate().update(sql, new Object[] { orderId, userId });
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return num;
}
CREATE TABLE `orders` (
`id` varchar(50) NOT NULL,
`user_id` varchar(50) DEFAULT NULL,
`product_id` varchar(50) DEFAULT NULL,
`created` varchar(50) DEFAULT NULL,
`num` int(11) DEFAULT NULL,
`total` double DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
KEY `product_id` (`product_id`),
CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
CONSTRAINT `orders_ibfk_2` FOREIGN KEY (`product_id`) REFERENCES `product` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `product` (
`id` varchar(50) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`price` double DEFAULT NULL,
`stock` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `product` VALUES ('0001', '鼠标', '50', '100');
INSERT INTO `product` VALUES ('0002', '键盘', '100', '20');
INSERT INTO `product` VALUES ('0003', '手机', '2000', '10');
INSERT INTO `product` VALUES ('0004', '电脑', '5000', '10');
INSERT INTO `product` VALUES ('0005', '空调', '3000', '100');
CREATE TABLE `users` (
`id` varchar(50) NOT NULL,
`username` varchar(50) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `users` VALUES ('0001', '张三', '123456');
INSERT INTO `users` VALUES ('0002', '李四', '123456');