Java言語を使用して、オンラインスーパーマーケットのショッピング決済機能をシミュレートするプログラムを作成します。実行後、プログラムにはグラフィカルユーザーインターフェイスがあるため、ユーザーはさまざまな購入製品に関する情報を入力し、最終的にユーザーのショッピングリストと合計価格を提供できます。
要件分析:
1.管理者は製品とその価格を追加します
2。ユーザーによる製品を購入する際の注文情報および決済注文コードを印刷:
/ * *作成者:Zhang Junqiang *時間:2016/5/15 * */パッケージsalesys; java.awt。*; java.awt.event。*; java.util.vectorをインポートします。 javax.swing。*; Java.sql。*をインポートします。クラスグッズ{public string [] name;パブリックフロート[]価格; goods(){name = new String [100];価格=新しいフロート[100]; }} public classスーパーマーケット拡張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拡張jframe {goods goods;プライベートJButtonユーザー。プライベートJButtonマネージャー。プライベートJlabel LoginLabel;プライベートManageWindow Magwin; Private userWindow userwin;プライベートリスナーlis;プライベートMainWinow LoginWin; Private Int Goodsnum; / * *インターフェイスの設定 * */ private jlabel setnameLabel;プライベートJlabel setpricelabel; private jtextfield setnametext; Private JTextField SetPricEtext;プライベートjbutton input -cut; Private Textarea inputarea;プライベートjbutton returnbut1;プライベートjbutton cancelcut; / * *ユーザーインターフェイス * */ private vector <string> buyitem;プライベートフロート[] BuyCount; Private int buynum;プライベートJComboBox GoodsCombox;プライベートjbutton returnbut2;プライベートJlabel ChoiceGoodlabel;プライベートJlabel Showpricelabel;プライベートjtextfield showprice; Private Textarea ShowChoice;プライベートJlabel Shownunum;プライベートJTextfield showbuynumtext;プライベートjbutton submitbuy;プライベートjbutton deleteBuybut;プライベートJLIST ChoiceList;プライベートjbutton count -cout;プライベートフロートサモニー。 / ***データベースインポート*/ステートメント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( "Set Product"); magwin.setbounds(300、300、500、400); magwin.setDefaultCloseoperation(do_nothing_on_close); userwin = new userWindow( "Welcome to Purchase"); userwin.setbounds(300、300、500、400); userwin.setDefaultCloseoperation(do_nothing_on_close); lis = newリスナー(); / * *インターフェイス初期化を設定 * */ setNameLabel = new JLabel( "Trade Name:"); setPricelabel = new Jlabel( "price:"); setNameText = new JTextField(5); setpricetext = new JTextField(5); inputbut = new jbutton( "add to add"); inputarea = new Textarea(); returnbut1 = new JButton( "Return"); cancelChut = new JButton( "Retract add"); / * *ユーザーインターフェイス初期化 * */ goodscombox = new jcombobox(); returnbut2 = new JButton( "Return"); ChoiceGoodLabel = new Jlabel( "製品を選択してください:"); showpricelabel = new Jlabel( "Price"); showprice = new jtextfield(5); showchoice = new Textarea(); showbuynum = new Jlabel( "購入数量:"); showbuynumtext = new JTextField(5); submitbuy = new jbutton( "確認購入"); deleteBuybut = new JButton( "Delete Order"); countbut = new jbutton( "Order Settlement"); ChoiceList = new Jlist(); buyitem = new Vector <String>(); buycount = new Float [100]; / * *データベースインポート * */ try {class.forname( "com.mysql.jdbc.driver"); } catch(classNotFoundException e){// todo auto-enerated catch block e.printstacktrace(); } string url = "jdbc:mysql:// localhost:3306/device";文字列ユーザー= "root";文字列パスワード= "zjq1314520";接続con = drivermanager.getConnection(url、user、password); stmt = con.createstatement(); / * *データベースデータのエクスポート * */ ImportSQL(); } public void importsql()throws sqlexception {int i = 1; // TODO自動生成方法スタブ結果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(マネージャー); 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(input -but); magwin.add(inputarea); magwin.add(cancelcut); magwin.add(returnbut1); inputbut.addactionlistener(lis); returnbut1.addactionlistener(lis); cancelChut.AddActionListener(LIS); / * *ユーザーインターフェイスレイアウト * */ userwin.setlayout(new borderlayout());コンテナ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));コンテナbutcon = new container(); butcon.setLayout(new FlowLayout());しかし、con.add(deleteBuybut);しかし、con.add(count -but);しかし、con.add(returnbut2); userwin.add(butcon、borderlayout.south); goodscombox.additemlistener(new ItemListener(){@Override public void itemStateChanged(ItemEvent e){// todo auto-feenated methood stub int i = goodscombox.getSelecteddex(); returnBut2.AddActionListener(LIS); submitbuy.addactionlistener(lis); DeleteBuyBut.AddActionListener(LIS); countbut.addactionlistener(lis); } private void addComboxItem(){//(int i = 0; i <goodsnum; i ++){goodscombox.additem(goods.name [i]); }} classリスナーはActionListener {@Override public void ActionPerformed(ActionEvent e){// todo auto-eneratedメソッドスタブ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()== inputt){// string showout = ""; if(setnametext.getText()。equals( "")|| setpricetext.getText()。equals( "")){joptionpane.showmessageialog( "spare items!"、 "warning"、joptionpane.plain_message); } else {goods.name [goodsnum] = setnametext.getText(); goods.price [goodsnum] = float.parsefloat(setpricetext.getText()); { / * *データベースに書き込み * } catch(sqlexception e1){// todo auto-feenated catch block e1.printstacktrace(); } goodsnum ++; addgoods(); setnametext.settext( ""); setpricetext.settext( ""); // showout = "トレード名:"+setnametext.getText()+"/t"+"frice:"+setpricetext.getText()+"/n"; //inputarea.append(Showout); }} if(e.getSource()== cancelcut){if(goodsnum> 0){goodsnum--;文字列deletename = goods.name [goodsnum]; string deleteprice = goods.price [goodsnum] .toString(); //system.out.println(deletename); / * *データベース内の要素を削除 * */ string sql = "goodse_infoからdelete name = '"+deletename+"' and price = '"+deleteprice+"'"; try {stmt.executeUpdate(sql); } catch(sqlexception e1){// todo auto-feenated catch block e1.printstacktrace(); } //接続con = dbmanager .getConnection();; // represedStatement 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();文字列contentitem = ""; float summon = float.parsefloat(showbuynumtext.getText())*(float)goods.price [goodscombox.getSelectedIndex()]; contentItem = "貿易名:"+good.name [goodscombox.getSelectedIndex()]+""+"ung spurice.price.price [goodscombox.selectedIndex()。toString()+" "+"購入数量: "+showbuynumtext.getText()+" "+"+"+summon.tostring(); buyitem.addelement(contentitem); // buyitem [buynum] = contentItem; BuyNum ++; chooselist.removeall(); ChoiceList.setListData(buyItem); summoney+= summon; } else {joptionpane.showmessageialog(magwin、 "購入数量は空にすることはできません」、「警告」、joptionpane.plain_message); }} if(e.getSource()== deleteBuybut){if(choicelist.getSelectedValue()== null){joptionpane.showmessageialog(magwin、 "削除するアイテムは選択されていません」、「警告」、joptionpane.plain_message); } else if(buynum> 0){int i = choiceList.getSelectedIndex();文字列selectItem = buyItem.get(i); //system.out.println(selectitem);文字列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);壊す; }} buyitem.remove(i); ChoiceList.RemoveAll(); ChoiceList.setListData(buyItem); ChoiceList.Validate(); buynum--; } else {joptionpane.showmessageialog(magwin、 "ショッピングカートは空で、削除できません」、「警告」、joptionpane.plain_message); }} if(e.getSource()== count -but){// 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( ""); //(int i = 0; i <goodsnum; i ++){string massage = "貿易名:"+goods.name [i]+"+"+"frice:"+goods.price [i] .tostring()+"/n"; inputarea.append(マッサージ); }}} class managewindow extends jframe {managewindow(string title){super(title); }} class userWindow拡張jframe {userwindow(string title){super(title); }}関連するデータベースパーツを削除し、コンピューターで実行してください!
関連スクリーンショット:
管理者インターフェイス:
ユーザーインターフェイス:
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。