使用Java语言编写一个模拟网上超市购物结算功能的程序,要求程序运行后有一个图形用户界面,可供用户输入购买的各种商品相关信息,最后给出用户的购物清单及总价格。
需求分析:
1.管理员添加商品以及其价格
2.用户购买商品打印订单信息以及结算订单
代码:
package SaleSys;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;
import java.sql.*;
class Goods{
public String[] name;
public Float[] price;
Goods(){
name =new String[100];
price=new Float[100];
}
}
public class SuperMarket extends Jframe{
public static void main(String[] args) throws SQLException{
MainWinow mainWin=new MainWinow("网上超市购物结算");
mainWin.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
mainWin.setBounds(300, 300, 500, 400);
mainWin.setVisible(true);
mainWin.setWin(mainWin);
mainWin.setMinWindowLayout();
}
}
class MainWinow extends Jframe{
Goods goods;
private JButton user;
private JButton manager;
private JLabel loginLabel;
private ManageWindow magWin;
private UserWindow userWin;
private Listener lis;
private MainWinow loginWin;
private int goodsNum;
private JLabel setNameLabel;
private JLabel setPriceLabel;
private JTextField setNameText;
private JTextField setPriceText;
private JButton inputBut;
private textarea inputArea;
private JButton returnBut1;
private JButton cancelBut;
private Vector buyItem;
private Float[] buyCount;
private int buyNum;
private JComboBox goodsCombox;
private JButton returnBut2;
private JLabel choiceGoodLabel;
private JLabel showPriceLabel;
private JTextField showPrice;
private textarea showChoice;
private JLabel showBuyNum;
private JTextField showBuyNumText;
private JButton submitBuy;
private JButton deleteBuyBut;
private JList choiceList;
private JButton countBut;
private Float sumMoney;
Statement stmt;
MainWinow(String winName) throws SQLException{
super(winName);
goodsNum=0;
buyNum=0;
sumMoney=(float)0;
goods=new Goods();
user=new JButton("我是用户");
manager=new JButton("我是管理员");
loginLabel=new JLabel("请选择角色!");
magWin=new ManageWindow("设置商品");
magWin.setBounds(300, 300, 500, 400);
magWin.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
userWin=new UserWindow("欢迎选购");
userWin.setBounds(300, 300, 500, 400);
userWin.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
lis=new Listener();
setNameLabel=new JLabel("商品名:");
setPriceLabel=new JLabel("价格:");
setNameText=new JTextField(5);
setPriceText=new JTextField(5);
inputBut=new JButton("确认添加");
inputArea=new textarea();
returnBut1=new JButton("返回");
cancelBut=new JButton("撤回添加");
goodsCombox=new JComboBox();
returnBut2=new JButton("返回");
choiceGoodLabel=new JLabel("请选择商品:");
showPriceLabel=new JLabel("价格");
showPrice=new JTextField(5);
showChoice=new textarea();
showBuyNum=new JLabel("购买数量:");
showBuyNumText=new JTextField(5);
submitBuy=new JButton("确认购买");
deleteBuyBut=new JButton("删除订单");
countBut=new JButton("订单结算");
choiceList=new JList();
buyItem=new Vector();
buyCount=new Float[100];
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String url= "jdbc:mysql://localhost:3306/device";
String user="root";
String password="zjq1314520";
Connection con=DriverManager.getConnection(url,user,password);
stmt = con.createStatement();
importSql();
}
public void importSql() throws SQLException {
int i=1;
// TODO Auto-generated method stub
ResultSet result=stmt.executeQuery(
"SELECT name,price FROM goods_info"
);
while(result.next()){
goods.name[i-1]=result.getString(1);
goods.price[i-1]=Float.parseFloat(result.getString(2));
i++;
}
goodsNum=i-1;
}
public void setWin(MainWinow w){
loginWin=w;
}
public void setMinWindowLayout(){
Container loginCon=new Container();
loginCon.setLayout(new FlowLayout());
loginCon.add(manager);
loginCon.add(user);
manager.addActionListener(lis);
user.addActionListener(lis);
this.setLayout(new BorderLayout());
this.add(loginLabel,BorderLayout.NORTH);
this.add(loginCon,BorderLayout.CENTER);
this.validate();
magWin.setLayout(new FlowLayout());
magWin.add(setNameLabel);
magWin.add(setNameText);
magWin.add(setPriceLabel);
magWin.add(setPriceText);
magWin.add(inputBut);
magWin.add(inputArea);
magWin.add(cancelBut);
magWin.add(returnBut1);
inputBut.addActionListener(lis);
returnBut1.addActionListener(lis);
cancelBut.addActionListener(lis);
userWin.setLayout(new BorderLayout());
Container userCon=new Container();
userCon.setLayout(new FlowLayout());
userCon.add(choiceGoodLabel);
userCon.add(goodsCombox);
userCon.add(showPriceLabel);
userCon.add(showPrice);
userCon.add(showBuyNum);
userCon.add(showBuyNumText);
userCon.add(submitBuy);
userWin.add(userCon,BorderLayout.NORTH);
//choiceList.setListData(goods.name);
userWin.add(choiceList,BorderLayout.CENTER);
userWin.add(new JScrollPane(choiceList));
Container butCon=new Container();
butCon.setLayout(new FlowLayout());
butCon.add(deleteBuyBut);
butCon.add(countBut);
butCon.add(returnBut2);
userWin.add(butCon,BorderLayout.SOUTH);
goodsCombox.addItemListener(
new ItemListener(){
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
int i=goodsCombox.getSelectedIndex();
if(i>=0)showPrice.setText(goods.price[i].toString());
}
}
);
returnBut2.addActionListener(lis);
submitBuy.addActionListener(lis);
deleteBuyBut.addActionListener(lis);
countBut.addActionListener(lis);
}
private void addComboxItem() {
// TODO Auto-generated method stub
for(int i=0;i0){
goodsNum--;
String deleteName=goods.name[goodsNum];
String deletePrice=goods.price[goodsNum].toString();
//System.out.println(deleteName);
String sql = "delete from goods_info where name = '"+deleteName+"' AND price ='"+deletePrice+"'";
try {
stmt.executeUpdate(sql);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//Connection con= DBManager .getConnection();;
//PreparedStatement ps = con.prepareStatement(sql);
addGoods();
}
}
if(e.getSource()==returnBut1){
loginWin.setVisible(true);
magWin.setVisible(false);
}
if(e.getSource()==returnBut2){
loginWin.setVisible(true);
userWin.setVisible(false);
}
if(e.getSource()==submitBuy){
if(!showBuyNumText.getText().equals("")){
buyCount[goodsCombox.getSelectedIndex()]=Float.parseFloat(showBuyNumText.getText());
String contentItem="";
Float sumMon=Float.parseFloat(showBuyNumText.getText())*(Float)goods.price[goodsCombox.getSelectedIndex()];
contentItem="商品名:"+goods.name[goodsCombox.getSelectedIndex()]+" "
+"单价:"+goods.price[goodsCombox.getSelectedIndex()].toString()+" "
+"购买数量:"+showBuyNumText.getText()+" "
+"总价:"+sumMon.toString();
buyItem.addElement(contentItem);
//buyItem[buyNum]=contentItem;
buyNum++;
choiceList.removeAll();
choiceList.setListData(buyItem);
sumMoney+=sumMon;
}
else{
JOptionPane.showMessageDialog(magWin,"购买数量不可以为空", "警告",JOptionPane.PLAIN_MESSAGE);
}
}
if(e.getSource()==deleteBuyBut){
if(choiceList.getSelectedValue()==null){
JOptionPane.showMessageDialog(magWin,"未选择待删除的物品", "警告",JOptionPane.PLAIN_MESSAGE);
}
else if(buyNum>0){
int i=choiceList.getSelectedIndex();
String selectItem=buyItem.get(i);
//System.out.println(selectItem);
String deletePrice="";
for(int j=0;j
删除有关数据库部分便可以在自己电脑上运行!
有关截图:
管理员界面:
用户界面:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



