电商购物平台的不断完善

news2024/11/27 0:32:06

目录

1.java编译环境的创建,与所需要用到的插件

第一个电商平台

1.初步思路:

2.确定java类

3.源码分析

成果: 

第二个电商购物平台

代码:

最终的成果:


1.java编译环境的创建,与所需要用到的插件

(1) 选择的编译器为2022版本的intellij idea

首先新建一个空项目

同时,创建完之后,我们点击 文件 -> 项目结构 

进入项目结构,点击项目,选择好你想要的jdk版本,若没有,可以在idea中下载,这里我使用openjdk-19, 

点击模块,添加模块

 我们在Fram1中完成第一个电商平台,Fram2中完成第二个电商平台以此类推

(2) 所需要用到的插件为:JFormDesigner

 下载安装重启软件之后即可使用窗口——swing

第一个电商平台

1.初步思路:

完成一个电商购物平台的登录注册窗体,点击“点我注册”,显示注册窗体,用户输入用户信息,点击提交后到达显示用户信息的窗体,用户点击登录注册窗体的登录按钮,到达商品信息查询的窗体

分析

四个窗口:电商购物平台登录窗口,用户注册窗口,注册的人员名单,全部的商品名单

两个内容:人员信息,商品信息

2.确定java类

根据分析可以得到要完成6个java类,分别是电商购物平台登录窗口类,用户注册窗口类,注册人员名单类,全部的商品名单类,还有人员信息类和商品信息类

3.源码分析

(1) 人员信息类——user类

        按照初步思路来说人员信息需要的属性有:id,type(管理员,普通人员),password,name,sex,city

        每一次都要get属性并set

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

再用tostring方法把所有的属性变为额字符串的形式

所以代码如下

public class user {
    private String id;
    private String type;
    private String password;
    private String name;
    private char sex;
    private String city;

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return this.type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public char getSex() {
        return this.sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public String getCity() {
        return this.city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public user(String id, String type, String password, String name, char sex, String city) {
        this.id = id;
        this.type = type;
        this.password = password;
        this.name = name;
        this.sex = sex;
        this.city = city;
    }


    public String toString() {
        return this.id + ":" + this.type + ":" + this.password + ":" + this.name + ":" + this.sex + ":" + this.city;
    }
}

(2)书籍商品信息类——Category类

所需要的属性为:

 与人员信息操作一致,代码如下

public class Category {
    private String id;
    private String name;
    private String author;
    private String firstlevel;
    private String secondlevel;
    private int number;

    public Category(String id, String name, String author, int number, String firstlevel, String secondlevel) {
        this.id = id;
        this.name = name;
        this.author = author;
        this.firstlevel = firstlevel;
        this.secondlevel = secondlevel;
        this.number = number;
    }

    public String getFirstlevel() {
        return this.firstlevel;
    }

    public void setFirstlevel(String firstlevel) {
        this.firstlevel = firstlevel;
    }

    public String getSecondlevel() {
        return this.secondlevel;
    }

    public void setSecondlevel(String secondlevel) {
        this.secondlevel = secondlevel;
    }

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return this.author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public int getNumber() {
        return this.number;
    }

    public void setNumber(int number) {
        this.number = number;
    }
}

(3) 登录界面——LoginFrame类

首先确定登录界面的情况

首先这些属性需要 JLabel(标签类):用户名,用户类型,密码

                                JTextField(文本框)JPasswordField(文本框)

                                JComboBox<String>(选择框)

                                JButton(按钮)

定义主函数,跳出一个登录页面窗口

    public static void main(String[] args) {
        new LoginFrame();
    }

构造登录页面窗口

  public LoginFrame() {
        this.setSize(400, 300);
        this.setTitle("电商购物-登录页面");
        this.setLocation(300, 200);
    }

 初始化窗口的大小和标题还有位置还要初始化属性和可见度

        this.init();
        this.setVisible(true);

 在初始化方法中,先对于每一个属性赋予初始值,对于文本框进行new+构造方法空值,注册按钮则选择跳转,最后则进行网格布局,p.add在网格中全部添加之后,再确定网格边界的位置,则代码如下:

import java.awt.GridLayout;
import java.awt.LayoutManager;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class LoginFrame extends JFrame {
    private JLabel l_name;
    private JLabel l_type;
    private JLabel l_password;
    private JTextField t_name;
    private JComboBox<String> c_type;
    private JPasswordField p_password;
    private JButton b_login;
    private JButton b_reset;
    private JButton b_register;

    public static void main(String[] args) {
        new LoginFrame();
    }

    public LoginFrame() {
        this.setSize(400, 300);
        this.setTitle("电商购物-登录页面");
        this.setLocation(300, 200);
        this.init();
        this.setVisible(true);
    }

    public void init() {
        this.l_name = new JLabel("用户名", 0);
        this.l_type = new JLabel("用户类型", 0);
        this.l_password = new JLabel("密码", 0);
        this.t_name = new JTextField();
        this.c_type = new JComboBox();
        this.c_type.addItem("管理员");
        this.c_type.addItem("普通用户");
        this.p_password = new JPasswordField();
        this.b_login = new JButton("登录");
        this.b_reset = new JButton("重置");
        this.b_register = new JButton("点我注册");
        this.b_register.addActionListener((e) -> {
            new RegisterFrame();
        });
        this.setLayout((LayoutManager)null);
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(3, 2, 5, 5));
        p.add(this.l_name);
        p.add(this.t_name);
        p.add(this.l_type);
        p.add(this.c_type);
        p.add(this.l_password);
        p.add(this.p_password);
        p.setBounds(5, 5, 375, 185);
        this.add(p);
        p = new JPanel();
        p.setLayout(new GridLayout(1, 3, 5, 5));
        p.add(this.b_login);
        p.add(this.b_reset);
        p.add(this.b_register);
        p.setBounds(5, 195, 375, 60);
        this.add(p);
    }
}

剩余三个窗口类都与之类似,分别是:

注册窗口


import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class RegisterFrame extends JFrame {
    private JLabel account;
    private JLabel name;
    private JLabel password;
    private JLabel confirmpass;
    private JLabel sex;
    private JLabel city;
    private JLabel type;
    private JTextField t_account;
    private JTextField t_name;
    private JButton regedit;
    private JButton reset;
    private JPasswordField t_password;
    private JPasswordField t_confirmspass;
    private JRadioButton t_sex1;
    private JRadioButton t_sex2;
    private JComboBox t_city;
    private JComboBox c_type;

    public RegisterFrame() {
        this.setTitle("电商注册平台");
        this.setSize(300, 450);
        this.setLocation(300, 300);
        this.init();
        this.setVisible(true);
    }

    public void init() {
        this.setLayout(new GridLayout(8, 2, 5, 5));
        this.account = new JLabel("账号", 0);
        this.add(this.account);
        this.t_account = new JTextField();
        this.add(this.t_account);
        this.name = new JLabel("姓名", 0);
        this.add(this.name);
        this.t_name = new JTextField();
        this.add(this.t_name);
        this.password = new JLabel("密码", 0);
        this.t_password = new JPasswordField();
        this.add(this.password);
        this.add(this.t_password);
        this.confirmpass = new JLabel("确认密码", 0);
        this.t_confirmspass = new JPasswordField();
        this.add(this.confirmpass);
        this.add(this.t_confirmspass);
        this.type = new JLabel("用户类型", 0);
        this.add(this.type);
        this.c_type = new JComboBox();
        this.c_type.addItem("管理员");
        this.c_type.addItem("普通用户");
        this.add(this.c_type);
        this.sex = new JLabel("性别", 0);
        this.add(this.sex);
        JPanel J = new JPanel();
        this.t_sex1 = new JRadioButton("男");
        this.t_sex2 = new JRadioButton("女");
        J.add(this.t_sex1);
        J.add(this.t_sex2);
        this.add(J);
        this.city = new JLabel("城市", 0);
        this.add(this.city);
        this.t_city = new JComboBox();
        this.t_city.addItem("长治");
        this.t_city.addItem("太原");
        this.t_city.addItem("运城");
        this.t_city.addItem("大同");
        this.add(this.t_city);
        this.regedit = new JButton("注册");
        this.add(this.regedit);
        this.regedit.addActionListener((e) -> {
            this.initdata();
        });
        this.reset = new JButton("重置");
        this.add(this.reset);
    }

    public void initdata() {
        String id = this.t_account.getText().trim();
        String name = this.t_name.getText().trim();
        String type = (String)this.c_type.getSelectedItem();
        char sex;
        if (this.t_sex1.isSelected()) {
            sex = 30007;
        } else {
            sex = 22899;
        }

        String city = (String)this.t_city.getSelectedItem();
        String password = null;
        if ((new String(this.t_password.getPassword())).equals(new String(this.t_confirmspass.getPassword()))) {
            password = new String(this.t_password.getPassword());
            user u = new user(id, type, password, name, sex, city);
            new UserInfoFrame(u);
        } else {
            JOptionPane.showMessageDialog(this, "前后输入密码不一致!请重新输入。", "警告对话框", 2);
            this.t_password.setText("");
            this.t_confirmspass.setText("");
        }

    }
}

书籍商品窗口 


import java.awt.GridLayout;
import java.awt.LayoutManager;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class BookInfoFrame extends JFrame {
    private JTable t_book;
    private JLabel welcomecontent1;
    private JLabel welcomecontent2;
    private JLabel bookname;
    private JLabel bookkind;
    private JTextField t_name;
    private JComboBox t_kind;
    private JButton query;
    private JPanel J1;
    private JPanel J2;
    private JPanel J3;
    private JPanel J4;

    public BookInfoFrame(Category[] C, user u) {
        this.setLayout(new GridLayout(7, 3));
        this.setTitle("电商购物平台-商品查询页面");
        this.setSize(500, 500);
        this.setLocation(500, 200);
        String temp = null;
        if (u.getSex() == 30007) {
            temp = "先生";
        } else if (u.getSex() == 22899) {
            temp = "女士";
        }

        this.setLayout((LayoutManager)null);
        this.J1 = new JPanel();
        this.J1.setLayout(new GridLayout(1, 2));
        String var10003 = u.getName();
        this.welcomecontent1 = new JLabel("您好," + var10003 + temp);
        this.welcomecontent2 = new JLabel("来自:" + u.getCity());
        this.J1.add(this.welcomecontent1);
        this.J1.add(this.welcomecontent2);
        this.J1.setBounds(15, 5, 800, 20);
        this.add(this.J1);
        this.bookname = new JLabel("书籍名称:", 0);
        this.t_name = new JTextField();
        this.bookkind = new JLabel("书籍种类:", 0);
        this.t_kind = new JComboBox();
        this.t_kind.addItem("小说类");
        this.t_kind.addItem("工具类");
        this.query = new JButton("查询");
        this.query.addActionListener((e) -> {
            this.showmessage();
        });
        this.J2 = new JPanel();
        this.J2.setLayout(new GridLayout(1, 5, 5, 5));
        this.J2.add(this.bookname);
        this.J2.add(this.t_name);
        this.J2.add(this.bookkind);
        this.J2.add(this.t_kind);
        this.J2.add(this.query);
        this.J2.setBounds(5, 45, 450, 30);
        this.add(this.J2);
        this.init(C);
        this.setVisible(true);
    }

    public void init(Category[] C) {
        this.J3 = new JPanel();
        Object[] t_title = new Object[]{"书籍编号", "书籍名称", "书籍作者", "库存", "书籍分类"};
        Object[][] books = new Object[C.length][5];

        for(int i = 0; i < C.length; ++i) {
            books[i][0] = C[i].getId();
            books[i][1] = C[i].getName();
            books[i][2] = C[i].getAuthor();
            books[i][3] = C[i].getNumber();
            Object[] var10000 = books[i];
            String var10002 = C[i].getFirstlevel();
            var10000[4] = var10002 + "->" + C[i].getSecondlevel();
        }

        this.t_book = new JTable(books, t_title);
        this.J3.add(new JScrollPane(this.t_book));
        this.J3.setBounds(6, 100, 480, 300);
        this.add(this.J3);
        JButton buy = new JButton("购买");
        buy.addActionListener((e) -> {
            this.warn();
        });
        buy.setBounds(370, 420, 80, 30);
        this.add(buy);
    }

    public void showmessage() {
        JOptionPane.showMessageDialog(this, "表格已经做出来了,别不识好歹(doge),请使用SQL server去处理哦亲(^_^)!", "警告对话框", 2);
    }

    public void warn() {
        JOptionPane.showMessageDialog(this, "你没有钱!", "警告对话框", 2);
    }
}

管理者窗口 



import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class UserInfoFrame extends JFrame {
    private JTable t_user;

    public UserInfoFrame(user u) {
        this.setTitle("电商购物-注册用户信息");
        this.setSize(500, 400);
        this.setLocation(1000, 200);
        this.init(u);
        this.setVisible(true);
    }

    public void init(user u) {
        Object[] t_title = new Object[]{"用户名", "姓名", "密码", "性别", "城市"};
        Object[][] users = new Object[][]{{u.getId(), u.getName(), u.getPassword(), u.getSex(), u.getCity()}};
        this.t_user = new JTable(users, t_title);
        this.add(new JScrollPane(this.t_user));
        Category[] c = new Category[]{new Category("b01", "Java核心技术", "霍斯特曼", 50, "工具类", "软件编程"), new Category("b02", "名著-三国演义", "罗贯中", 40, "小说类", "历史"), new Category("b03", "名著-水浒传", "施耐庵", 30, "小说类", "历史"), new Category("b04", "名著-红楼梦", "曹雪芹", 20, "小说类", "历史")};
        new BookInfoFrame(c, u);
    }
}

成果: 

第二个电商购物平台

在第一个电商购物平台进行完善,解决一下普通用户不能直接登录的问题,其次在商品界面进行优化: 

(1)提供购买商品并修改商品库存的功能

(2)提供移除商品并恢复商品库存的功能

(3)用户可以查看购物车已购买商品的详细信息

代码:

首先我们确定好软件包——可以分为entry和frame

 在entry中存放数据

在exception存放异常,等待抛出异常类

在Frame存放界面

在entry包中,book类(商品书籍类),user类(用户类)groupclass类(购物车类)与第一个购物平台的数据类情况一致

book类

package shop.entry;
public class Book {
    private String id,name,author,firstlevel,secondlevel;
    private int number;
    private float price;

    public Book(String id, String name, String author, String firstlevel, String secondlevel, int number, float price) {
        this.id = id;
        this.name = name;
        this.author = author;
        this.firstlevel = firstlevel;
        this.secondlevel = secondlevel;
        this.number = number;
        this.price = price;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    public String getFirstlevel() {
        return firstlevel;
    }

    public void setFirstlevel(String firstlevel) {
        this.firstlevel = firstlevel;
    }

    public String getSecondlevel() {
        return secondlevel;
    }

    public void setSecondlevel(String secondlevel) {
        this.secondlevel = secondlevel;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }
}

groupclass类

package shop.entry;
public class Groupclass {
    public String name;
    public Float price;
    public int num;
    public Float Totle;

    public Groupclass(String name, Float price, int num, Float totle) {
        this.name = name;
        this.price = price;
        this.num = num;
        this.Totle = totle;
    }
}

user类

package shop.entry;

import shop.entry.ShoppingCard;

public class User {
    private String id,type,password,name;
    private char sex;
    private String city;

    private ShoppingCard S;

    public ShoppingCard getS() {
        return S;
    }

    public void setS(ShoppingCard s) {
        S = s;
    }

    public User(String id, String type, String password, String name, char sex, String city) {
        this.id = id;
        this.type = type;
        this.password = password;
        this.name = name;
        this.sex = sex;
        this.city = city;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", type='" + type + '\'' +
                ", password='" + password + '\'' +
                ", name='" + name + '\'' +
                ", sex=" + sex +
                ", city='" + city + '\'' +
                '}';
    }
}

entry还需要一个添加购物车购买时候的抛出异常窗口,用throw抛出,exception创立了一个异常包,所以说代码如下

shoppingcard类为(先导入exception包):

package shop.entry;

import shop.exception.CartException;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class ShoppingCard extends HashMap<Book,Integer> {
    public void add(Book B, int num) throws CartException {
        if (B.getNumber() >= num) {
            if (this.containsKey(B)) {
                this.replace(B, this.get(B) + num);
                B.setNumber(B.getNumber() - num);
            } else {
                this.put(B, num);
                B.setNumber(B.getNumber() - num);
            }
        } else {
            throw new CartException("库存不足!,这本书编号为" + B.getId() + "书名为" + B.getName() + ",库存还有" + B.getNumber() + "本。");
        }
    }

    public void remove(Book B,int num) throws CartException {
        if (this.get(B) >= num) {
            this.replace(B, this.get(B) - num);
            B.setNumber(B.getNumber() + num);
        } else {
            throw new CartException(B.getName() + "这本书在您的购物车共" + this.get(B) +"本,请重新操作!");
        }
    }

    public int countBookNum() {
        int cnt = 0;
        Iterator<Integer> itervalue = this.values().iterator();
        while (itervalue.hasNext()) {
            cnt += itervalue.next();
        }
        return cnt;
    }

    public float countBookPrice() {
        float price = 0;
        Iterator<Map.Entry<Book, Integer>> e = this.entrySet().iterator();
        while (e.hasNext()) {
            Map.Entry<Book,Integer> I = e.next();
            price += I.getKey().getPrice() * I.getValue();
        }
        return price;
    }


    @Override
    public String toString() {
        return "购物车中共有" + countBookNum() + "本书,共花费" + countBookPrice() + "元。";
    }
}

异常包中的异常类CartException为:

package shop.exception;
public class CartException extends Exception {
    private String info;
    public CartException() {}

    public CartException(String info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return info;
    }
}

下面我们就来完成窗口包的全部内容,和第一个情况类似,细看就可看懂

LoginFrame类:在普通用户中,点击登录如果正确即可跳转book窗口

        b_login.addActionListener(e -> {
            boolean flag = false;
            for (User u : UU) {
                if (u.getName().equals(t_name.getText())) {
                    flag = true;
                    if (new String(p_password.getPassword()).equals(u.getPassword())) {
                        if(c_type.getSelectedItem().toString().equals("普通用户")) {
                            new BookInfoFrame(u);
                        } else {
                            new AdminFrame(u);
                            JOptionPane.showMessageDialog(this,"管理员窗口等待下次更新","警告窗口",JOptionPane.WARNING_MESSAGE);
                        }
                    } else {
                        warning();
                    }
                }
            }
            if(flag == false) {
                showinfo();
            }
        });

完整代码如下: 

package shop.Frame;

import shop.entry.Book;
import shop.entry.User;

import javax.swing.*;
import java.awt.*;

import static shop.Frame.BookInfoFrame.BB;
import static shop.Frame.RegisterFrame.UU;
public class LoginFrame extends JFrame {
    public static void main(String[] args) {
        Book b1 = new Book("b0001","数据结构与算法","严蔚敏","工具类" , "算法", 10,34.2f);
        Book b2 = new Book("b0002","JAVA编程","李华玲","工具类" , "教科书" , 10,34.1f);
        Book b3 = new Book("b0003","西游记","吴承恩","小说类" , "名著" ,10,12.2f);
        Book b4 = new Book("b0004","悲惨世界","雨果","小说类" , "名著",10,36.3f);
        BB.add(b1);
        BB.add(b2);
        BB.add(b3);
        BB.add(b4);
        new LoginFrame();
    }

    private JLabel l_name, l_type, l_password;
    private JTextField t_name;
    private JComboBox<String> c_type;
    private JPasswordField p_password;

    private JButton b_login, b_reset, b_register;

    public LoginFrame() {
        this.setSize(400, 300);
        this.setTitle("电商购物-登录页面");
        this.setLocation(300, 200);
        init();
        this.setVisible(true);
    }

    public void init() {
        l_name = new JLabel("用户名", JButton.CENTER);
        l_type = new JLabel("用户类型", JButton.CENTER);
        l_password = new JLabel("密码", JButton.CENTER);
        t_name = new JTextField();
        c_type = new JComboBox<>();
        c_type.addItem("管理员");
        c_type.addItem("普通用户");
        p_password = new JPasswordField();

        b_login = new JButton("登录");
        b_login.addActionListener(e -> {
            boolean flag = false;
            for (User u : UU) {
                if (u.getName().equals(t_name.getText())) {
                    flag = true;
                    if (new String(p_password.getPassword()).equals(u.getPassword())) {
                        if(c_type.getSelectedItem().toString().equals("普通用户")) {
                            new BookInfoFrame(u);
                        } else {
                            new AdminFrame(u);
                            JOptionPane.showMessageDialog(this,"管理员窗口等待下次更新","警告窗口",JOptionPane.WARNING_MESSAGE);
                        }
                    } else {
                        warning();
                    }
                }
            }
            if(flag == false) {
                showinfo();
            }
        });
        b_reset = new JButton("重置");
        b_reset.addActionListener(e -> {
            t_name.setText("");
        });
        b_register = new JButton("点我注册");

        b_register.addActionListener(e -> new RegisterFrame());
        this.setLayout(null);

        JPanel p = new JPanel();
        p.setLayout(new GridLayout(3, 2, 5, 5));

        p.add(l_name);
        p.add(t_name);

        p.add(l_type);
        p.add(c_type);

        p.add(l_password);
        p.add(p_password);

        p.setBounds(5, 5, 375, 185);
        this.add(p);

        p = new JPanel();
        p.setLayout(new GridLayout(1, 3, 5, 5));
        p.add(b_login);
        p.add(b_reset);
        p.add(b_register);

        p.setBounds(5, 195, 375, 60);

        this.add(p);
    }

    public void warning() {
        JOptionPane.showMessageDialog(this, "密码不一致!请重新输入。", "警告对话框", JOptionPane.WARNING_MESSAGE);
    }

    public void showinfo() {
        JOptionPane.showMessageDialog(this,"账号不存在!请注册","警告对话框",JOptionPane.WARNING_MESSAGE);
    }
}


其他窗口分别为:

AdminFrame:
package shop.Frame;
import shop.entry.User;
import javax.swing.*;
public class AdminFrame extends JFrame {
    public AdminFrame(User u) {
        this.setSize(400, 500);
        this.setTitle("管理员菜单");
        this.setLocation(300, 200);
        this.setLayout(null);
        init(u);
        this.setVisible(true);
    }

    public void init(User u) {
        String temp = null;
        if(u.getSex() == '男') {
            temp = "先生";
        } else if(u.getSex() == '女') {
            temp = "女士";
        }
        JLabel welcome = new JLabel("欢迎您," + u.getName() + temp + "来自" + u.getCity());
        welcome.setBounds(5,5,300,30);
        this.add(welcome);
    }
}
BookInfoFrame
package shop.Frame;
import shop.entry.Book;
import shop.entry.User;
import javax.swing.*;
import java.awt.*;
import java.util.HashSet;
public class BookInfoFrame extends JFrame {

    public static HashSet<Book> BB = new HashSet<Book>();

    private JTable t_book;
    private JLabel welcomecontent1, welcomecontent2, bookname, bookkind;
    private JTextField t_name;
    private JComboBox t_kind;
    private JButton query;
    private JPanel J1, J2, J3;

    public BookInfoFrame(User u) {
        this.setLayout(new GridLayout(7, 3));
        this.setTitle("电商购物平台-商品查询页面");
        this.setSize(500, 500);
        this.setLocation(500, 200);
        String temp = null;
        if (u.getSex() == '男') {
            temp = "先生";
        } else if (u.getSex() == '女') {
            temp = "女士";
        }
        this.setLayout(null);
        J1 = new JPanel();
        J1.setLayout(new GridLayout(1, 2));
        welcomecontent1 = new JLabel("您好," + u.getName() + temp);
        welcomecontent2 = new JLabel("来自:" + u.getCity());
        J1.add(welcomecontent1);
        J1.add(welcomecontent2);
        J1.setBounds(15, 5, 800, 20);
        this.add(J1);
        bookname = new JLabel("书籍名称:", JLabel.CENTER);
        t_name = new JTextField();
        bookkind = new JLabel("书籍种类:", JLabel.CENTER);
        t_kind = new JComboBox<String>();
        t_kind.addItem("小说类");
        t_kind.addItem("工具类");
        query = new JButton("查询");
        query.addActionListener(e -> {
            Book[] b = new Book[BB.size()];
            for(int i = 0;i < BB.size();i++) {
                BB.toArray(b);
            }
            new FindresultFrame(b,t_kind.getSelectedItem().toString());
        });
        J2 = new JPanel();
        J2.setLayout(new GridLayout(1, 5, 5, 5));
        J2.add(bookname);
        J2.add(t_name);
        J2.add(bookkind);
        J2.add(t_kind);
        J2.add(query);
        J2.setBounds(5, 45, 450, 30);
        this.add(J2);
        init();
        JButton buy = new JButton("购买");
        buy.addActionListener(e->{
            new BuyFrame(u);
        });
        buy.setBounds(370, 420, 80, 30);
        this.add(buy);
        this.setVisible(true);
    }

    public void init() {
        J3 = new JPanel();
        Book[] B = new Book[BB.size()];
        for (int i = 0; i < BB.size(); i++) {
            BB.toArray(B);
        }
        Object[] titie = {"书籍编号", "书籍名称", "书籍作者", "书籍数量", "单价", "书籍分类"};
        Object[][] books = new Object[B.length][6];
        for (int i = 0; i < B.length; i++) {
            books[i][0] = B[i].getId();
            books[i][1] = B[i].getName();
            books[i][2] = B[i].getAuthor();
            books[i][3] = B[i].getNumber();
            books[i][4] = B[i].getPrice() + "¥";
            books[i][5] = B[i].getFirstlevel() + ">" + B[i].getSecondlevel();
        }
        t_book = new JTable(books, titie);
        J3.add(new JScrollPane(t_book));
        J3.setBounds(2, 100, 490, 300);
        this.add(J3);
    }
}

BuyFrame
package shop.Frame;

import shop.entry.Book;
import shop.entry.ShoppingCard;
import shop.entry.User;
import shop.exception.CartException;

import javax.swing.*;

import java.awt.*;

import static shop.Frame.BookInfoFrame.BB;
public class BuyFrame extends JFrame {

    private JComboBox<String> p;
    private JTextField num;

    public static ShoppingCard S = new ShoppingCard();
    public BuyFrame(User u) {
        this.setSize(330, 150);
        this.setLocation(200,200);
        this.setLayout(null);
        this.setTitle("购物车菜单");
        this.setLocation(300, 200);
        init(u);
        this.setVisible(true);
    }

    public void init(User u) {
        JPanel J = new JPanel();
        J.setLayout(new GridLayout(1,4));
        p = new JComboBox<String>();
        Book[] b = new Book[BB.size()];
        for(int i = 0;i < BB.size();i++) {
            BB.toArray(b);
        }
        for(int i = 0;i < b.length;i++) {
            p.addItem(b[i].getId());
        }
        JLabel mess = new JLabel("数量",JLabel.CENTER);
        num = new JTextField("0");
        JButton add = new JButton("添加");
        add.addActionListener(e->{
            u.setS(S);
            insert(u);
        });
        JButton del = new JButton("移除");
        del.addActionListener(e->{
            u.setS(S);
            delete(u);
        });
        J.add(p);
        J.add(mess);
        J.add(num);
        J.add(add);
        J.add(del);
        J.setBounds(5,5,300,40);
        this.add(J);

    }

    public void insert(User u) {
        String temp = (String) p.getSelectedItem();
        for(Book I : BB) {
            if(I.getId().equals(temp)) {
                try {
                    S.add(I, Integer.parseInt(num.getText()));
                } catch (CartException e) {
                    JOptionPane.showMessageDialog(this,e.toString(),"警告对话框",JOptionPane.WARNING_MESSAGE);
                } finally {
                    JOptionPane.showMessageDialog(this,u.getS().toString(),"提示",JOptionPane.CANCEL_OPTION);
                }
            }
        }
        new BookInfoFrame(u);
        new ShoppingFrame(u);
    }

    public void delete(User u) {
        String temp = (String) p.getSelectedItem();
        for(Book I : BB) {
            if(I.getId().equals(temp)) {
                try {
                    S.remove(I, Integer.parseInt(num.getText()));
                } catch (CartException e) {
                    JOptionPane.showMessageDialog(this,e.toString(),"警告对话框",JOptionPane.WARNING_MESSAGE);
                } finally {
                    JOptionPane.showMessageDialog(this,u.getS().toString(),"提示",JOptionPane.CANCEL_OPTION);
                }
            }
        }
        new BookInfoFrame(u);
        new ShoppingFrame(u);
    }

}

FindresultFrame
package shop.Frame;
import shop.entry.Book;

import javax.swing.*;

public class FindresultFrame extends JFrame {
    public FindresultFrame(Book b[],String str) {
        JPanel j = new JPanel();
        Book[] c = new Book[b.length];
        int cnt = 0 ;
        this.setSize(500, 200);
        this.setLocation(200,200);
        this.setTitle("查询结果");
        for (int i = 0; i < b.length; i++) {
            if(b[i].getFirstlevel().equals(str)) {
                c[cnt++] = b[i];
            }
        }
        this.setLocation(300, 200);
        this.setLayout(null);
        Object[] titie = {"书籍编号", "书籍名称", "书籍作者", "书籍数量", "单价", "书籍分类"};
        Object[][] books = new Object[cnt][6];
        for (int i = 0; i < cnt; i++) {
            books[i][0] = c[i].getId();
            books[i][1] = c[i].getName();
            books[i][2] = c[i].getAuthor();
            books[i][3] = c[i].getNumber();
            books[i][4] = c[i].getPrice() + "¥";
            books[i][5] = c[i].getSecondlevel();
        }
        JTable t_book = new JTable(books, titie);
        j.add(new JScrollPane(t_book));
        j.setBounds(2, 10, 490, 100);
        this.add(j);
        this.setVisible(true);
    }
}
RegisterFrame 
package shop.Frame;

import shop.entry.User;

import javax.swing.*;
import java.awt.*;
import java.util.HashSet;
public class RegisterFrame extends JFrame {
    public static HashSet<User> UU = new HashSet<User>();
    private JLabel account, name, password, confirmpass, sex, city, type;
    private JTextField t_account, t_name;
    private JButton regedit, reset;
    private JPasswordField t_password, t_confirmspass;
    private JRadioButton t_sex1, t_sex2;

    private JComboBox t_city, c_type;

    public RegisterFrame() {
        this.setTitle("电商注册平台");
        this.setSize(300, 450);
        this.setLocation(300, 300);
        init();
        this.setVisible(true);
    }

    public void init() {
        this.setLayout(new GridLayout(8, 2, 5, 5));
        account = new JLabel("账号", JLabel.CENTER);
        this.add(account);
        t_account = new JTextField();
        this.add(t_account);
        name = new JLabel("姓名", JLabel.CENTER);
        this.add(name);
        t_name = new JTextField();
        this.add(t_name);
        password = new JLabel("密码", JLabel.CENTER);
        t_password = new JPasswordField();
        this.add(password);
        this.add(t_password);
        confirmpass = new JLabel("确认密码", JLabel.CENTER);
        t_confirmspass = new JPasswordField();
        this.add(confirmpass);
        this.add(t_confirmspass);
        type = new JLabel("用户类型", JLabel.CENTER);
        this.add(type);
        c_type = new JComboBox<String>();
        c_type.addItem("管理员");
        c_type.addItem("普通用户");
        this.add(c_type);
        sex = new JLabel("性别", JLabel.CENTER);
        this.add(sex);
        JPanel J = new JPanel();
        t_sex1 = new JRadioButton("男");
        t_sex2 = new JRadioButton("女");
        J.add(t_sex1);
        J.add(t_sex2);
        this.add(J);
        city = new JLabel("城市", JLabel.CENTER);
        this.add(city);
        t_city = new JComboBox<String>();
        t_city.addItem("长治");
        t_city.addItem("太原");
        t_city.addItem("运城");
        t_city.addItem("大同");
        this.add(t_city);
        regedit = new JButton("注册");
        this.add(regedit);
        regedit.addActionListener(e -> initdata());

        reset = new JButton("重置");
        this.add(reset);
    }

    public void initdata() {
        String id = t_account.getText().trim();
        String name = t_name.getText().trim();
        String type = (String) c_type.getSelectedItem();
        char sex;
        if (t_sex1.isSelected()) {
            sex = '男';
        } else {
            sex = '女';
        }
        String city = (String) t_city.getSelectedItem();
        if (new String(t_password.getPassword()).equals(new String(t_confirmspass.getPassword()))) {
            String password = new String(t_password.getPassword());
            UU.add(new User(id, type, password, name, sex, city));
            JOptionPane.showMessageDialog(this, "注册成功!请登录。", "提示", JOptionPane.CLOSED_OPTION);
        } else {
            JOptionPane.showMessageDialog(this, "前后输入密码不一致!请重新输入。", "警告对话框", JOptionPane.WARNING_MESSAGE);
            t_password.setText("");
            t_confirmspass.setText("");
        }
    }
}
ShoppingFrame
package shop.Frame;

import shop.entry.Book;
import shop.entry.Groupclass;
import shop.entry.User;

import javax.swing.*;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
public class ShoppingFrame extends JFrame {
    public ShoppingFrame(User u) {
        this.setSize(500, 300);
        this.setTitle("购物车详情");
        this.setLocation(500, 500);
        init(u);
        this.setVisible(true);
    }

    public void init(User u) {
        HashSet<Groupclass> K = new HashSet<Groupclass>();
        JPanel J3 = new JPanel();
        Object[] titie = {"名称", "单价", "数量", "总价"};
        Object[][] goods = new Object[u.getS().size()][4];
        Iterator<Map.Entry<Book, Integer>> I = u.getS().entrySet().iterator();
        while (I.hasNext()) {
            Map.Entry<Book, Integer> entry = I.next();
            K.add(new Groupclass(entry.getKey().getName(),entry.getKey().getPrice(),entry.getValue(),entry.getKey().getPrice() * entry.getValue()));
        }
        Groupclass[] g = new Groupclass[K.size()];
        for (int i = 0; i < K.size(); i++) {
            K.toArray(g);
        }
        for (int i = 0; i < K.size(); i++) {
            goods[i][0] = g[i].name;
            goods[i][1] = g[i].price;
            goods[i][2] = g[i].num;
            goods[i][3] = g[i].Totle + "¥";
        }
        JTable t_goods = new JTable(goods, titie);
        J3.add(new JScrollPane(t_goods));
        J3.setBounds(2, 100, 490, 300);
        this.add(J3);
    }
}

最终的成果:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/42415.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

链夹式烟苗注水移栽机的总体设计

目 录 1 引言 1 1.1课题来源及研究的目的和意义 1 1.2农艺要求 1 1.3链夹式烟苗注水移栽机的发展现状 1 1.4研究内容 3 2 链夹式烟苗注水移栽机的设计方案 4 2.1链夹式烟苗注水移栽机总体方案设计思路 4 2.2链夹式烟苗注水移栽机工作原理 5 3 链夹式烟苗注水移栽机具体设计 6 3…

带你入门HTML+CSS网页设计,编写网页代码的思路

带你入门HTMLCSS网页设计&#xff0c;编写网页代码的思路 这篇文章主要给大家详细解释一下这些代码的作用和意义&#xff0c;以及编写网页代码的格式与思路。 下面我贴上html代码&#xff1a; <!--HTML--> <div> <h2>这是我的第一个网页</h2> <p&…

Metabase学习教程:仪表盘-3

自定义联动&#xff1a;选择当人们单击仪表盘中的图表时会发生什么 您可以设置仪表盘部件以将用户导航到仪表盘、保存的问题和URL&#xff0c;并使用仪表盘中的值更新目标仪表盘的筛选器&#xff0c;或参数化指向外部站点的链接。 Metabase提供了一些简单的构建块&#xff0c…

谷粒学院——Day08【课程发布-课程大纲和课程发布】

富文本编辑器Tinymce 一、Tinymce可视化编辑器 参考 https://panjiachen.gitee.io/vue-element-admin/#/components/tinymce https://panjiachen.gitee.io/vue-element-admin/#/example/create 二、组件初始化 Tinymce是一个传统javascript插件&#xff0c;默认不能用于V…

MySQL安装部署

1、卸载mariadb 查看是否有默认的mariadbrpm -qa|grep mariadb 如果有&#xff0c;卸载rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64&#xff0c;然后删除etc目录下的my.cnfrm -rf /etc/my.cnf&#xff08;注意这里需要确定tar包里是否有默认的cnf文件&#xff0c;在su…

NLP实践!文本语法纠错模型实战,搭建你的贴身语法修改小助手 ⛵

&#x1f4a1; 作者&#xff1a;韩信子ShowMeAI &#x1f4d8; 深度学习实战系列&#xff1a;https://www.showmeai.tech/tutorials/42 &#x1f4d8; 自然语言处理实战系列&#xff1a;https://www.showmeai.tech/tutorials/45 &#x1f4d8; 本文地址&#xff1a;https://sho…

AI智能机器人的测评以及部署

作为业内人士&#xff0c;今天给大家测评下电销机器人。究竟是什么样的电销机器人才是让客户满意的&#xff1f; 咱们先来说说电销机器人到底是什么&#xff1f; 相信很多人都对电销机器人没有过多的了解。甚至还有人会问&#xff1b;“什么&#xff1f;机器人&#xff1f;啥样…

两万字详细解读AQS,你真的了解它吗?

1、JUC的由来 synchronized 关键字是JDK官方人员用C代码写的&#xff0c;在JDK6以前是重量级锁。Java大牛 Doug Lea对 synchronized 在并发编程条件下的性能表现不满意就自己写了个JUC&#xff0c;以此来提升并发性能&#xff0c;本文要讲的就是JUC并发包下的AbstractQueuedSy…

Listen、Attention、Spell模型

LAS是一个做语音识别的经典seq2seq模型&#xff0c;主要分为三个部分Listen、Attention、Spell Listen Listen部分就是一个encoder。 输入声学特征向量&#xff0c;提取信息、消除噪声&#xff0c;输出向量。 encoder可以是RNN 也可以是CNN。比较常见的是先用CNN&#xff0…

第三章变量

第三章变量 查看javaAPI文档的网址&#xff1a; https://www.matools.com/ 3.1程序中号的使用 1&#xff09;当左右两边都是数值型时&#xff0c;则做加法运算 2&#xff09;当左右两边有一方为字符串&#xff0c;则做拼接运算 3&#xff09;运算顺序从左到右 System,out.prin…

Springboot+vue校园新闻网站idea

将系统需求进行分析总结&#xff0c;系统需求如下&#xff1a; 系统可以运行在Windows操作系统平台上&#xff0c;并通过友好的界面进行管理 系统用户分为游客&#xff0c;登录用户&#xff0c;管理员 游客可以浏览新闻 游客可以浏览评论 管理员具有用户所有的权限 管理员还可以…

别再把Tableau、PowerBI吹上天了,在中国根本用不起来,看看为啥

工作业务相关&#xff0c;这几年接触BI较多&#xff0c;借此浅聊下我对BI工具以及市场的看法&#xff0c;原创禁止转载。 1、BI并不玄乎&#xff0c;本质就是实现简单数据分析和可视化的工具 很多人觉得BI玄乎&#xff0c;其实很大程度是因为BI厂家给造的名词太多了&#xff…

Ajax学习:如何在Chrome网络控制台查看通信报文(请求报文/响应报文)

第一步&#xff1a;F12开启控制台&#xff0c; 第二步骤&#xff1a;打开网络标签 然后刷新页面 在网络标签位置处&#xff0c;这时候会出现所有发送的请求 点击第一个&#xff1a;会出现内容 预览部分:是解析 观察解析结果处 标头headers:主要观察请求头和请求体部分 GET请…

saltstack 企业级实战

一、自动化运维工具对比 使用所需软件配置单个服务器是一项相当简单的任务。 但是,如果许多服务器需要安装相同或相似的软件和配置,则该过程将需要大量的工时才能完成,这会耗尽您本已紧张的资源。如果没有某种形式的自动化,这项任务几乎无法完成。考虑到这一任务,开发了新…

【SpringCloud】06 链路跟踪 Sleuth+zipkin

链路追踪 在大型系统的微服务化构建中&#xff0c;一个系统被拆分成了许多微服务。这些模块负责不同的功能&#xff0c;组合成系统&#xff0c;最终可以提供丰富的功能。在这种架构中&#xff0c;一次请求往往需要涉及到多个服务。互联网应用构建在不同的软件模块集上&#xf…

【附源码】计算机毕业设计JAVA智慧养老院管理系统

【附源码】计算机毕业设计JAVA智慧养老院管理系统 目运行 环境项配置&#xff1a; Jdk1.8 Tomcat8.5 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; JAVA …

Assignment写作抄袭常见形式怎么了解?

直接照抄他人的想法并且当做自己的Assignment上交&#xff0c;这是最简单的Assignment抄袭行为。实际上&#xff0c;形式更微妙的Assignment抄袭形式更为常见。今天我们为大家介绍英国Assignment抄袭常见的形式&#xff0c;帮助大家做好英国Assignment查重&#xff0c;避免Assi…

React源码分析(一)Fiber

前言 本次React源码参考版本为17.0.3。 React架构前世今生 查阅文档了解到&#xff0c; React16.x是个分水岭。 React15及之前 在16之前&#xff0c;React架构大致可以分为两层&#xff1a; Reconciler&#xff1a; 主要职责是对比查找更新前后的变化的组件&#xff1b;R…

TMS Logging提供了短日志输出

TMS Logging提供了短日志输出 TMS Logging Compact跨平台框架以最小的代码量为各种目标提供了短日志输出。 TMS记录惊人属性&#xff1a; 登录到一个或多个输出处理程序&#xff0c;如控制台、HTML、文本、文本、CSV文件、TCP/IP、浏览器、Windows事件日志等&#xff0c;。。。…

代码质量与安全 | 使用Incredibuild加速Klocwork静态代码分析

Klocwork是一款优秀的静态代码分析和SAST工具&#xff0c;适用于 C、C、C#、Java、JavaScript、Python和Kotlin&#xff0c;可识别软件安全性、质量和可靠性问题&#xff0c;帮助强制遵守标准。 Incredibuild是一款加速编译工具&#xff0c;为C代码编译和分析提供强大的分布式处…