Java编写网上超市购物结算功能程序
程序员文章站
2024-03-12 16:24:50
使用java语言编写一个模拟网上超市购物结算功能的程序,要求程序运行后有一个图形用户界面,可供用户输入购买的各种商品相关信息,最后给出用户的购物清单及总价格。
需求分析:...
使用java语言编写一个模拟网上超市购物结算功能的程序,要求程序运行后有一个图形用户界面,可供用户输入购买的各种商品相关信息,最后给出用户的购物清单及总价格。
需求分析:
1.管理员添加商品以及其价格
2.用户购买商品打印订单信息以及结算订单
代码:
/* * 创建者:张俊强 * 时间: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("网上超市购物结算"); 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; /* * 设置界面 * */ 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; /** * 数据库导入 */ 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("设置商品"); 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(); /* * 设置界面初始化 * */ setnamelabel=new jlabel("商品名:"); setpricelabel=new jlabel("价格:"); setnametext=new jtextfield(5); setpricetext=new jtextfield(5); inputbut=new jbutton("确认添加"); 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("购买数量:"); showbuynumtext=new jtextfield(5); submitbuy=new jbutton("确认购买"); deletebuybut=new jbutton("删除订单"); countbut=new jbutton("订单结算"); choicelist=new jlist(); buyitem=new vector<string>(); buycount=new float[100]; /* * 数据库的导入 * */ 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(); /* * 数据库数据的导出 * */ 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(); /* * 设置界面布局 * */ 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 { /* * 写进数据库 * */ 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); /* * 删除数据库里面的元素 * */ 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); } /* * 用户界面事件响应 * */ 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()+" " +"购买数量:"+showbuynumtext.gettext()+" " +"总价:"+summon.tostring(); buyitem.addelement(contentitem); //buyitem[buynum]=contentitem; buynum++; choicelist.removeall(); choicelist.setlistdata(buyitem); summoney+=summon; } else{ joptionpane.showmessagedialog(magwin,"购买数量不可以为空", "警告",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); } }
删除有关数据库部分便可以在自己电脑上运行!
有关截图:
管理员界面:
用户界面:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。