Compare commits

..

7 Commits
src ... view

@ -0,0 +1,200 @@
package flowershop.view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableModel;
import flowershop.dao.OrderDao;
import flowershop.dao.OrderDetailDao;
import flowershop.model.Flower;
import flowershop.model.Order;
import flowershop.model.OrderDetail;
import flowershop.model.User;
import flowershop.dao.FlowerDao;
import flowershop.daoimpl.FlowerDaoImpl;
import flowershop.daoimpl.OrderDaoImpl;
import flowershop.daoimpl.OrderDetailDaoImpl;
//商品购物车窗口
public class CartFrame extends MyFrame {
private JTable table;
// 购物车数据
private Object[][] data;
// 创建商品Dao
private FlowerDao Dao = new FlowerDaoImpl();
// 购物车键是选择的商品Id值是商品的数量
private Map<String, Integer> cart;
// 引用到上级FrameProductListFrame
FlowerListFrame flowerListFrame;
public CartFrame(Map<String, Integer> cart, FlowerListFrame flowerListFrame) {
super("商品购物车", 1000, 700);
this.cart = cart;
this.flowerListFrame = flowerListFrame;
JPanel topPanel = new JPanel();
topPanel.setBackground(new Color(250,220,230));
FlowLayout fl_topPanel = (FlowLayout) topPanel.getLayout();
fl_topPanel.setVgap(10);
fl_topPanel.setHgap(20);
getContentPane().add(topPanel, BorderLayout.NORTH);
JButton btnReturn = new JButton("返回商品列表");
btnReturn.setFont(new Font("微软雅黑", Font.PLAIN, 15));
topPanel.add(btnReturn);
JButton btuSubmit = new JButton("提交订单");
// 注册【提交订单】按钮的ActionEvent事件监听器
btuSubmit.addActionListener(e -> {
// 生成订单
generateOrders();
JLabel label = new JLabel("订单已经生成,等待付款。");
btuSubmit.setFont(new Font("微软雅黑", Font.PLAIN, 15));
if (JOptionPane.showConfirmDialog(this, label, "信息", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
// TODO 付款
System.out.println("未付款");
System.exit(0);
} else {
System.out.println("付款");
System.exit(1);
}
});
topPanel.add(btuSubmit);
JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, BorderLayout.CENTER);
scrollPane.setViewportView(getTable());
// 注册【返回商品列表】按钮的ActionEvent事件监听器
btnReturn.addActionListener(e -> {
// 更新购物车
for (int i = 0; i < data.length; i++) {
// 商品编号
String productid = (String) data[i][0];
// 数量
Integer quantity = (Integer) data[i][3];
cart.put(productid, quantity);
}
this.flowerListFrame.setVisible(true);
this.setVisible(false);
});
}
// 初始化左侧面板中的表格控件
private JTable getTable() {
// 准备表中数据
data = new Object[cart.size()][5];
Set<String> keys = this.cart.keySet();
int indx = 0;
for (String productid : keys) {
Flower f = Dao.findById(productid);
data[indx][0] = f.getProductid();// 商品编号
data[indx][1] = f.getName();// 商品名
data[indx][2] = new Double(f.getPrice());// 商品单价
data[indx][3] = new Integer(cart.get(productid));// 数量
// 计算商品应付金额
double amount = (double) data[indx][2] * (int) data[indx][3];
data[indx][4] = new Double(amount);
indx++;
}
// 创建表数据模型
TableModel model = new CartTableModel(data);
if (table == null) {
// 创建表
table = new JTable(model);
table.setBackground(new Color(250,220,230));
// 设置表中内容字体
table.setFont(new Font("微软雅黑", Font.PLAIN, 16));
// 设置表列标题字体
table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 16));
// 设置表行高
table.setRowHeight(51);
table.setRowSelectionAllowed(false);
} else {
table.setModel(model);
}
return table;
}
// 生成订单
private void generateOrders() {
// Example: Initialize the user before using it
MainApp.user = new User();
String someUserId = null;
MainApp.user.setUserid(someUserId);
if (MainApp.user != null) {
String userId = MainApp.user.getUserid();
// Continue processing with userId
} else {
// Handle the case where user is null
System.out.println("未初始化用户");
}
OrderDao orderDao = new OrderDaoImpl();
OrderDetailDao orderDetailDao = new OrderDetailDaoImpl();
Order order = new Order();
order.setUserid(MainApp.user.getUserid());
// 0待付款
order.setStatus(0);
// 订单Id是当前时间
Date now = new Date();
long orderid = now.getTime();
order.setOrderid(orderid);
order.setOrderdate(now);
order.setAmount(getOrderTotalAmount());
// 下订单时间是数据库自动生成不用设置
// 创建订单
orderDao.create(order);
for (int i = 0; i < data.length; i++) {
OrderDetail orderDetail = new OrderDetail();
orderDetail.setOrderid(orderid);
orderDetail.setProductid((String) data[i][0]);
orderDetail.setQuantity((int) data[i][3]);
orderDetail.setPrice((double) data[i][2]);
// 创建订单详细
orderDetailDao.create(orderDetail);
}
}
// 计算订单应付总金额
private double getOrderTotalAmount() {
double totalAmount = 0.0;
for (int i = 0; i < data.length; i++) {
// 计算商品应付金额
totalAmount += (Double) data[i][4];
}
return totalAmount;
}
}

@ -0,0 +1,76 @@
package flowershop.view;
import javax.swing.table.AbstractTableModel;
//购物车表格模型
public class CartTableModel extends AbstractTableModel {
// 表格列名columnNames
private String[] columnNames = { "编号", "花卉名", "单支价格", "数量", "应付金额" };
// 表格中数据保存在data二维数组中
private Object[][] data = null;
public CartTableModel(Object[][] data) {
this.data = data;
}
// 返回列数
@Override
public int getColumnCount() {
return columnNames.length;
}
// 返回行数
@Override
public int getRowCount() {
return data.length;
}
// 获得某行某列的数据而数据保存在对象数组data中
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
}
@Override
public String getColumnName(int columnIndex) {
return columnNames[columnIndex];
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
// 数量列可以修改
if (columnIndex == 3) {
return true;
}
return false;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
// 只允许修改数量列
if (columnIndex != 3) {
return;
}
try {
// 从表中获得修改之后的商品数量从表而来的数据都String类型
int quantity = new Integer((String) aValue);
// 商品数量不能小于0
if (quantity < 0) {
return;
}
// 更新数量列
data[rowIndex][3] = quantity;
// 计算商品应付金额
double price = (double) data[rowIndex][2];
double totalPrice = price * quantity;
// 更新商品应付金额列
data[rowIndex][4] = new Double(totalPrice);
} catch (Exception e) {
}
}
}

@ -0,0 +1,242 @@
package flowershop.view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import flowershop.model.Flower;
import flowershop.dao.FlowerDao;
import flowershop.daoimpl.FlowerDaoImpl;
//商品列表窗口
public class FlowerListFrame extends MyFrame{
private JTable table;
private JLabel lblImage;
private JLabel lblPrice;
private JLabel lblDescn;
//商品列表集合
private List<Flower> flowers =null;
//创建商品Dao对象
private FlowerDao dao = new FlowerDaoImpl();
// 购物车键是选择的商品Id值是商品的数量
private Map<String,Integer> cart = new HashMap<String,Integer>();
// 选择的商品索引
private int selectedRow = -1;
public FlowerListFrame(String title, int width, int height) {
super(title, width, height);
// TODO 自动生成的构造函数存根
flowers = dao.findAll();
//添加顶部搜索面板
getContentPane().add(getSearchPanel(),BorderLayout.NORTH);
//创建分栏面板
JSplitPane splitPane = new JSplitPane();
//设置指定分割条位置,从窗格的左边到分割条的右边
splitPane.setDividerLocation(600);
//设置左侧面板
splitPane.setLeftComponent(getLeftPanel());
//设置右侧面板
splitPane.setRightComponent(getRightPanel());
//把分栏面板添加到内容面板
getContentPane().add(splitPane,BorderLayout.CENTER);
}
//初始化右侧面板
private JPanel getRightPanel() {
JPanel rightPanel = new JPanel();
rightPanel.setBackground(new Color(255, 220, 230));
rightPanel.setLayout(new GridLayout(2,1,0,20));
lblImage = new JLabel();
rightPanel.add(lblImage);
lblImage.setHorizontalAlignment(SwingConstants.CENTER);
JPanel detailPanel = new JPanel();
detailPanel.setBackground(new Color(255, 220, 230));
rightPanel.add(detailPanel);
detailPanel.setLayout(new GridLayout(8,1,0,5));
JSeparator separator_1 = new JSeparator();
detailPanel.add(separator_1);
lblPrice = new JLabel();
detailPanel.add(lblPrice);
//设置字体
lblPrice.setFont(new Font("微软雅黑",Font.PLAIN,16));
lblDescn = new JLabel();
detailPanel.add(lblDescn);
//设置字体
lblDescn.setFont(new Font("微软雅黑",Font.PLAIN,16));
JSeparator separator_2 = new JSeparator();
detailPanel.add(separator_2);
JButton btnAdd = new JButton("添加到购物车");
btnAdd.setFont(new Font("微软雅黑",Font.PLAIN,15));
detailPanel.add(btnAdd);
//布局占位使用
JLabel lb1 =new JLabel("");
detailPanel.add(lb1);
JButton btnCheck = new JButton("查看购物车");
btnCheck.setFont(new Font("微软雅黑",Font.PLAIN,15));
detailPanel.add(btnCheck);
//注册【添加到购物车】按钮的ActionEvent事件监听器
btnAdd.addActionListener(e ->{
if(selectedRow <0) {
return;
}
//添加商品到购物车处理
Flower selectFlower = flowers.get(selectedRow);
String productid = selectFlower.getProductid();
if(cart.containsKey(productid)) {//购物车中已经有该商品
//获得商品数量
Integer quantity = cart.get(productid);
cart.put(productid, ++quantity);
}else {//购物车中还没有还商品
cart.put(productid, 1);
}
System.out.println(cart);
});
// //注册【查看到购物车】按钮的ActionEvent事件监听器
btnCheck.addActionListener(e->{
CartFrame cartFrame = new CartFrame(cart,this);
cartFrame.setVisible(true);
this.setVisible(false);
});
return rightPanel;
}
//初始化左侧面板
private JScrollPane getLeftPanel() {
JScrollPane leftScrollPane = new JScrollPane();
//将表格作为滚动面板的各个视口试图
leftScrollPane.setViewportView(getTable());
return leftScrollPane;
}
//初始化左侧面板中的表格控件
private JTable getTable() {
FlowerTableModel model = new FlowerTableModel(this.flowers);
if(table == null) {
table = new JTable(model);
//设置表中内容字体
table.setFont(new Font("微软雅黑",Font.PLAIN,16));
//设置表列标题字体
table.getTableHeader().setFont(new Font("微软雅黑",Font.PLAIN,16));
//设置表行高
table.setRowHeight(51);
table.setRowSelectionAllowed(true);
table.setBackground(new Color(250,220,230));
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ListSelectionModel rowSelectionModel = table.getSelectionModel();
rowSelectionModel.addListSelectionListener(e ->{
//只处理鼠标释放
if(e.getValueIsAdjusting()) {
return;
}
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
selectedRow = lsm.getMinSelectionIndex();
if(selectedRow < 0) {
return;
}
//更新右侧面板内容
Flower f = flowers.get(selectedRow);
String petImage = String.format("/image/%s",f.getImage());
ImageIcon icon = new ImageIcon(FlowerListFrame.class.getResource(petImage));
lblImage.setIcon(icon);
String descn = f.getDescn();
lblDescn.setText("花语: "+ descn);
double price = f.getPrice();
String sprice = String.format("单支价格:%.2f",price);
lblPrice.setText(sprice);
});
}else {
table.setModel(model);
}
return table;
}
//初始化搜索面板
private JPanel getSearchPanel() {
//TODO
JPanel searchPanel = new JPanel();
searchPanel.setBackground(new Color(255, 220, 230));
FlowLayout flowLayout = (FlowLayout) searchPanel.getLayout();
flowLayout.setVgap(20);
flowLayout.setHgap(40);
JLabel lbl = new JLabel("请选择花卉类别:");
lbl.setFont(new Font("微软雅黑",Font.PLAIN,15));
searchPanel.add(lbl);
String[] categorys= {"春季花卉","夏季花卉","秋季花卉","冬季花卉"};
JComboBox comboBox = new JComboBox(categorys);
comboBox.setFont(new Font("微软雅黑",Font.PLAIN,15));
searchPanel.add(comboBox);
JButton btnGo = new JButton("查询");
btnGo.setFont(new Font("微软雅黑", Font.PLAIN, 15));
searchPanel.add(btnGo);
JButton btnReset = new JButton("重置");
btnReset.setFont(new Font("微软雅黑",Font.PLAIN,15));
searchPanel.add(btnReset);
//注册查询按钮的ActionEvent事件监听器
btnGo.addActionListener(e ->{
//所选择的类别
String category = (String) comboBox.getSelectedItem();
//按照类别查询
flowers = dao.findByCategory(category);
TableModel model = new FlowerTableModel(flowers);
table.setModel(model);
});
// 注册重置按钮的ActionEvent事件监听器
btnReset.addActionListener(e -> {
flowers = dao.findAll();
TableModel model = new FlowerTableModel(flowers);
table.setModel(model);
});
return searchPanel;
}
}

@ -0,0 +1,58 @@
package flowershop.view;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import flowershop.model.Flower;
//商品列表表格模型
public class FlowerTableModel extends AbstractTableModel{
//表格列表columnNames
private String[] columnNames = {"编号","花卉名","类别"};
private List<Flower> data = null;
public FlowerTableModel(List<Flower> data) {
this.data = data;
}
//返回行数
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return data.size();
}
//返回列数
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return columnNames.length;
}
//获得某行某列的数据而数据保存在对象数组data中
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
// 每一行就是一个Product商品对象
Flower f = data.get(rowIndex);
switch(columnIndex) {
case 0 :
return f.getProductid();
case 1 :
return f.getName();
default :
return f.getCategory();
}
}
public String getColumnName(int columnIndex) {
return columnNames[columnIndex];
}
}

@ -0,0 +1,93 @@
package flowershop.view;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import flowershop.model.User;
import flowershop.dao.UserDao;
import flowershop.daoimpl.UserDaoImpl;
public class LoginFrame extends MyFrame {
private JTextField txtUserid;
private JPasswordField txtpassword;
public LoginFrame(String title,int width, int height) {
super(title,width,height);
//TODO
getContentPane().setLayout(null);
JLabel label1 = new JLabel();
label1.setHorizontalAlignment(SwingConstants.RIGHT);
label1.setBounds(51,33,83,30);
label1.setText("账号:");
label1.setFont(new Font("微软雅黑",Font.PLAIN,15));
getContentPane().add(label1);
txtUserid = new JTextField(10);
txtUserid.setText("");
txtUserid.setBounds(158,33,157,30);
txtUserid.setFont(new Font("微软雅黑",Font.PLAIN,15));
getContentPane().add(txtUserid);
JLabel label2 = new JLabel();
label2.setHorizontalAlignment(SwingConstants.RIGHT);
label2.setBounds(51,85,83,30);
label2.setText("密码:");
label2.setFont(new Font("微软雅黑",Font.PLAIN,15));
getContentPane().add(label2);
txtpassword = new JPasswordField(10);
txtpassword.setText("");
txtpassword.setBounds(158,85,157,30);
txtpassword.setFont(new Font("微软雅黑",Font.PLAIN,15));
getContentPane().add(txtpassword);
JButton btnOK = new JButton();
btnOK.setText("确定");
btnOK.setFont(new Font("微软雅黑",Font.PLAIN,15));
btnOK.setBounds(61, 140, 100, 30);
getContentPane().add(btnOK);
JButton btnCancel = new JButton();
btnCancel.setText("取消");
btnCancel.setFont(new Font("微软雅黑",Font.PLAIN,15));
btnCancel.setBounds(225,140,100,30);
getContentPane().add(btnCancel);
btnOK.addActionListener(e->{
UserDao accd = new UserDaoImpl();
User ac = accd.findById(txtUserid.getText());
String tpassword = new String(txtpassword.getPassword());
if(ac !=null && tpassword.equals(ac.getPassword())) {
System.out.println("登陆成功! ");
FlowerListFrame plf =new FlowerListFrame("商品列表",1000,700);
plf.setVisible(true);
setVisible(false);
}else {
JLabel label3 = new JLabel("您输入的账户或密码有误,请重新输入!");
label3.setFont(new Font("微软雅黑",Font.PLAIN,15));
JOptionPane.showMessageDialog(null,label3,"登录失败",JOptionPane.ERROR_MESSAGE);
}
});
btnCancel.addActionListener(e->{
//退出系统
System.exit(0);
});
}
}

@ -0,0 +1,18 @@
package flowershop.view;
import java.awt.Color;
import flowershop.model.User;
public class MainApp {
//用户登录成功后,需要保存的用户信息
public static User user;
//Session(会话)
public static void main(String[] args) {
//TODO 调用LoginFrame
LoginFrame loginFrame = new LoginFrame("用户登录",400,250);
loginFrame.setVisible(true);
}
}

@ -0,0 +1,37 @@
package flowershop.view;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class MyFrame extends JFrame {
//获得屏幕的宽度
private double ScreenWidth = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
//获得屏幕的高度
private double ScreenHeigth = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
public MyFrame(String title,int width,int heigth) {
super(title);
//设置窗口大小
setSize(width,heigth);
//计算窗口居中的坐标
int x = (int)(ScreenWidth - width) / 2;
int y = (int)(ScreenHeigth - heigth) / 2;
//设置窗口的位置
setLocation(x,y);
//注册窗口事件
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
// 退出系统
System.exit(0);
}
});
}
}
Loading…
Cancel
Save