投票管理系统_第1页
投票管理系统_第2页
投票管理系统_第3页
投票管理系统_第4页
投票管理系统_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、投票管理系统1 题目描述投票选举过程:(1)用户从键盘输入候选人的个数(提示用户输入人数为112)、当选标准(例:若为50,只需输入50即可)。点击确定后,由用户从键盘输入每一个候选人的姓名,提交后即可生成候选人的信息(包括姓名和编号)。(2)投票者就开始投票,对投票者没有姓名和编号的存储,只有结果的存储。(3)以表格的形式展示选举结果,包括:候选者的编号、姓名、所得票数、得票率、是否中选,并附投票人的个数和当选标准。2类设计2。1 类图Manager:控制整个系统,并对投票结果以及是否当选进行控制;UserInterface:控制整个系统的展示;MainGraph:接收候选者人数、姓名、当选

2、标准信息;VoteGraph:接受用户的投票信息;ResultGraph:展示最终的投票结果;Candidate:候选人的实体对象,包括候选人的各种属性;2。2 类声明本报告采用java编程语言,一共构造了六个类:1、Candidate; 2、Manager;3、UserInterface;4、MainGraph;5、VoteGraph;6、ResultGraph.2。2。1、候选者类:Candidatepublicclass Candidate private String name;/候选者姓名privateintid;/候选者的编号privateintnum; / 得票数量private

3、doublevoteResult; /得票率privatebooleanisVote;/是否中选Candidate(String name, int id) ;/Candidate 的构造方法public String getName() ;publicvoidsetName(String name) ;publicintgetId() ;publicvoidsetId(int id) ;publicintgetNum() ;publicvoidsetNum(intnum) ;publicdoublegetVoteResult() ;publicvoidsetVoteResult(double

4、voteResult) ;publicbooleanisVote();publicvoidsetVote(booleanisVote) ;2.2。2、管理类:Manager类publicclassManager publicintcandidates; / 候选人的数量publicintpercentage; / 当选标准(百分比)publicintvoters = 0; / 投票人数量ListCandidate listc ;/ 装载候选者的容器publicstaticvoid main(String args) ;publicvoidcalculateResult() ;/ 计算结果 2。

5、2。3、窗口的操作类UserInterfacepublicclassUserInterfaceextendsJFrame privatestaticfinallongserialVersionUID = 1L;public Container contentPanel; / 当前界面控制器public Manager manager; /利于数据通信,主要包括:候选人数量、当选标准、投票者数量publicJPanelp1; /输入信息的面板publicJPanelp2;/投票面板publicJPanelp3;/显示投票结果的面板public ListJTextField list_textfi

6、eld ;/装载候选人姓名的容器public List<JRadioButton list_jradiobutton ;/投票publicUserInterface(Manager m) ;publicvoid update() ;/窗口中增删组建后立即更新publicvoidaddCandi(intnum);/panel1输入侯选者姓名完成后,生成候选者对象2。2。4、输入面板类:MainGraphpublicclassMainGraphprivateUserInterfaceuserInterface; /与主面板的通信publicMainGraph(UserInterfaceuse

7、rInterface) ;/MainGraph的构造函数publicvoidlaunchMainGraph() ;/加载主面板2。2。5、投票面板类:VoteGraphpublicclassVoteGraph privateUserInterfaceuserInterface;/与主面板的通信publicVoteGraph(UserInterfaceuserInterface) ;/构造函数publicvoidlaunchVoteGraph();/加载投票面板2。2。6、显示结果类:ResultGraphpublicclassResultGraph privateUserInterfaceus

8、erInterface;/与主面板的通信publicResultGraph(UserInterfaceuserInterface) ;/构造函数publicvoidlaunchResultGraph() ;/加载显示结果的面板3 功能实现3。1 窗口显示点击运行后弹出一个窗口(Panel1),要求用户输入候选人的个数(112)以及当选标准(若为50,只需输入50即可)假若依次输入12, 50。点击确定后,窗口中会提示输入侯选人的姓名,候选人的编号是从小到大依次生成的。依次输入12人的姓名点击提交后,就会弹出投票的窗口假若共有7名投票人参与投票(每次投票完点击提交即为完成依次投票)投票过程如下:

9、投票者1:1号:吴红岩 3号:邵芳 9号:李丹 11号:李四投票者2:2号:刘莉莉 3号:邵芳 4号:尹聪敏 6号:王小丽8号:王永霞投票者3:1号:吴红岩 2号:刘莉莉 4号:尹聪敏 5号:张三7号:李泽 9号:李丹投票者4:1号:吴红岩 7号:李泽 12号:王龙贺投票者5:3号:邵芳 4号:尹聪敏 5号:张三 7号:李泽10号:王小慧投票者6:1号:吴红岩 2号:刘莉莉 4号:尹聪敏 5号:张三10号:王小慧 11号:李四 12号:王龙贺投票者7:1号:吴红岩 2号:刘莉莉 3号:邵芳 4号:尹聪敏7号:李泽最终的投票结果如下图所示:3。2 功能1整个系统的UML时序图如下图所示:附录:程

10、序源代码1、Candidate 的详细代码:publicclass Candidate private String name;/候选者姓名privateintid;/候选者的编号privateintnum; / 得票数量privatedoublevoteResult; /得票率privatebooleanisVote;/是否中选Candidate(String name, int id) this。name = name;this。id = id;this。num = 0;this。voteResult = 0;this.isVote = false;public String getNam

11、e() returnname;publicvoidsetName(String name) this。name = name;publicintgetId() returnid;publicvoidsetId(int id) this。id = id;publicintgetNum() returnnum;publicvoidsetNum(intnum) this。num = num;publicdoublegetVoteResult() returnvoteResult;publicvoidsetVoteResult(doublevoteResult) this。voteResult = v

12、oteResult;publicbooleanisVote() returnisVote;publicvoidsetVote(booleanisVote) this。isVote = isVote;public String toString() returnthis。name;2、 Manager类的详细代码importjava。util.ArrayList;importjava.util。List;publicclass Manager publicintcandidates; / 候选人的数量publicintpercentage; / 当选标准(百分比)publicintvoters

13、= 0; / 投票人数量ListCandidate> listc = newArrayList<Candidate>();publicstaticvoid main(String args) Manager m = newManager();UserInterfaceui = newUserInterface(m);MainGraphmainGraph = newMainGraph(ui);mainGraph。launchMainGraph();publicvoidcalculateResult() double d;for (int i = 0; i candidates;

14、 i+) Candidate c = listc。get(i);d = (double) c.getNum() / voters;c。setVoteResult(double) Math.round(d 10000) / 100);if (d = percentage * 0。01) c。setVote(true);3、 UserInterface类的详细代码:importjava。awt。Color;importjava.awt。Container;importjava.awt。Font;importjava。awt。event。ActionEvent;importjava.awt。even

15、t。ActionListener;importjava。util.ArrayList;importjava。util。Iterator;importjava。util.List;importjava。util.Vector;importjavax.swing.JButton;importjavax.swing。JFrame;importjavax。swing.JLabel;importjavax。swing.JPanel;importjavax.swing。JRadioButton;importjavax.swing。JScrollPane;importjavax。swing.JTable;i

16、mportjavax。swing。JTextField;importjavax。swing。SwingConstants;importjavax.swing。table。DefaultTableCellRenderer;/* author why */publicclassUserInterfaceextendsJFrame privatestaticfinallongserialVersionUID = 1L;public Container contentPanel; / 当前界面控制器public Manager manager; /利于数据通信,主要包括:候选人数量、当选标准、投票者数

17、量publicJPanelp1; /输入信息的面板publicJPanelp2;/投票面板publicJPanelp3;/显示投票结果的面板public ListJTextField list_textfield = newArrayListJTextField>();public ListJRadioButton> list_jradiobutton = newArrayList<JRadioButton();publicUserInterface(Manager m) super("欢迎来到投票管理系统”);this。manager = m;this。setVi

18、sible(true);this。setBounds(150, 30, 700, 700);this.setDefaultCloseOperation(JFrame。EXIT_ON_CLOSE);this.setResizable(false);this.setBackground(Color.black);contentPanel = this。getContentPane();publicvoid update() this。repaint();this。setVisible(true);publicvoidaddCandi(intnum) JTextFieldtext_name;JLab

19、ellabel_name;JButton submit = newJButton(”提交");String ss = ”";int x1 = 80, x2 = 200, y = 150, w = 150, h = 20;intyy = y;for (int i = 1; i = num; i+) if (y >= this.getHeight() - 250) y = yy;x1 = x1 + 300;x2 = x2 + 300;y += 50;text_name = newJTextField(); / 新建输入姓名的文本框label_name = newJLabe

20、l(ss);label_name。setText("候选人 ” + i + " 的姓名:”);label_name.setBounds(x1, y, w, h);text_name.setBounds(x2, y, w, h);list_textfield。add(text_name); / 把输入姓名的文本框加入到list中p1.add(label_name);p1.add(text_name);this。update();submit。setBounds(this.getWidth() / 3, this.getHeight() 150, 100, 20);submit

21、.addActionListener(newActionListener() publicvoidactionPerformed(ActionEvent e) for (int i = 0; i < list_textfield.size(); i+) String name = list_textfield。get(i)。getText(); / 得到文本框中输入的候选者姓名Candidate candi = new Candidate(name, i + 1); / 根据输入的姓名生成一个候选者manager.listc。add(candi); / 把候选者加入到list中VoteG

22、raphvoteGraph = newVoteGraph(UserInterface。this);voteGraph.launchVoteGraph(); / 加载投票面板(Panel2);p1.add(submit);4、 MainGraph类的详细代码:importjava。awt.Color;importjava.awt。Container;importjava。awt.Font;importjava.awt。HeadlessException;importjava.awt。event.ActionEvent;importjava。awt。event。ActionListener;imp

23、ortjavax。swing.JButton;importjavax。swing。JLabel;importjavax。swing.JPanel;importjavax。swing。JTextField;/* 系统的主界面,接受候选者人数,以及当选的标准 * author why /publicclassMainGraphprivateUserInterfaceuserInterface; /与主面板的通信publicMainGraph(UserInterfaceuserInterface) super();this。userInterface = userInterface;publicvo

24、idlaunchMainGraph() userInterface。p1 = newJPanel();userInterface。p1.setLayout(null);userInterface。p1。setBackground(Color。orange);userInterface。contentPanel.add(userInterface。p1);JLabel lab = newJLabel("请输入投票信息”);lab。setBounds(200, 30, 250, 50);int size = 25;lab。setFont(new Font("Serif”, Fo

25、nt。PLAIN, size));JLabel label = newJLabel(”请输入候选人的个数(112):”);finalJTextField text = newJTextField(); / 输入候选人的个数JLabellabelp = newJLabel(”请输当选标准(百分比):”);finalJTextFieldpertage = newJTextField(); / 输入当选百分比JButton b = newJButton(”确定”); / 对候选人个数与百分比进行计较的按钮label。setBounds(80, 100, 170, 20);labelp.setBoun

26、ds(80, 130, 150, 20);text。setBounds(240, 100, 150, 20);pertage。setBounds(240, 130, 150, 20);b.setBounds(500, 130, 70, 20);b。addActionListener(newActionListener() publicvoidactionPerformed(ActionEvent e) userInterface.manager。candidates = Integer.parseInt(text。getText()); / 得到候选者的个数userInterface。addC

27、andi(userInterface。manager。candidates);userInterface.manager。percentage = Integer。parseInt(pertage。getText()); / 得到当选百分比);userInterface。p1。add(label);userInterface。p1。add(lab);userInterface.p1。add(text);userInterface。p1.add(b);userInterface。p1。add(labelp);userInterface.p1。add(pertage);userInterface。

28、update();5、 VoteGraph类的详细代码:importjava.awt。Color;importjava.awt.Font;importjava.awt。event。ActionEvent;importjava。awt。event。ActionListener;importjava。util。Iterator;importjavax。swing.JButton;importjavax。swing.JLabel;importjavax.swing。JPanel;importjavax。swing。JRadioButton;publicclassVoteGraph privateUs

29、erInterfaceuserInterface;/与主面板的通信publicVoteGraph(UserInterfaceuserInterface) super();this。userInterface = userInterface;publicvoidlaunchVoteGraph() userInterface。list_jradiobutton。clear();int x = 80, y = 150, w = 120, h = 20;intyy = y;JRadioButtonjrb;userInterface。contentPanel。removeAll();userInterf

30、ace.p2 = newJPanel();userInterface。contentPanel。add(userInterface。p2);userInterface.p2。setLayout(null);userInterface.p2。setBackground(Color.orange);JLabel label = newJLabel("请为候选者投票”);label.setBounds(200, 100, 250, 50);int size = 25;label。setFont(new Font(”Serif”, Font.PLAIN, size));JButtonvote

31、r_submit = newJButton(”提交”);voter_submit.setBounds(100, userInterface。getHeight() - 150, 100, 20);voter_submit。addActionListener(newActionListener() publicvoidactionPerformed(ActionEvent e) userInterface。manager.voters+;for (int i = 0; i userInterface。manager。candidates; i+) if (userInterface。list_j

32、radiobutton。get(i).isSelected()) userInterface。manager。listc。get(i)。setNum(userInterface.manager.listc.get(i)。getNum() + 1);userInterface。contentPanel。remove(userInterface.p2);launchVoteGraph();userInterface。update(););JButtonresult_submit = newJButton("显示结果");result_submit。setBounds(370,

33、userInterface。getHeight() 150, 100, 20);result_submit。addActionListener(newActionListener() publicvoidactionPerformed(ActionEvent e) userInterface。manager。calculateResult();ResultGraph result = newResultGraph(userInterface);result.launchResultGraph();userInterface。update(););Iterator<Candidate it

34、erator = userInterface。manager。listc。iterator();for (int i = 1; i = userInterface.manager。candidates; i+) / 添加候选者以备投票用Candidate cand = iterator。next();if (y = userInterface.getHeight() 250) y = yy;x = x + 300;y += 50;jrb = newJRadioButton(i + ”号: " + cand.getName());jrb.setBounds(x, y, w, h);us

35、erInterface.list_jradiobutton。add(jrb);userInterface.p2.add(jrb);userInterface.update();userInterface。p2。add(voter_submit);userInterface。p2.add(result_submit);userInterface。p2.add(label);userInterface.contentPanel。add(userInterface。p2);userInterface.update();6、 ResultGraph类的详细代码:importjava.awt。Color

36、;importjava。awt.Font;importjava.util。Vector;importjavax。swing。JLabel;importjavax。swing。JPanel;importjavax.swing。JScrollPane;importjavax。swing.JTable;importjavax.swing。table。DefaultTableCellRenderer;publicclassResultGraph privateUserInterfaceuserInterface;/与主面板的通信publicResultGraph(UserInterfaceuserIn

37、terface) super();this。userInterface = userInterface;publicvoidlaunchResultGraph() JLabel label = newJLabel(”最终结果");label。setBounds(300, 50, 250, 50);int size = 25;label.setFont(new Font("Serif”, Font。PLAIN, size);JLabel la = newJLabel("共有” + userInterface.manager.voters + ”个投票人当选标准:”+ userInterface。manager.percentage + ”");la.setBounds(200, 570, 250, 50);intsi = 15;la.setFont(new Font(”Serif”, Font。PLAIN, si);JScrollPanescrollPane = newJScrollPane(); / 支持滚动scrollPane.setBounds(100, 150, 500, 380);String columnNames = ”候选者编号”, ”姓名”, ”票数”, ”得票率", ”是否中选”

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论