目录
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);
}
}