使用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 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;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

// 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

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

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);

}

}

删除有关数据库部分便可以在自己电脑上运行!

有关截图:

管理员界面:

用户界面:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

java购物结算_Java编写网上超市购物结算功能程序相关推荐

  1. Java基于springboot +vue网上超市购物网站 多商家

    随着我国信息化的发展,大家更多的是希望通过网络获取到更多的直接所需的信息,而商品一直以来就是人类永恒的追求之一,如何能够享有到更多的商品是很多人一直以来关系的问题. 本系统通过在线网购的方式让用户可以 ...

  2. java购物小票_Java学习02-26(购物小票)

    一.利用外部设备为变量赋值: 第一步:在程序首部导入 java.util. 包中的Scanner类 格式:import java.util.Scanner; 第二步:在主方法中创建Scanner类的对 ...

  3. java编写超市收银系统_java编写的超市收银系统

    [实例简介] 用java编写的超市收银系统, [实例截图] [核心代码] ad9ea874-4694-4cc4-b634-760c9c1b6b65 └── 超市收银系统 ├── sql │   ├── ...

  4. java制造病毒_java编写病毒的可行性分析

    java编写病毒的可行性分析 最近心情十分郁闷,查阅一些病毒的资料消遣一下,居然发现这样的论调讲 java语言不可能编写病毒,在此特地反驳一下. 1 可执行 论调1:java需要依赖jre,无法在无j ...

  5. java实现通话_Java做一个最简单的通话程序

    Java做一个最简单的通话程序 作者:未知    文章来源:www.jspcn.net 发布日期:2005年01月19日 Java中的网络编程是一个很重要的部分,也是其编程优越性的地方之一.在Java ...

  6. java控制台超市收银系统_java编写的超市收银系统

    代码片段和文件信息 package supermarket; import supermarket.view.View; public class Test{ public static void m ...

  7. 编写一个超市购物程序,在一家超市有牙刷、毛巾、水杯、苹果和香蕉五种商品,商品价格如下表所示。

    import java.util.Scanner;//导入public class text1 {static double toothbrush = 8.8;//牙刷价格static double ...

  8. 大二学生HTML期末大作业——HTML+CSS+JavaScript食品网上超市购物商城网页与制作

    常见网页设计作业题材有 个人. 美食. 公司. 学校. 旅游. 电商. 宠物. 电器. 茶叶. 家居. 酒店. 舞蹈. 动漫. 服装. 体育. 化妆品. 物流. 环保. 书籍. 婚纱. 游戏. 节日. ...

  9. java 定时删除_Java编写定时删除文件程序

    Java编写定时删除文件程序 /*Java教程:http://www.javaweb.cc*/ import java.io.File; import java.util.Calendar; impo ...

  10. java 固定电话_Java 编写过滤手机号码或者固定电话的工具类

    以下是分享自己编写的用于过滤手机号码.固定电话.黑名单的工具类TelCheckUtils, import java.util.HashSet; import java.util.Set; import ...

最新文章

  1. Upgrading PHP on CentOS 6.5 (Final)
  2. 测试tcp连接数工具_后端开发程序员不知道压力测试怎么能行
  3. 网站下载器WebZip、Httrack及AWWWB.COM网站克隆器
  4. python怎么做图形界面-图形界面
  5. 写了一个Windows服务,通过C#模拟网站用户登录并爬取BUG列表查询有没有新的BUG,并提醒我...
  6. xay loves count 枚举-复杂度-顺序无关-选择
  7. JavaScript实现计算需要更改的位数,以便将 numberA转换为 numberB(bitsDiff)算法(附完整源码)
  8. Oracle面试题及答案整理
  9. 麻省理工学生发明 震惊世界
  10. c语言自定义输出小数点位数_C语言中输出时怎样控制小数点后的位数,请举例说明......
  11. python打开文件_用Python(in PsychoPy)打开SPSS数据文件
  12. Delphi Code Editor 之 几个特性
  13. 《季羡林先生》读书笔记-3
  14. java使用蒙特卡罗方法计算半径为r圆的面积_不用微积分,如何计算圆面积
  15. HWDB数据集gnt格式转为png格式
  16. Trusted Execution Technology (TXT) --- 基本原理篇
  17. java park_我可以在纯Java中实现park / unpark方法吗?
  18. Endnote x7 和X8的下载
  19. 编译原理教程_10 代码优化和目标代码生成
  20. IT项目管理学习笔记(一)——第8-11章

热门文章

  1. 计算机键入命令,Win7系统安装软件提示命令行语法错误键入“ 命令/?”怎么办...
  2. HLS tag “CHARACTERISTICS”DASH的AudioPurposeCS:
  3. 星星之火-38:LTE物理层无线资源与帧结构快速入门
  4. [NOI2010] 航空管制 (构反图+拓扑)
  5. 伪随机数的产生和流密码
  6. 使用PIE下载Sentinel-2 时序NDVI数据
  7. 777 权限 android,Android linux系统644、755、777权限详解
  8. 天使轮、A轮、B轮、C轮、D轮融资 究竟是什么?
  9. UEFI/GPT分区
  10. linux gpt分区挂载,GPT分区和挂载