2数据库结构2.1用户表
2.2商品表
2.3订单表2.4购物车表
3 实体类定义3.1User.javapublic class User {
String userName;
String password;
double account;
boolean identity;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
} public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getAccount() {
return account;
}
public void setAccount(double d) {
this.account = d;
}
public boolean getIdentity() {
return identity;
}
public void setIdentity(boolean b) {
this.identity = b;
}
}
3.1Goods.javapackage shop;
public class Goods {
String goodsID;
String goodsName;
int store;
double price;
String color;
String size;
String image;
String detail;
public String getGoodsID() {
return goodsID;
}
public void setGoodsID(String goodsID) {
this.goodsID = goodsID;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public int getStore() {
return store;
}
public void setStore(int store) {
this.store = store;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
3.3Shop.javapackage shop;
public class Shop {
private User user = new User();
private Goods goods = new Goods();
private int number;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Goods getGoods() {
return goods;
}
public void setGoods(Goods goods) {
this.goods = goods;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
3.4Order.java
package shop;
public class Order {
private User user=new User();
private Goods goods=new Goods();
private String orderID;
private String orderTime;
private int number;
private int pay;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Goods getGoods() {
return goods;
}
public void setGoods(Goods goods) {
this.goods = goods;
}
public String getOrderID() {
return orderID;
}
public void setOrderID(String orderID) {
this.orderID = orderID;
}
public String getOrderTime() {
return orderTime;
}
public void setOrderTime(String orderTime) {
this.orderTime = orderTime;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
4 功能类定义——图形界面实现4.1登录界面(1)布局框架采用网格布局3行1列,每一行放一个面板,面板采用流水布局,前两行(用户名、密码)采用左对齐,最后一行(按钮行)采用居中对齐。
图4-1-1登录界面布局图
(2)源代码public class Load extends JFrame implements ActionListener{
public Load() {
this.init();
this.setVisible(true);
this.setSize(400, 400);
this.setTitle("");
//this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}
public void init(){
this.setLayout(new GridLayout(3,1));
JPanel jp1=new JPanel();
load.addActionListener(this);
cancel.addActionListener(this);
zc.addActionListener(this);
}
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource()==load){
String userName = userField.getText();
System.out.println(userName);
String psw = pswField.getText();
String sql="select * from userinfo where UserName='"+userName+"'";
System.out.println(sql);
//加数据库操作代码
try{
ResultSet rs=ConnectionDB.selectData(sql);
if(!rs.next()){
JOptionPane.showMessageDialog(this,"用户名错误,此用户名不存在!", "系统消息", JOptionPane.INFORMATION_MESSAGE);
} else{
user.setUserName(rs.getString("" + "userName"));
//System.out.println(rs.getString("userName"));
user.setAccount(rs.getDouble("account"));
user.setPassword(rs.getString("password"));
user.setIdentity(rs.getBoolean("identity"));
//System.out.println(user.getIdentity());
if(user.getPassword().equals(psw)) {
if(user.isIdentity()) {
GoodsMaintain goodsMaintain=new GoodsMaintain();
}
else {
Main main=new Main(user);
}
this.dispose();
}
else {
JOptionPane.showMessageDialog(this, "密码错误", "系统消息", JOptionPane.INFORMATION_MESSAGE);
}
}
ConnectionDB.close();
} catch (SQLException e) {
e.printStackTrace();}
}else if(arg0.getSource()==cancel){
userField.setText("");
pswField.setText("");
}else if(arg0.getSource()==zc){
zhuce zcl=new zhuce(user);
}}
public static void main(String[] args) {
Load l=new Load();
}
}
(3)完成效果截图
图4-1-2登录界面效果图
4.2 商品信息维护界面(1)布局先采用边界布局分为中间C和南边S,南边S采用流水布局中间对齐,顺次摆放4个按钮,中间C再次采用边界布局分为中间CC和南边CS,CC采用网格布局分成4行1列网格,每个网格上流水布局左对齐,按顺次摆放控件;南边CS采用流水布局左对齐,将标签“商品描述”和文本域控件放上。
图4-2-1商品信息维护界面布局图
(2)源代码public class GoodsMaintain extends JFrame implements ActionListener{
public GoodsMaintain() {
// TODO Auto-generated constructor stub
this.init();
this.setVisible(true);
this.setSize(500, 500);
this.setTitle("");
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
id=IDField.getText();
//查找商品
if(e.getSource()==jbsearch){
if(id.equals("")){
JOptionPane.showMessageDialog(this,"商品ID不能为空","系统信息",JOptionPane.INFORMATION_MESSAGE);
}else{
sql="select * from goodsinfo where goodsID='"+id+"'";
rs=(ResultSet) ConnectionDB.selectData(sql);
try {
if(rs.next()){
nameField.setText(rs.getString("goodsName"));
priceField.setText(String.valueOf(rs.getDouble("price")));
imageField.setText(rs.getString("image"));
storeField.setText(String.valueOf(rs.getInt("store")));
colorBox.setEditable(true);
colorBox.setSelectedItem(rs.getString("color"));
sizeBox.setEditable(true);
sizeBox.setSelectedItem(rs.getString("size"));
detailArea.setText(rs.getString("detail"));
}else{
JOptionPane.showMessageDialog(this,"商品不存在","系统信息",JOptionPane.INFORMATION_MESSAGE);
}
ConnectionDB.close();
} catch (HeadlessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}else if(e.getSource()==jbupdate){
if(id.equals("")){
JOptionPane.showMessageDialog(this,"商品ID不能为空","系统信息",JOptionPane.INFORMATION_MESSAGE);
}else{
goodsName=nameField.getText();
price=Double.parseDouble(priceField.getText());
image=imageField.getText();
store=Integer.parseInt(storeField.getText());
detail=detailArea.getText();
sql="update goodsinfo set goodsName='"+goodsName+"',store='"+store+"',price='"+price+"',color='"+color+"',size='"+size+"',image='"+image+"',detail='"+detail+"'where goodsID='"+id+"'";
ConnectionDB.updateData(sql);
System.out .println(sql);
JOptionPane.showMessageDialog(this, "商品修改成功", "系统信息", JOptionPane.INFORMATION_MESSAGE);;
ConnectionDB.close();
}
}else if(e.getSource()==jbdelete){
if(id.equals("")){
JOptionPane.showMessageDialog(this,"商品ID不能为空","系统信息",JOptionPane.INFORMATION_MESSAGE);
}else{
sql="delete from goodsinfo where goodsID='"+id+"'";
ConnectionDB.updateData(sql);
JOptionPane.showMessageDialog(this, "商品删除成功", "系统信息", JOptionPane.INFORMATION_MESSAGE);;
ConnectionDB.close();
IDField.setText("");
nameField.setText("");
priceField.setText("");
imageField.setText("");
storeField.setText("");
colorBox.setEditable(true);
colorBox.setSelectedItem("");
sizeBox.setEditable(true);
sizeBox.setSelectedItem("");
detailArea.setText("");
}else if(e.getSource()==jbadd){
id=IDField.getText();
if(id.equals("")){
JOptionPane.showMessageDialog(this,"商品ID不能为空","系统信息",JOptionPane.INFORMATION_MESSAGE);
}else{
sql="select * from goodsinfo where goodsID='"+id+"'";
rs=(ResultSet) ConnectionDB.selectData(sql);
try {
if(rs.next()){
JOptionPane.showMessageDialog(this,"商品信息已存在","系统信息",JOptionPane.INFORMATION_MESSAGE);
ConnectionDB.close();
}else{
ConnectionDB.close();
goodsName=nameField.getText();
price=Double.parseDouble(priceField.getText());
image=imageField.getText();
store=Integer.parseInt(storeField.getText());
detail=detailArea.getText();
sql="insert into goodsinfo(goodsID,goodsName,store,price,color,size,image,detail) values('"+id+"','"+goodsName+"','"+store+"','"+price+"','"+color+"','"+size+"','"+image+"','"+detail+"')";
ConnectionDB.updateData(sql);
JOptionPane.showMessageDialog(this,"商品信息添加成功","系统信息",JOptionPane.INFORMATION_MESSAGE);
ConnectionDB.close();}
} catch (HeadlessException e1) {
e1.printStackTrace();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
(3)完成效果截图
图4-2-2商品信息维护界面效果图
4.3主页面(1)布局先采用边界布局分为北边N、中间C、西边W和南边S。北边N采用边界布局分为中间NC和南边NS,NC流水布局中间对齐,放jlLogo,NS采用1行两列网格布局,左侧网格流水布局左对齐,放标签jl,右侧网格流水布局右对齐,放标签、文本框nameField、按钮jbsearch。中间C采用2行3列网格布局,用循环放mp。西边W采用3行1列网格布局,每个网格流水布局中间对齐,放按钮jbadd、jbshop和jborder。南边S流水布局中间对齐放标签。
图4-3-1主页面布局图
(2)源代码 public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jbadd){
Rechange rechange=new Rechange(user);
}else if(e.getSource()==jbshop){
ShopSearch shopSearch=new ShopSearch(user);
}
else if(e.getSource()==jbsearch){
if(nameField.getText().trim().equals("")){
JOptionPane.showMessageDialog(this,"商品名称不为空","系统消息",JOptionPane.INFORMATION_MESSAGE);
}else{
sql="select * from goodsinfo where goodsName='"+nameField.getText().trim()+"'";
System.out.println(sql);
//st=conn.createStatement();
//rs=st.executeQuery(sql);
rs=ConnectionDB.selectData(sql);//通过ConnectionDB连接
try {
if(rs.next()){//进行判断集合中是否有数据存在 rs.next()
goods.setGoodsID(rs.getString("goodsID"));
goods.setGoodsName(rs.getString("goodsName"));
goods.setStore(rs.getInt("store"));
goods.setPrice(rs.getDouble("price"));
goods.setColor(rs.getString("color"));
goods.setSize(rs.getString("size"));
goods.setImage(rs.getString("image"));
goods.setSize(rs.getString("size"));
goods.setDetail(rs.getString("detail"));
GoodsBuy GoodsBuy=new GoodsBuy(goods,user);
}else{
JOptionPane.showMessageDialog(this,"商品不存在","系统消息",JOptionPane.INFORMATION_MESSAGE);}
ConnectionDB.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else if(e.getSource()==(jborder)){
OrderAllSearch orderAllSearch=new OrderAllSearch(user);
}}
(3)完成效果截图
图4-3-2主页面完成效果图
4.4 商品购买页面(1)布局先采用边界布局分为中间C和南边S。中间C采用1行2列网格布局,左侧网格流水布局中间对齐,放标签jliamge,右侧网格采用7行1列网格布局,每一行再采用流水布局左对齐,将对应组件放上去。南边S流水布局中间对齐,将组件放上。
图4-4-1购买界面布局图
(2)源代码public class GoodsBuy extends JFrame implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jbadd){
sql="insert into shopinfo values('"+goods.getGoodsID()+"','"+goods.getGoodsName()+"','"+user.getUserName()+"','"+Integer.parseInt(nubmerField.getText())+"','"+goods.getColor()+"','"+goods.getSize()+"','"+goods.getPrice()+"')";
ConnectionDB.updateData(sql);
JOptionPane.showMessageDialog(this,"添加成功","系统消息",JOptionPane.INFORMATION_MESSAGE);
ConnectionDB.close();
}else if(e.getSource()==jborder){
//OrderSearch or = new OrderSearch(order,user,goods);
String orderid=null;
sql="select max(orderID) as id from orderinfo";
rs=ConnectionDB.selectData(sql);
try {
if(rs.next()){
System.out.println(orderid);
orderid=String.valueOf(Integer.parseInt(rs.getString("id"))+1);
}else{
orderid="10001";
System.out.println(orderid);
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ConnectionDB.close();
Date date=new Date();
String orderTime=String.valueOf(date.getYear()+1900);
orderTime=orderTime+"-"+String.valueOf(date.getMonth()+1);
orderTime=orderTime+"-"+String.valueOf(date.getDate());
sql="insert into orderinfo values('"+orderid+"','"+goods.getGoodsID()+"','"+goods.getGoodsName()+"','"+orderTime+"','"+user.getUserName()+"','"+Integer.parseInt(nubmerField.getText())+"','"+goods.getColor()+"','"+goods.getSize()+"','0','"+goods.getPrice()+"')";
System.out.println(sql);
ConnectionDB.updateData(sql);
ConnectionDB.close();
order.setOrderID(orderid);
System.out.println(orderid);
order.setGoods(goods);
order.setUser(user);
order.setNumber(Integer.parseInt(nubmerField.getText()));
order.setPay(0);
order.setOrderTime(orderTime);
OrderlSearch orderSearch=new OrderlSearch(order,user);
}
}
}
(3)完成效果截图
图4-4-2购买界面完成效果图
4.5 单笔订单支付界面(1)布局先采用边界布局分为北边N、中间C和南边S。北边N流水布局左对齐,放标签jluser。中间C采用4行2列网格布局,每一网格再采用流水布局左对齐,放置组件。南边S流水布局右对齐,将按钮jbdelete和jbpay放上。
图4-5-1单笔订单完成布局图
(2)源代码public class OrderlSearch extends JFrame implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jbdelete){
sql="delete from orderinfo where orderID='"+order.getOrderID()+"'";
ConnectionDB.updateData(sql);
JOptionPane.showMessageDialog(this, "删除成功", "系统消息", JOptionPane.INFORMATION_MESSAGE);
ConnectionDB.close();
}else if(e.getSource()==jbpay){
double account=user.getAccount();
double all=goods.getPrice()*order.getNumber();