欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2022年03月18日 16:18:54   作者:zjq_1314520  
這篇文章主要為大家詳細介紹了Java編寫網(wǎng)上超市購物結(jié)算功能程序的具體代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

使用Java語言編寫一個模擬網(wǎng)上超市購物結(jié)算功能的程序,要求程序運行后有一個圖形用戶界面,可供用戶輸入購買的各種商品相關(guān)信息,最后給出用戶的購物清單及總價格。

需求分析:

1.管理員添加商品以及其價格
2.用戶購買商品打印訂單信息以及結(jié)算訂單

代碼:

/* 
 * 創(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ù)庫導(dǎo)入 
  */ 
  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("確認(rèn)添加"); 
  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("確認(rèn)購買"); 
  deleteBuyBut=new JButton("刪除訂單"); 
  countBut=new JButton("訂單結(jié)算"); 
  choiceList=new JList(); 
  buyItem=new Vector<String>(); 
  buyCount=new Float[100]; 
  /* 
   * 數(shù)據(jù)庫的導(dǎo)入 
   * */ 
  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ù)的導(dǎo)出 
   * */ 
  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)截圖:

管理員界面:

用戶界面:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java數(shù)據(jù)結(jié)構(gòu) 遞歸之迷宮回溯案例講解

    Java數(shù)據(jù)結(jié)構(gòu) 遞歸之迷宮回溯案例講解

    這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)遞歸之迷宮回溯案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • 快速了解Java中ThreadLocal類

    快速了解Java中ThreadLocal類

    這篇文章主要介紹了快速了解Java中ThreadLocal類,介紹了ThreadLocal 是什么,ThreadLocal的作用,ThreadLocal 原理等相關(guān)內(nèi)容,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • Spring Boot 在啟動時進行配置文件加解密的方法詳解

    Spring Boot 在啟動時進行配置文件加解密的方法詳解

    這篇文章主要介紹了Spring Boot 在啟動時進行配置文件加解密的方法,本文通過實例給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • 通過java字節(jié)碼分析學(xué)習(xí)對象初始化順序

    通過java字節(jié)碼分析學(xué)習(xí)對象初始化順序

    今天用了jmock對進行單元測試編碼,發(fā)現(xiàn)一個比較奇怪的語法,static使用方法,見下面例子
    2013-11-11
  • spring注入在有常量的情況下使用@AllArgsConstructor操作

    spring注入在有常量的情況下使用@AllArgsConstructor操作

    這篇文章主要介紹了spring注入在有常量的情況下使用@AllArgsConstructor操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java8如何構(gòu)建一個Stream示例詳解

    Java8如何構(gòu)建一個Stream示例詳解

    Java 8 是迄今為止在語義上改動上最大的一個平臺。下面這篇文章主要給大家介紹了關(guān)于Java8如何構(gòu)建一個Stream的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-04-04
  • Struts2實現(xiàn)文件下載功能代碼分享(文件名中文轉(zhuǎn)碼)

    Struts2實現(xiàn)文件下載功能代碼分享(文件名中文轉(zhuǎn)碼)

    這篇文章主要介紹了Struts2實現(xiàn)文件下載功能代碼分享(文件名中文轉(zhuǎn)碼)的相關(guān)資料,需要的朋友可以參考下
    2016-06-06
  • Java知識梳理之泛型用法詳解

    Java知識梳理之泛型用法詳解

    從JDK?5.0以后,Java引入了“參數(shù)化類型(Parameterized?type)”的概念,允許我們在創(chuàng)建集合時再指定集合元素的類型。本文就來和大家深入聊聊Java泛型的使用
    2022-08-08
  • Java如何獲取一個隨機數(shù) Java猜數(shù)字小游戲

    Java如何獲取一個隨機數(shù) Java猜數(shù)字小游戲

    這篇文章主要為大家詳細介紹了Java如何獲取一個隨機數(shù),類似猜數(shù)字小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • 詳解Java時區(qū)處理之Date,Calendar,TimeZone,SimpleDateFormat

    詳解Java時區(qū)處理之Date,Calendar,TimeZone,SimpleDateFormat

    這篇文章主要介紹了Java時區(qū)處理之Date,Calendar,TimeZone,SimpleDateFormat的區(qū)別于用法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07

最新評論