استخدم لغة Java لكتابة برنامج يحاكي وظيفة تسوية التسوق لسوبر ماركت عبر الإنترنت. يتطلب الأمر أن يحتوي البرنامج على واجهة مستخدم رسومية بعد التشغيل ، مما يتيح للمستخدمين إدخال معلومات حول مختلف المنتجات المشتراة ، وأخيراً إعطاء قائمة التسوق الخاصة بالمستخدم وسعر إجمالي.
تحليل المتطلبات:
1. المسؤول يضيف المنتجات وأسعارها
2. طباعة معلومات الطلب ورمز طلب التسوية عند شراء المنتجات من قبل المستخدمين:
/ * * Creator: Zhang Junqiang * الوقت: 2016/5/15 * */Package Salesys ؛ استيراد java.awt.*؛ استيراد java.awt.event.*؛ استيراد java.util.vector ؛ استيراد javax.swing.*؛ استيراد java.sql.*؛ class class {public string [] name ؛ تعويم عام [] السعر ؛ Goods () {name = new String [100] ؛ السعر = تعويم جديد [100] ؛ }} Supermarket الفئة العامة يمتد JFRAME {public static void main (String [] args) remrows sqlexception {mainwinow mainwin = new MainWinow ("Online Supermarket Shopping Checkout") ؛ Mainwin.setDefaultCloseOperation (jframe.exit_on_close) ؛ Mainwin.SetBounds (300 ، 300 ، 500 ، 400) ؛ mainwin.setVisible (صحيح) ؛ Mainwin.setwin (Mainwin) ؛ mainwin.setMinWindowLayout () ؛ }} class mainwinow يمتد jframe {goods goods ؛ مستخدم jbutton الخاص ؛ مدير Jbutton الخاص ؛ خاص Jlabel LoginLabel ؛ Private ManageWindow Magwin ؛ userwindow userwin الخاص ؛ المستمع الخاص lis ؛ loginwin mainwinow الخاص ؛ Private Int Goodsnum ؛ / * * إعداد واجهة * */ private Jlabel setnamelabel ؛ خاص Jlabel SetPricelabel ؛ خاص jtextfield setnametext ؛ خاص JTextfield SetPricetext ؛ الخاص jbutton inputbut ؛ Textarea inputarea الخاص ؛ Private Jbutton ReturnBut1 ؛ خاص jbutton cancelbut ؛ / * * واجهة المستخدم * */ متجه خاص <string> buyItem ؛ تعويم خاص [] buycount ؛ Private Int Buynum ؛ خاص JCombobox GoodsCombox ؛ خاص Jbutton ReturnBut2 ؛ خاص Jlabel Choicegoodlabel ؛ خاص Jlabel showpricelabel ؛ خاص Jtextfield Showprice ؛ Textarea showchoice ؛ خاص Jlabel Showbuynum ؛ خاص Jtextfield Showbuynumtext ؛ الخاص Jbutton Sumpuy ؛ الخاص jbutton deletebuybut ؛ jlist choicelist الخاص ؛ خاص jbutton countbut ؛ استدعاء تعويم خاص ؛ / *** استيراد قاعدة البيانات*/ بيان STMT ؛ Mainwinow (String WinName) يلقي Sqlexception {Super (WinName) ؛ goudsnum = 0 ؛ buynum = 0 ؛ Summoney = (float) 0 ؛ البضائع = البضائع الجديدة () ؛ user = new Jbutton ("أنا مستخدم") ؛ المدير = New JButton ("أنا مسؤول") ؛ loginLabel = New JLabel ("الرجاء تحديد دور!") ؛ Magwin = new ManageWindow ("Set Product") ؛ Magwin.SetBounds (300 ، 300 ، 500 ، 400) ؛ magwin.setDefaultCloseOperation (do_nothing_on_close) ؛ userwin = جديد userwindow ("مرحبًا بك في الشراء") ؛ userwin.setBounds (300 ، 300 ، 500 ، 400) ؛ userwin.setDefaultCloseOperation (do_nothing_on_close) ؛ lis = مستمع جديد () ؛ / * * تعيين تهيئة الواجهة * */ setnamelabel = new jlabel ("اسم التجارة:") ؛ setPricelabel = New JLabel ("Price:") ؛ setnametext = new JTextfield (5) ؛ setPricetext = new JTextfield (5) ؛ inputBut = new jbutton ("تأكيد لإضافة") ؛ inputarea = new textArea () ؛ returnbut1 = new jbutton ("return") ؛ CancelBut = 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 ("تسوية الطلب") ؛ choiciList = new JList () ؛ buyItem = ناقل جديد <string> () ؛ buyCount = تعويم جديد [100] ؛ / * * database import * */ try {class.forname ("com.mysql.jdbc.driver") ؛ } catch (classnotfoundException e) {// todo catch catch e.printstacktrace () ؛ } url url = "jdbc: mysql: // localhost: 3306/device" ؛ string user = "root" ؛ سلسلة كلمة المرور = "ZJQ1314520" ؛ Connection con = drivermanager.getConnection (url ، المستخدم ، كلمة المرور) ؛ stmt = con.createstatement () ؛ / * * تصدير بيانات قاعدة البيانات * */ ImpirtSQL () ؛ } public void ishorsql () يلقي sqlexception {int i = 1 ؛ // todo todo method method resultset results = stmt.executequery ("Select Name ، Price from Goods_info") ؛ بينما (result.next ()) {goods.name [i-1] = result.getString (1) ؛ Goods.price [i-1] = float.parsefloat (result.getString (2)) ؛ i ++ ؛ } goodnum = i-1 ؛ } public void setwin (mainwinow w) {loginwin = w ؛ } public void setMinWindowLayout () {Container logincon = new Container () ؛ logincon.setLayout (New FlowLayout ()) ؛ logincon.add (مدير) ؛ logincon.add (المستخدم) ؛ MANGER.AddActeListener (LIS) ؛ user.addActionListener (LIS) ؛ this.setLayout (New BorderLayout ()) ؛ this.add (loginlabel ، borderlayout.north) ؛ this.add (logincon ، borderlayout.center) ؛ this.validate () ؛ / * * SET Interface Layout * */ MAGWIN.SetLayout (New FlowLayout ()) ؛ magwin.add (setnamelabel) ؛ magwin.add (setnametext) ؛ magwin.add (setPricelabel) ؛ magwin.add (setPricetext) ؛ magwin.add (inputbut) ؛ magwin.add (inpectarea) ؛ Magwin.add (CancelBut) ؛ magwin.add (returnbut1) ؛ inputbut.addActionListener (LIS) ؛ ReturnBut1.AddActionListener (LIS) ؛ CancelBut.addActionListener (LIS) ؛ / * * تخطيط واجهة المستخدم * */ userwin.setLayout (New BorderLayout ()) ؛ حاوية usercon = حاوية جديدة () ؛ usercon.setLayout (New FlowLayout ()) ؛ usercon.add (choicegoodlabel) ؛ UserCon.Add (goodscombox) ؛ usercon.add (showpricelabel) ؛ usercon.add (showprice) ؛ usercon.add (showbuynum) ؛ usercon.add (showbuynumtext) ؛ usercon.add (simpluy) ؛ userwin.add (usercon ، borderlayout.north) ؛ //Choicelist.setListData(goods.name) ؛ userwin.add (choicicelist ، borderlayout.center) ؛ userwin.add (new jscrollpane (Choicicelist)) ؛ حاوية ولكن حاوية جديدة () ؛ butcon.setLayout (New FlowLayout ()) ؛ butcon.add (deleteBuyBut) ؛ butcon.add (countbut) ؛ butcon.add (returnbut2) ؛ userwin.add (butcon ، borderlayout.south) ؛ pouldcombox.additemListener (new itemListener () {Override public void itemStateChanged (itemevent e) {// todo method tuto method clud int i = goodscombox.getselectionex () ؛ if (i> = 0) showprice.settext tex (goods.price [i] .tosTring ()) ؛}) ؛ returnbut2.addActionListener (LIS) ؛ submitBuy.addActionListener (LIS) ؛ DeLeTeBuyBut.addActionListener (LIS) ؛ CountBut.addActionListener (LIS) ؛ } private void addComboxItem () {// todo method method method tuto for (int i = 0 ؛ i <goodsnum ؛ i ++) {goodscombox.additem (Goods.name [i]) ؛ }} مستمع الفئة تنفذ ActionListener {Override public void actionperformed (ActionEvent e) {// todo method method method tuto if ( loginwin.setVisible (false) ؛ Magwin.setVisible (صحيح) ؛ } if ( userwin.setVisible (true) ؛ goodcombox.removeallitems () ؛ addComboxItem () ؛ } if ( if (setnametext.getText (). يساوي ("") || setPricetext.getText (). يساوي ("")) {joptionpane.showmessagedialog (magwin ، "لا توجد عناصر احتياطية!" ، "تحذير" ، joptionpane.plain_message) ؛ } آخر {goods.name [goodnum] = setnametext.getText () ؛ Goods.price [goodnum] = float.parsefloat (setPricetext.getText ()) ؛ جرب { / * * اكتب إلى قاعدة البيانات * * / stmt.executeupdate ("أدخل في goods_info (الاسم ، السعر) القيم ('"+goods.name [goodsnum]+"' ، '"+(float) goods.price [goodsnum]+")") ؛ } catch (sqlexception e1) {// todo acto catch block e1.printstacktrace () ؛ } goodnum ++ ؛ addgoods () ؛ setnametext.settext ("") ؛ setPricetext.settext ("") ؛ // showout = "trade name:"+setnametext.getText ()+"/t"+"Price:"+setPricetext.getText ()+"/n" ؛ //inputarea.append(showout) ؛ }} if ( سلسلة حذف = goods.name [goodnum] ؛ سلسلة حذف = Goods.price [goodnum] .ToString () ؛ //system.out.println(deletename) ؛ / * * حذف العناصر في قاعدة البيانات * */ string sql = "delete from goods_info حيث name = '"+deletename+"' و price = '"+deleteprice+"' '" ؛ حاول {stmt.executeupdate (sql) ؛ } catch (sqlexception e1) {// todo acto catch block e1.printstacktrace () ؛ } // connection con = dbmanager .getConnection () ؛؛ // preparedStatement ps = con.preparestatement (SQL) ؛ addgoods () ؛ }} if ( magwin.setVisible (false) ؛ } / * * استجابة حدث واجهة المستخدم * * / if ( userwin.setVisible (false) ؛ } if ( String ContentItem = "" ؛ float summon = float.parsefloat (showbuynumtext.getText ())*(float) goods.price [goodcombox.getSelectedIndex ()] ؛ contentItem = "Trade Name:"+Goods.Name [goodcombox.getSelectedIndex ()]+""+"Price unit:"+gods.price [goodscombox.getselectedIndex ()]. buyItem.AddElement (contentItem) ؛ // buyItem [buynum] = contentItem ؛ buynum ++ ؛ chooselist.removeall () ؛ choicicelist.setListData (buyItem) ؛ Summoney+= استدعاء ؛ } آخر {joptionpane.showmessagedialog (Magwin ، "لا يمكن أن تكون كمية الشراء فارغة" ، "تحذير" ، joptionpane.plain_message) ؛ }} if ( } آخر إذا (buynum> 0) {int i = choicelist.getSelectedIndex () ؛ String selectItem = buyItem.get (i) ؛ //system.out.println(selectItem) ؛ سلسلة حذف = "" ؛ لـ (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 ، selectem.length ()) ؛ system.out.println (deleteprice) ؛ Summoney- = float.parsefloat (deleteprice) ؛ استراحة؛ }} buyitem.remove (i) ؛ choicicelist.removeall () ؛ choicicelist.setListData (buyItem) ؛ choicicelist.validate () ؛ buynum-- ؛ } آخر {joptionpane.showmessagedialog (Magwin ، "عربة التسوق فارغة ، لا يمكن حذفها" ، "تحذير" ، joptionpane.plain_message) ؛ }} if ( if (str.equals ("إجمالي السعر")) {buyItem.Remove (i) ؛ }} buyItem.AddElement ("السعر الإجمالي:"+summoney.toString ()) ؛ choicicelist.removeall () ؛ choicicelist.setListData (buyItem) ؛ choicicelist.validate () ؛ }} private void addgoods () {if (! inputArea.getText (). equals ("")) inputarea.settext ("") ؛ // todo method method method tuto for (int i = 0 ؛ i <goodsnum ؛ i ++) {String massage = "trade name:"+gods.name [i]+"/t"+"price: inpectarea.append (التدليك) ؛ }}} Class ManageWindow يمتد JFrame {manageWindow (عنوان السلسلة) {super (title) ؛ }} class userwindow يمتد jframe {userWindow (عنوان السلسلة) {super (title) ؛ }}احذف جزء قاعدة البيانات ذات الصلة وقم بتشغيله على جهاز الكمبيوتر الخاص بك!
لقطات الشاشة ذات الصلة:
واجهة المسؤول:
واجهة المستخدم:
ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.