Use the Java language to write a simulation of the online supermarket shopping settlement function of the program, requires the program to run a graphical user interface, for users to enter the purchase of various goods related information, and finally give the user's shopping list and the total price.
Requirements Analysis:
1. Administrator to add goods and their price
2. User Purchase goods print order information and settle order
Code:
* * Creator: Zhangjunqiang * Time: 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 ("Online supermarket shopping settlement");
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;
* * Settings interface */private JLabel Setnamelabel; Private JlabeL Setpricelabel;
Private JTextField Setnametext;
Private JTextField Setpricetext;
Private JButton inputbut;
Private TextArea Inputarea;
Private JButton returnBut1;
Private JButton cancelbut;
/* User Interface * */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;
/** * Database Import * * * Statement stmt;
Mainwinow (String winname) throws sqlexception{super (Winname);
goodsnum=0;
buynum=0;
summoney= (float) 0;
Goods=new goods ();
User=new JButton ("I am a user");
Manager=new JButton ("I am an administrator");
Loginlabel=new JLabel ("Please select role!"); MAgwin=new Managewindow ("Set up Commodity");
Magwin.setbounds (300, 300, 500, 400);
Magwin.setdefaultcloseoperation (Do_nothing_on_close);
Userwin=new Userwindow ("Welcome to buy");
Userwin.setbounds (300, 300, 500, 400);
Userwin.setdefaultcloseoperation (Do_nothing_on_close);
Lis=new Listener ();
* * SET INTERFACE Initialization * * * * * * * setnamelabel=new JLabel ("Product Name:");
Setpricelabel=new JLabel ("Price:");
Setnametext=new JTextField (5);
Setpricetext=new JTextField (5);
Inputbut=new JButton ("confirm add");
Inputarea=new TextArea ();
Returnbut1=new JButton ("return");
Cancelbut=new JButton ("recall add");
/* User Interface Initialization * */goodscombox=new JComboBox ();
Returnbut2=new JButton ("return");
Choicegoodlabel=new JLabel ("Please select Product:");
Showpricelabel=new JLabel ("price");
Showprice=new JTextField (5);
Showchoice=new TextArea ();
Showbuynum=new JLabel ("Purchase quantity:");
Showbuynumtext=new JTextField (5);
Submitbuy=new JButton ("Confirm purchase");
Deletebuybut=new JButton ("delete order");
Countbut=new JButton ("Order settlement"); ChoicElist=new JList ();
Buyitem=new vector<string> ();
Buycount=new float[100];
* * * Import * * * */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 ();
* * * Database Data export */Importsql ();
The 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 ();
* * SET Interface layout * */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);
/* User Interface Layout * */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, "No free items!", "Warning", Joptionpane.plain_
message);
} else{Goods.name[goodsnum]=setnametext.gettext ();
Goods.price[goodsnum]=float.parsefloat (Setpricetext.gettext ()); try {* * * is written to the database */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= "Product Name:" +setnametext.gettext () + "T" + "Price:" +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); * * Delete the elements inside the database */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);
}/* User Interface Event Response */if (E.getsource () ==returnbut2) {loginwin.setvisible (true);
Userwin.setvisible (FALSE); } if (E.getsource () ==submitbuy) {if (!showbuynumtext.gettext (). Equals (")) {Buycount[goodscombox.getselecte
Dindex ()]=float.parsefloat (Showbuynumtext.gettext ());
String contentitem= "";
Float Summon=float.parsefloat (Showbuynumtext.gettext ()) * (float) goods.price[goodscombox.getselectedindex ()]; contentitem= "Product Name:" +goods.name[goodscombox.getselectedindex ()]+ "" + "unit Price:" +goods.price[ Goodscombox.getselectedindex ()].tostring () + "" + "Purchase Quantity:" +showbuynumtext.gettext () + "" + "Total Price:" +summon.tostring (
);
Buyitem.addelement (ContentItem);
Buyitem[buynum]=contentitem;
buynum++;
Choicelist.removeall ();
Choicelist.setlistdata (Buyitem); Summoney+=summon;
else{Joptionpane.showmessagedialog (Magwin, "Purchase quantity can not be empty", "warning", joptionpane.plain_message); } if (E.getsource () ==deletebuybut) {if (Choicelist.getselectedvalue () ==null) {Joptionpane.showmes
Sagedialog (Magwin, "items not selected for deletion", "warning", 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 ("Total Price:")) {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, "Shopping cart is empty, can not be deleted", "Warning", 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 ("Total Price")) {buyitem.remove (i);
} buyitem.addelement ("Total Price:" +summoney.tostring ());
Choicelist.removeall ();
Choicelist.setlistdata (Buyitem);
Choicelist.validate ();
The private void Addgoods () {if (!inputarea.gettext (). Equals (")") Inputarea.settext (""); TODO auto-generated method stub for (int i=0;i<goodsnum;i++) {String massage= ' product name: ' +goods.name[i]+ ' \ t ' + ' Price: '
+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); }
}
Delete the relevant Database section can be run on your own computer!
For screenshots:
Administrator interface:
User interface:
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.