亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

需求分析:

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

代碼:

/* 
 * 創(chuàng)建者:張俊強(qiáng) 
 * 時(shí)間: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)上超市購(gòu)物結(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ù)庫(kù)導(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("請(qǐng)選擇角色!"); 
  magWin=new ManageWindow("設(shè)置商品"); 
  magWin.setBounds(300, 300, 500, 400); 
  magWin.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); 
  userWin=new UserWindow("歡迎選購(gòu)"); 
  userWin.setBounds(300, 300, 500, 400); 
  userWin.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); 
  lis=new Listener(); 
  /* 
  * 設(shè)置界面初始化 
  * */ 
  setNameLabel=new JLabel("商品名:"); 
  setPriceLabel=new JLabel("價(jià)格:"); 
  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("請(qǐng)選擇商品:"); 
  showPriceLabel=new JLabel("價(jià)格"); 
  showPrice=new JTextField(5); 
  showChoice=new TextArea(); 
  showBuyNum=new JLabel("購(gòu)買(mǎi)數(shù)量:"); 
  showBuyNumText=new JTextField(5); 
  submitBuy=new JButton("確認(rèn)購(gòu)買(mǎi)"); 
  deleteBuyBut=new JButton("刪除訂單"); 
  countBut=new JButton("訂單結(jié)算"); 
  choiceList=new JList(); 
  buyItem=new Vector<String>(); 
  buyCount=new Float[100]; 
  /* 
   * 數(shù)據(jù)庫(kù)的導(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ù)庫(kù)數(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,"不可以有空余項(xiàng)!", "警告",JOptionPane.PLAIN_MESSAGE); 
    } 
    else{ 
     goods.name[goodsNum]=setNameText.getText(); 
     goods.price[goodsNum]=Float.parseFloat(setPriceText.getText()); 
     try { 
      /* 
       * 寫(xiě)進(jìn)數(shù)據(jù)庫(kù) 
       * */ 
      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"+"價(jià)格:"+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ù)庫(kù)里面的元素 
      * */ 
     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()]+" " 
      +"單價(jià):"+goods.price[goodsCombox.getSelectedIndex()].toString()+" " 
      +"購(gòu)買(mǎi)數(shù)量:"+showBuyNumText.getText()+" " 
      +"總價(jià):"+sumMon.toString(); 
     buyItem.addElement(contentItem); 
     //buyItem[buyNum]=contentItem; 
     buyNum++; 
     choiceList.removeAll(); 
     choiceList.setListData(buyItem); 
     sumMoney+=sumMon; 
    } 
    else{ 
     JOptionPane.showMessageDialog(magWin,"購(gòu)買(mǎi)數(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("總價(jià):")){ 
       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,"購(gòu)物車(chē)為空,不可刪除", "警告",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("總價(jià)")){ 
      buyItem.remove(i); 
     } 
    } 
    buyItem.addElement("總價(jià):"+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"+"價(jià)格:"+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ù)庫(kù)部分便可以在自己電腦上運(yùn)行!

有關(guān)截圖:

管理員界面:

用戶界面:

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

相關(guān)文章

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

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

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

    快速了解Java中ThreadLocal類

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

    Spring Boot 在啟動(dòng)時(shí)進(jìn)行配置文件加解密的方法詳解

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

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

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

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

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

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

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

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

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

    Java知識(shí)梳理之泛型用法詳解

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

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

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

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

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

最新評(píng)論