1、 确定会员以及商品价格消费总额满200发会员卡一张 总结全天账单4.3.2 程序代码及解释import java.awt.*; import javax.swing.*;class shangpin String SId; String Smane; int jiage; public shangpin(String a,String b,int c) SId=a; Smane=b; jiage=c; class huiyuan String HId; String Hname; public huiyuan(String a,String b) HId=a; Hname=b;public c
2、lass chaoshi extends JFrame static int key=0; /会员 static int sum1=0; /总消费 static int sum2=0; /当天合计 static int huiyuansum=0; /会员人数 static int Spinsum1=0; /商品1合计 static int Spinsum2=0; /商品2合计 static int Spinsum3=0; /商品3合计 static int Spinsum4=0; /商品4合计 static int Spinsum5=0; /商品5合计 public static void m
3、ain(String agrs) JFrame frame =new JFrame(超市);frame.setSize(400,250); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c=frame.getContentPane(); c.add(pan(); frame.setVisible(true);/向窗口中添加面板和按钮static JPanel pan () JPanel panel=new JPanel(); JPanel part1=new JPanel(); part1.add(new JLa
4、bel(会员卡号:); final JTextField text1=new JTextField(); text1.setHorizontalAlignment(JTextField.RIGHT); text1.setColumns(11); JButton button1=new JButton(确定 part1.add(text1); part1.add(button1); part1.setVisible(true); final JLabel JLabel00 =new JLabel(非会员 part1.add(JLabel00); panel.add(part1); JPanel
5、part2=new JPanel(); part2.add(new JLabel(商品代号: final JTextField text2=new JTextField(); text2.setHorizontalAlignment(JTextField.RIGHT); text2.setColumns(15); JButton button2=new JButton( part2.add(text2); part2.add(button2); panel.add(part2); JPanel part3=new JPanel(); final JLabel JLabel0=new JLabe
6、l( final JLabel JLabel1=new JLabel(000 final JLabel JLabel2=new JLabel(0000 part3.add(new JLabel(商品名 part3.add(JLabel0);商品价格 part3.add(JLabel1);元总价格 part3.add(JLabel2); panel.add(part3); JPanel part4=new JPanel(); JButton button3=new JButton(结账 JButton button4=new JButton(总汇 part4.add(button3); part
7、4.add(new JLabel( part4.add(button4); panel.add(part4); button1.addActionListener(new ActionListener() huiyuan huiyuan1=new huiyuan(001,小李 huiyuan huiyuan2=new huiyuan(002小张 huiyuan huiyuan3=new huiyuan(003小王 /添加时间监听 public void actionPerformed(ActionEvent e) String kahao=text1.getText(); if(kahao.e
8、quals(huiyuan1.HId) JLabel00.setText(huiyuan1.Hname); key=1; if(kahao.equals(huiyuan2.HId) JLabel00.setText(huiyuan2.Hname); if(kahao.equals(huiyuan3.HId) JLabel00.setText(huiyuan3.Hname); ); button2.addActionListener(new ActionListener() shangpin shangpin1=new shangpin(商品1,50); shangpin shangpin2=n
9、ew shangpin(商品2,100); shangpin shangpin3=new shangpin(商品3,30); shangpin shangpin4=new shangpin(004商品4,60); shangpin shangpin5=new shangpin(005商品5,200);/添加时间监听 String shangpinhao=text2.getText(); if(shangpinhao.equals(shangpin1.SId) JLabel0.setText(+shangpin1.Smane); String str = String.format(%04d,s
10、hangpin1.jiage); JLabel1.setText(str); sum1=sum1+shangpin1.jiage; sum2=sum2+shangpin1.jiage; String str1 = String.format(,sum1); JLabel2.setText(str1); Spinsum1+; ; if(shangpinhao.equals(shangpin2.SId) JLabel0.setText(+shangpin2.Smane); String str = String.format(,shangpin2.jiage); JLabel1.setText(s
11、tr); sum1=sum1+shangpin2.jiage; sum2=sum2+shangpin2.jiage; String str1 = String.format( JLabel2.setText(str1); Spinsum2+; ; if(shangpinhao.equals(shangpin3.SId) +shangpin3.Smane);,shangpin3.jiage); sum1=sum1+shangpin3.jiage; sum2=sum2+shangpin3.jiage; Spinsum3+; if(shangpinhao.equals(shangpin4.SId)
12、+shangpin4.Smane);,shangpin4.jiage); sum1=sum1+shangpin4.jiage; sum2=sum2+shangpin4.jiage; Spinsum4+; if(shangpinhao.equals(shangpin5.SId) +shangpin5.Smane);,shangpin5.jiage); sum1=sum1+shangpin5.jiage; sum2=sum2+shangpin5.jiage; Spinsum5+; button3.addActionListener(new ActionListener() if(sum1!=0)
13、huiyuansum+; JDialog duox=new JDialog(); JPanel duoxP=new JPanel(); duox.getContentPane().add(duoxP); duox.setTitle(消费总额 duoxP.setVisible(true); duoxP.setLayout(new GridLayout(4,1,0,0); duox.setSize(200,300); duox.setVisible(true); JPanel panel1=new JPanel(); JPanel part11=new JPanel(); JPanel part1
14、2=new JPanel(); JLabel Label11=new JLabel(str1); Label11.setText(str1); part11.add(new JLabel(消费总额: part11.add(Label11); if(key!=1)&sum1=200) part12.add(new JLabel(消费达200元发放一张会员卡 panel1.add(part12); panel1.add(part11); duoxP.add(panel1); key=0; sum1=0; JLabel1.setText( JLabel2.setText( JLabel00.setT
15、ext( text1.setText( text2.setText( button4.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) JDialog duox=new JDialog(); duox.getContentPane().add(duoxP); duox.setTitle( duoxP.setVisible(true); duoxP.setLayout(new GridLayout(8,1,0,0); duox.setSize(200,400); JPanel par
16、t21=new JPanel();JPanel part22=new JPanel(); JPanel part23=new JPanel();JPanel part24=new JPanel(); JPanel part25=new JPanel();JPanel part26=new JPanel(); JPanel part27=new JPanel(); JButton button5=new JButton(全部清零 final JLabel label21=new JLabel(); part21.add(new JLabel(总消费额 String str21 = String.
17、format(,sum2); label21.setText(str21); part21.add(label21); final JLabel label22=new JLabel(); part22.add(new JLabel(商品1消费量 String str22 = String.format(,Spinsum1); label22.setText(str22); part22.add(label22); final JLabel label23=new JLabel(); part23.add(new JLabel(商品2消费量 String str23 = String.form
18、at(,Spinsum2); label23.setText(str23); part23.add(label23); final JLabel label24=new JLabel(); part24.add(new JLabel(商品3消费量 String str24 = String.format(,Spinsum3); label24.setText(str24); part24.add(label24); final JLabel label25=new JLabel(); part25.add(new JLabel(商品4消费量 String str25 = String.form
19、at(,Spinsum4); label25.setText(str25); part25.add(label25); final JLabel label26=new JLabel(); part26.add(new JLabel(商品5消费量 String str26 = String.format(,Spinsum5); label26.setText(str26); part26.add(label26); final JLabel label27=new JLabel(); part27.add(new JLabel(消费人次 String str27 = String.format
20、(,huiyuansum); label27.setText(str27); part27.add(label27); button5.addActionListener(new ActionListener() sum2=0; Spinsum1=0; Spinsum2=0; Spinsum3=0; Spinsum4=0; Spinsum5=0; huiyuansum=0; label21.setText(label22.setText(label23.setText( label24.setText(label25.setText(label26.setText( label27.setTe
21、xt( ); duoxP.add(part21);duoxP.add(part22);duoxP.add(part23);duoxP.add(part24); duoxP.add(part25);duoxP.add(part26); duoxP.add(part27);duoxP.add(button5); return panel;5 总结与展望 通过本次课程设计使我受益匪浅,对课堂上所学的知识有了进一步的巩固加深,本次课程设计锻炼了我各个方面的能力,对java有了更深的理解,让我学会了如何用课堂上所学的知识与实际联系起来 ,使我更灵活的运用JAVA的各项功能,加深了我对代码的熟识度和理解度,为以后奠定了良好的基础。 参考文献1 刘万军主编.面向对象程序设计.北京:清华大学出版社,20062 宛延闿等编着.实用Java程序设计教程.北京:机械工业出版社,2006 3郑莉.王行言.马素霞编着.Java语言程序设计.北京:4丁振凡主编.Java语言实用教程.北京:北京邮电大学出版社,2005成绩评定成绩 教师签字
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1