Java课设大作业——购物车

Java课设大作业——购物车

团队名称介绍

团队名称:依托答辩

团队成员:

成员职务负责部分
冯朝兴组长全部

项目Git地址及提交记录

https://gitee.com/whats-Thename/shopping.git

在这里插入图片描述

前期调查

以在京东进行网购为例,用户登录后,可以在商城中浏览商品,选择将心仪的商品加入购物车,也可以选择查看购物车,并对购物车进行管理,管理操作包括删除商品条目、更改商品数量,最后点击结算,生成订单,最后支付,即可完成购买。

商场界面:

在这里插入图片描述

购物车界面:

在这里插入图片描述

项目功能架构图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0osah55X-1673445077277)(C:\Users\86139\AppData\Roaming\Typora\typora-user-images\image-20230111212741356.png)]

面向对象设计包图、类图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-M5kSf2IM-1673445077279)(C:\Users\86139\AppData\Roaming\Typora\typora-user-images\image-20230111211552812.png)]

在这里插入图片描述

项目运行截图

用户登录界面:

在这里插入图片描述

主菜单界面:

在这里插入图片描述

浏览商品并添加单件商品到购物车:

在这里插入图片描述

搜索商品并添加多件商品到购物车:

在这里插入图片描述

管理购物车之修改商品数量:

管理购物车之删除商品条目:
(删除前)
在这里插入图片描述
(删除后)
在这里插入图片描述

管理购物车之结算:

项目关键代码

0、用户登录

public class LoginService {
    public static boolean login(String name,String password) {
        if (name == null || password == null || "".equals(name) || "".equals(password)) {
        	return false;
        }
        return "123".equals(name) && "123".equals(password);
    }
}
public class UserLoginFrame extends javax.swing.JFrame {
    private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	//账号
    	String name = nameTextField.getText();
        char[] password = pwdPasswordField.getPassword();
        String pwd = new String(password);
        //登录成功
        if (LoginService.login(name, pwd)) {
            MenuFrame menuFrame = new MenuFrame();
            menuFrame.setVisible(true);
            this.setVisible(false);
            this.dispose();
        }else {
            JOptionPane.showMessageDialog(null,"账号或密码错误!");
        }
    }
}

1、导入商品信息

public class Mall {
    public void importToMall() {
    	File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
    	String desktopPath = desktopDir.getAbsolutePath();
    	String filePath = desktopPath.replaceAll("\\\\","//");
    	File selectedFile = new File(filePath+"//data.txt");
        String line;
        try (InputStreamReader isr = new InputStreamReader(new FileInputStream(selectedFile));
            BufferedReader br = new BufferedReader(isr)) {
            Scanner sc = null;
            while ((line = br.readLine()) != null) {
                sc = new Scanner(line);
                Commodity commodity = new Commodity(sc.nextInt(),sc.next(),sc.nextDouble());
                list.add(commodity);
            }
        } catch (FileNotFoundException e) {
            Logger.getLogger(MallDialog.class.getName()).log(Level.SEVERE,null,e);
        } catch (IOException e) {
            Logger.getLogger(MallDialog.class.getName()).log(Level.SEVERE,null,e);
        }
    }
}

2、浏览商品

public class MallDialog extends javax.swing.JDialog {
	private void initializeTable() {
        DefaultTableModel model = (DefaultTableModel) dataTable.getModel();
        List<Commodity> list = mall.getList();
        for (Commodity commodity:list) {
            Object[] x = {commodity.getId(),commodity.getName(),commodity.getPrice()};
            model.addRow(x);
        }
    }
}

3、添加购物车

public class MallDialog extends javax.swing.JDialog {
	private void addToCartButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	//获取当前选中的行
    	int[] selectedRows = dataTable.getSelectedRows();
    	//存储数据
        TableModel model = dataTable.getModel();
        for (int selectedRow:selectedRows) {
            Integer id = (Integer) model.getValueAt(selectedRow, 0);
            String name = (String) model.getValueAt(selectedRow, 1);
            Double price = (Double) model.getValueAt(selectedRow, 2);
            Commodity commodity = new Commodity(id, name, price);
            cart.add(commodity);
        }
        JOptionPane.showMessageDialog(null,"加购成功");
    }
}

4、查看购物车

public class ShoppingCartDialog extends javax.swing.JDialog {
	private void initializeTable() {
        DefaultTableModel model = (DefaultTableModel) dataTable.getModel();
        List<Cart.ItemEntry> list = cart.getList();
        for (Cart.ItemEntry entry : list) {
            Commodity commodity = entry.getItem();
            Object[] x = {commodity.getId(), commodity.getName(), commodity.getPrice(), entry.getCount()};
            model.addRow(x);
        }
    }
}

5、删除购物车中的商品条目

public class ShoppingCartDialog extends javax.swing.JDialog {
    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	//获取当前选中的行
    	int[] selectedRows = dataTable.getSelectedRows();
        DefaultTableModel model = (DefaultTableModel) dataTable.getModel();
        for (int selectedRow : selectedRows) {
            Integer id = (Integer) model.getValueAt(selectedRow, 0);
            String name = (String) model.getValueAt(selectedRow, 1);
            Double price = (Double) model.getValueAt(selectedRow, 2);
            Commodity commodity = new Commodity(id, name, price);
            cart.remove(commodity);
            model.removeRow(selectedRow);
        }
    }
}

6、更改购物车中商品数量

public class ShoppingCartDialog extends javax.swing.JDialog {
    private void modifyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifyButtonActionPerformed
        try {
            String inputValue = JOptionPane.showInputDialog("该商品数量修改为");
            int n = Integer.parseInt(inputValue);
            int selectedRow = dataTable.getSelectedRow();
            DefaultTableModel model = (DefaultTableModel) dataTable.getModel();
            Integer id = (Integer) model.getValueAt(selectedRow, 0);
            String name = (String) model.getValueAt(selectedRow, 1);
            Double price = (Double) model.getValueAt(selectedRow, 2);
            Commodity commodity = new Commodity(id, name, price);
            cart.modify(commodity, n);
            if (n >= 1) {
            	model.setValueAt(n, selectedRow, 3);
            }else {
            	model.removeRow(selectedRow);
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "数量的数字格式出错!");
        }
    }
}

7、购物车结算

public class ShoppingCartDialog extends javax.swing.JDialog {
    private void settleButtonActionPerformed(java.awt.event.ActionEvent evt) {
        int choice = JOptionPane.showConfirmDialog(null,"总价为:"+cart.getSettlement()+",是否结算?","订单",JOptionPane.YES_NO_OPTION);
        //选择 是
        if (choice == 0) {
            cart.getList().clear();
            DefaultTableModel model = (DefaultTableModel) dataTable.getModel();
            model.setRowCount(0);
        }
    }
}

项目代码扫描结果及改正

代码扫描结果:

1、方法内部单行注释,在被注释语句上方另起一行,使用//注释,不要使用行尾注释。

2、所有的覆写方法,必须加@Override注释。

3、类、类属性、类方法的注释必须使用javadoc规范,使用/** 内容 */格式,不得使用行注释和块注释的格式。

4、if/else/for/while/do语句中必须使用大括号,即使只有一行代码。

代码改正方式:

1、原先在行尾的注释改为注释在语句上方,或者直接删除。

2、给没加@Override注释的重写方法注释。

3、属性和方法由原先的行注释改为使用javadoc形式的注释,或者直接删除。

4、给没使用大括号的只有一行代码的if语句加上大括号。

项目总结

1、 将商品信息从用文件存储改进为用数据库存储。

2、 实现支持多用户的功能,每个用户都拥有自己的购物车。

3、实现购物车信息的存储。

4、实现实时显示购物车商品价格总和的功能。

5、进一步为购物车结算的订单单独设计一个界面。

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值