激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - Java編寫網(wǎng)上超市購物結(jié)算功能程序

Java編寫網(wǎng)上超市購物結(jié)算功能程序

2020-05-12 12:36zjq_1314520 JAVA教程

這篇文章主要為大家詳細介紹了Java編寫網(wǎng)上超市購物結(jié)算功能程序的具體代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

使用Java語言編寫一個模擬網(wǎng)上超市購物結(jié)算功能的程序,要求程序運行后有一個圖形用戶界面,可供用戶輸入購買的各種商品相關(guān)信息,最后給出用戶的購物清單及總價格。
需求分析:
1.管理員添加商品以及其價格
2.用戶購買商品打印訂單信息以及結(jié)算訂單
代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
 * 創(chuàng)建者:張俊強
 * 時間: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("網(wǎng)上超市購物結(jié)算");  
  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;
 /*
 * 設(shè)置界面
 * */
 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;
 /**
  * 數(shù)據(jù)庫導入
  */
  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("設(shè)置商品");
  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();
  /*
  * 設(shè)置界面初始化
  * */
  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("購買數(shù)量:");
  showBuyNumText=new JTextField(5);
  submitBuy=new JButton("確認購買");
  deleteBuyBut=new JButton("刪除訂單");
  countBut=new JButton("訂單結(jié)算");
  choiceList=new JList();
  buyItem=new Vector<String>();
  buyCount=new Float[100];
  /*
   * 數(shù)據(jù)庫的導入
   * */
  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();
  /*
   * 數(shù)據(jù)庫數(shù)據(jù)的導出
   * */
  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();
  /*
  * 設(shè)置界面布局
  * */
  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 {
      /*
       * 寫進數(shù)據(jù)庫
       * */
      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);
     /*
      * 刪除數(shù)據(jù)庫里面的元素
      * */
     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);
   }
   /*
   * 用戶界面事件響應(yīng)
   * */
   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()+" "
      +"購買數(shù)量:"+showBuyNumText.getText()+" "
      +"總價:"+sumMon.toString();
     buyItem.addElement(contentItem);
     //buyItem[buyNum]=contentItem;
     buyNum++;
     choiceList.removeAll();
     choiceList.setListData(buyItem);
     sumMoney+=sumMon;
    }
    else{
     JOptionPane.showMessageDialog(magWin,"購買數(shù)量不可以為空", "警告",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);
 }
}

刪除有關(guān)數(shù)據(jù)庫部分便可以在自己電腦上運行!

有關(guān)截圖:

Java編寫網(wǎng)上超市購物結(jié)算功能程序

管理員界面:

Java編寫網(wǎng)上超市購物結(jié)算功能程序

用戶界面:

Java編寫網(wǎng)上超市購物結(jié)算功能程序

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲一区二区网址 | 成人不卡一区二区 | 全黄裸片武则天一级第4季 九色p | 久久精品亚洲精品国产欧美kt∨ | 国产一区网址 | 91av久久 | 欧美一级做性受免费大片免费 | 成人精品一区二区 | 一级黄色毛片a | 久久久久久久久久91 | 久久久国产精品网站 | 久久精品欧美电影 | 福利在线小视频 | 久久逼逼 | 欧产日产国产精品乱噜噜 | 性欧美xxxx极品摘花 | 久久国产精品系列 | 久久在线精品 | 久久久久久久久久性 | 亚洲网站在线 | 欧美一级α片 | 国产精品片一区二区三区 | 亚洲欧美一区二区三区在线观看 | 中文字幕在线视频网站 | 爱操av| 成人国产视频在线观看 | 日本一级特级 | 孕妇体内谢精满日本电影 | 黄色a级片视频 | 精品久久久一二三区播放播放播放视频 | 亚洲午夜激情网 | 成人 精品 | 一本一道久久久a久久久精品91 | 久久久久久久国产a∨ | 无遮挡一级毛片视频 | 91成人免费在线观看 | 国产中出视频 | 欧美xxxx精品另类 | 小雪奶水翁胀公吸小说最新章节 | 97中文字幕第一一一页 | 欧美一级免费视频 |