Java 언어를 사용하여 온라인 슈퍼마켓의 쇼핑 정산 기능을 시뮬레이션하는 프로그램을 작성하십시오. 이 프로그램에는 실행 후 그래픽 사용자 인터페이스가 있어야하므로 사용자는 구매 한 다양한 제품에 대한 정보를 입력하고 마지막으로 사용자의 쇼핑 목록과 총 가격을 제공 할 수 있습니다.
요구 사항 분석 :
1. 관리자는 제품과 가격을 추가합니다
2. 사용자가 제품을 구매할 때 주문 정보 및 결제 주문 코드를 인쇄하십시오.
/ * * Creator : Zhang Junqiang * 시간 : 2016/5/15 * */Package Salesys; java.awt.*; import java.awt.event.*; import java.util.vector; javax.swing을 가져옵니다.*; Java.sql.*; 클래스 상품 {public String [] 이름; 공개 플로트 [] 가격; goods () {name = new String [100]; 가격 = 새로운 플로트 [100]; }} public class 슈퍼마켓 확장 jframe {public static void main (String [] args)은 sqlexception {mainwinow mainwin = New Mainwinow ( "온라인 슈퍼마켓 쇼핑 체크 아웃"); mainwin.setDefaultCloseOperation (jframe.exit_on_close); Mainwin.SetBounds (300, 300, 500, 400); mainwin.set -Visible (true); Mainwin.setwin (Mainwin); mainwin.setminwindowlayout (); }} Class MainWinow는 Jframe {Goods Goods; 개인 JBUTTON 사용자; 개인 JBUTTON 관리자; 개인 jlabel loginlabel; 개인 관리 와인도 MAGWIN; Private UserWindow userWin; 개인 청취자 LI; 개인 메인 위로 로그인윈; Private int goodsnum; / * * 인터페이스 설정 * */ private jlabel setnamelabel; 개인 jlabel setpricelabel; 개인 Jtextfield setnametext; Private Jtextfield SetPricetext; 개인 jbutton inputbut; 개인 TextRea inputarea; 개인 JBUTTON returnbut1; 개인 JBUTTON CANCELBUT; / * * 사용자 인터페이스 * */ private vector <string> buyitem; 개인 플로트 [] BuyCount; 개인 int buynum; 개인 jcombobox goodscombox; 개인 JBUTTON returnbut2; 개인 jlabel ChoiceGoodlabel; 개인 jlabel showpricelabel; 개인 Jtextfield ShowPrice; 개인 TextRea Showchoice; 개인 jlabel showbuynum; 개인 Jtextfield ShowBuynumtext; 개인 JBUTTON 제출부; 개인 JBUTTON DELETEBUYBUT; 개인 JLIST Choicelist; 개인 JBUTTON COUNTBUT; 개인 플로트 여름; / *** 데이터베이스 가져 오기*/ 문자 stmt; MainWinow (String Winname)는 sqlexception {super (winname)를 던졌습니다. goodsnum = 0; Buynum = 0; Summoney = (float) 0; 상품 = 새로운 상품 (); user = new Jbutton ( "나는 사용자입니다"); Manager = New Jbutton ( "I am Administrator"); loginlabel = new Jlabel ( "역할을 선택하십시오!"); magwin = new ManageWindow ( "제품 설정"); 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 ( "가격 :"); setnametext = 새로운 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"); ChoeseGoodLabel = New Jlabel ( "제품을 선택하십시오 :"); showpricelabel = New Jlabel ( "가격"); ShowPrice = New Jtextfield (5); showchoice = new TextArea (); showbuynum = New Jlabel ( "구매 수량 :"); showbuynumtext = New Jtextfield (5); 제출부 = 새로운 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 자동 생성 캐치 블록 E.PrintStackTrace (); } string url = "jdbc : mysql : // localhost : 3306/device"; 문자열 사용자 = "루트"; 문자열 암호 = "ZJQ1314520"; Connection Con = DriverManager.getConnection (URL, 사용자, 암호); stmt = con.createstatement (); / * * 데이터베이스 데이터 내보내기 * */ importSql (); } public void importSql ()는 sqlexception {int i = 1; // TODO 자동 생성 메소드 Stub resultSet result = stmt.executeQuery ( "이름 선택, 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 () {컨테이너 logincon = new Container (); logincon.setlayout (new FlowLayout ()); logincon.add (관리자); logincon.add (사용자); 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 (입력); 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 (ChoeseGoodlabel); usercon.add (goodscombox); usercon.add (showpricelabel); usercon.add (showprice); usercon.add (showbuynum); usercon.add (showbuynumtext); usercon.add (제출부); userwin.add (usercon, borderlayout.north); //choicelist.setListData(goods.name); userwin.add (choicelist, borderlayout.center); userwin.add (New JSCrollPane (Choicelist)); 컨테이너 butcon = 새로운 컨테이너 (); 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-regendated method stub int i = goodscombox.getSelectedIndex (); if (i> = 0) showprice.settext (good.price [i] .tostring ()); returnbut2.addactionListener (LIS); submitbuy.addactionListener (LIS); deletebuybut.addactionListener (LIS); countbut.addactionListener (LIS); } private void addcomboxItem () {// todo 자동 생성 메소드 스터브 (int i = 0; i <goodsnum; i ++) {goodscombox.additem (goods.name [i]); }} 클래스 리스너는 ActionListener를 구현합니다 {@override public void ActionPerformed (ActionEvent e) {// todo 자동 생성 메소드 스터브 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, "spare imps!", "경고", joptionpane.plain_message); } else {goods.name [goodsnum] = setnametext.getText (); goods.price [goodsnum] = float.parsefloat (setpricetext.getText ()); { / * * 데이터베이스에 쓰기 * / stmt.executeUpdate ( "goods_info (이름, 가격) 값 ( '"+goods.name [goodsnum]+"', '"+(float) goods.price [goodsnum]+"')"); } catch (sqlexception e1) {// todo 자동 생성 캐치 블록 e1.printstacktrace (); } goodsnum ++; addgoods (); setnametext.settext ( ""); setpricetext.settext ( ""); // showout = "거래 이름 :"+setnametext.getText ()+"/t"+"price :"+setpricetext.getText ()+"/n"; //inputarea.append(showout); }} if (e.getSource () == cancelbut) {if (goodsnum> 0) {goodsnum--; 문자열 deletename = goods.name [goodsnum]; 문자열 deleteprice = goods.price [goodsnum] .toString (); //system.out.println(deleteName); / * * 데이터베이스에서 요소를 삭제하십시오 */ String SQL = "goods_info에서 삭제하십시오. try {stmt.executeUpdate (SQL); } catch (sqlexception e1) {// todo 자동 생성 캐치 블록 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 () == 제출 부) {if (! showBuynumtext.getText (). equals ( "")) {BuyCount [goodscombox.getSelectedIndex ()] = float.parsefloat (showbuynumtext.getText ()); 문자열 contentItem = ""; float summon = float.ploat.parsefloat (showbuynumtext.getText ())*(float) goods.price [goodscombox.getSelectedIndex ()]; ContentItem = "상표 이름 :"+goods.Name [goodscombox.getSelectedIndex ()]+""+"단가 :"+goods.price [goodscombox.getSelectedIndex ()]. toString ()+""+"구매 수량 :"+showBuynumtext.getText ()+"총 가격 :"+소환 (). buyitem.addelement (contentitem); // buyItem [buynum] = contentItem; Buynum ++; chooselist.removeall (); choicelist.setListData (buyitem); Summoney+= 소환; } 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 (); 문자열 selectItem = buyItem.get (i); //system.out.println(selectitem); 문자열 deleteprice = ""; for (int j = 0; 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.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 ( ""); // (int i = 0; i <goodsnum; i ++) {String Massage = "Trade Name :"+goods.name [i]+"/t"+"price :"+goods.price [i] .tostring ()+"/n"; inputArea.Append (마사지); }}} class managewindow는 jframe {manageWindow (문자열 제목) {super (title); }} class userwindow는 jframe {userwindow (문자열 제목) {super (title); }}관련 데이터베이스 부품을 삭제하고 컴퓨터에서 실행하십시오!
관련 스크린 샷 :
관리자 인터페이스 :
사용자 인터페이스 :
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.