Java编写网上超市购物结算功能程序
作者:zjq_1314520 发布时间:2021-10-29 13:55:52
标签:Java,网上超市,购物结算
使用Java语言编写一个模拟网上超市购物结算功能的程序,要求程序运行后有一个图形用户界面,可供用户输入购买的各种商品相关信息,最后给出用户的购物清单及总价格。
需求分析:
1.管理员添加商品以及其价格
2.用户购买商品打印订单信息以及结算订单
代码:
/*
* 创建者:张俊强
* 时间:2016/5/15
* */
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<String> 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<String>();
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;i<goodsNum;i++){
goodsCombox.addItem(goods.name[i]);
}
}
class Listener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==manager){
addGoods();
loginWin.setVisible(false);
magWin.setVisible(true);
}
if(e.getSource()==user){
loginWin.setVisible(false);
userWin.setVisible(true);
goodsCombox.removeAllItems();
addComboxItem();
}
if(e.getSource()==inputBut){
//String showOut="";
if(setNameText.getText().equals("")||setPriceText.getText().equals("")){
JOptionPane.showMessageDialog(magWin,"不可以有空余项!", "警告",JOptionPane.PLAIN_MESSAGE);
}
else{
goods.name[goodsNum]=setNameText.getText();
goods.price[goodsNum]=Float.parseFloat(setPriceText.getText());
try {
/*
* 写进数据库
* */
stmt.executeUpdate("insert into goods_info(name,price)values('"+goods.name[goodsNum]+"','"+(float)goods.price[goodsNum]+"')");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
goodsNum++;
addGoods();
setNameText.setText("");
setPriceText.setText("");
//showOut="商品名:"+setNameText.getText()+"\t"+"价格:"+setPriceText.getText()+"\n";
//inputArea.append(showOut);
}
}
if(e.getSource()==cancelBut){
if(goodsNum>0){
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<selectItem.length()-3;j++){
// System.out.println(selectItem.substring(j, j+3));
if(selectItem.substring(j, j+3).equals("总价:")){
deletePrice=selectItem.substring(j+3,selectItem.length());
System.out.println(deletePrice);
sumMoney-=Float.parseFloat(deletePrice);
break;
}
}
buyItem.remove(i);
choiceList.removeAll();
choiceList.setListData(buyItem);
choiceList.validate();
buyNum--;
}
else{
JOptionPane.showMessageDialog(magWin,"购物车为空,不可删除", "警告",JOptionPane.PLAIN_MESSAGE);
}
}
if(e.getSource()==countBut){//sumMoney
for(int i=0;i<buyItem.size();i++){
String str=buyItem.get(i).substring(0, 2);
if(str.equals("总价")){
buyItem.remove(i);
}
}
buyItem.addElement("总价:"+sumMoney.toString());
choiceList.removeAll();
choiceList.setListData(buyItem);
choiceList.validate();
}
}
private void addGoods() {
if(!inputArea.getText().equals(""))inputArea.setText("");
// TODO Auto-generated method stub
for(int i=0;i<goodsNum;i++){
String massage="商品名:"+goods.name[i]+"\t"+"价格:"+goods.price[i].toString()+"\n";
inputArea.append(massage);
}
}
}
}
class ManageWindow extends JFrame {
ManageWindow(String title){
super(title);
}
}
class UserWindow extends JFrame{
UserWindow(String title){
super(title);
}
}
删除有关数据库部分便可以在自己电脑上运行!
有关截图:
管理员界面:
用户界面:


猜你喜欢
- 目录示例1: EncryptByAes示例2: main示例3: wrapperPublicPriviteKeyTest示例4: initH
- 1.Java集合框架是什么?说出一些集合框架的优点?每种编程语言中都有集合,最初的Java版本包含几种集合类:Vector、Stack、Ha
- 之前在使用SpringBoot进行文件上传时,遇到了很多问题。于是在翻阅了很多的博文之后,总算将上传功能进行了相应的完善,便在这里记录下来,
- JAVA虽然是在C++基础上发展而来,但却对C++的许多缺陷有所改进,其中一个不得不提的就是字符串,我们知道,随着学习的深入,进入MFC时,
- java.lang.NoClassDefFoundError错误解决办法前言在日常Java开发中,我们经常碰到java.lang.NoCla
- namespace ConsoleApplication1{ using System; &n
- 本文实例讲述了Java数据结构之链表、栈、队列、树的实现方法。分享给大家供大家参考,具体如下:最近无意中翻到一本书,闲来无事写几行代码,实现
- 前言通常在工作中比较常用到的Microsoft Word是属于国外的文档内容编辑软件,其编译技术均属国外。而OFD是一种我国的自主文档格式,
- 前言我们了解数组这个概念之前,我们先思考下面几个问题。如果我们需要两个数据,那么直接创建两个变量即可int a;int b;如果需要五个数据
- 前言对于多线程,大家应该很熟悉。但是,大家了解线程池吗?今天,我将带大家全部学习关于线程池的所有知识。目录1. 简介2. 工作原理2.1 核
- /** * 快速计算二进制数中1的个数(Fast Bit Counting) * 该算法的思想如下: * 每次将该数与该数减一后的数值
-   利用 springboot + redis 实现过滤重复提交的请求,业务流程如下所示,首先定义一个拦
- SSO :同一个帐号在同一个公司不同系统上登陆 使用SpringSecurity实现类似于SSO登陆系统是十分简单的 下面我就搭
- Android 获取屏幕尺寸实例代码实现代码:/** * <supports-screens * android:smallScr
- 在使用JDBC的时候,数据库据连接是非常宝贵的资源。为了复用这些资源,可以将连接保存在一个队列中。当需要的时候可以从队列中取出未使用的连接。
- 学习hibernate的时候,小编已经接触多各种映射,mybatis中映射有到底是如何运转的,今天这篇博文,小编主要来简单的介绍一下myba
- Maven 作为一个优秀的项目管理工具,其插件机制为其功能扩展提供了非常大的便捷性。虽然说大多数情况下,我们可能不太会自己去编写 Maven
- 本文实例讲述了C#实现简单的Login窗口。分享给大家供大家参考。具体实现方法如下:C# 制作登录窗体,登录成功之后正确的做法是关闭(clo
- 1、static是什么意思?static 关键字表明一个成员变量或者是成员方法可以在没有所属的类的实例变量的情况下被访问。例如Main类pa
- 好久没有做web了,JSON目前比较流行,闲得没事,所以动手试试将对象序列化为JSON字符(尽管DotNet Framework