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

付费下载

下载本文档

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

文档简介

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

2、 MainGraph :接收候选者人数、姓名、当选标准信息; VoteGraph :接受用户的投票信息; ResultGraph :展示最终的投票结果; Can didate :候选人的实体对象,包括候选人的各种属性; 2.2类声明 本报告采用java编程语言,一共构造了六个类:1、Can didate; 2、Ma nager; 3、 Userlnterface ; 4、MainGraph ; 5、VoteGraph ; 6、ResultGraph。 2.2.1、候选者类:Can didate public class Can didate private String name 候选者姓名

3、private int id ; /候选者的编号 private int num 得票数量 private double voteResult ; / 得票率 private boolean isVote ; / 是否中选 Candidate(String name,int id) ; /Candidate的构造方法 public String getName(); public void setName(String name); public int getId(); public void setId( int id); public int getNum(); public void

4、setNum( int num); public double getVoteResult(); public void setVoteResult( double voteResult); public boolean isVote(); public void setVote( boolean isVote); 2.2.2、管理类:Manager 类 public class Manager public int can didates ; / 候选人的数量 public int percentage ; / 当选标准(百分比) public int voters = 0; / 投票人数量

5、 Listlistc ;/ 装载候选者的容器 public static void main(String args); public void calculateResult() ;/ 计算结果 2.2.3、 窗口的操作类Userl nterface public class UserInterfaceextends JFrame private static final long serialVersionUID = 1L; public Contain erconten tPa nel ; / 当前界面控制器 public Manager manager;/利于数据通信,主要包括:候选人

6、数量、当选标准、投 票者数量 public JPanel p1;/输入信息的面板 public JPanel p2;/ 投票面板 public JPanel p3;/显示投票结果的面板 public List list_textfield ; / 装载候选人姓名的容器 public Listlist_jradiobutton; / 投票 public UserI nterface(Ma nager m); public void update() ;/窗口中增删组建后立即更新 public void addCandi( int num); /panel1输入侯选者姓名完成后,生成候选者对象 2

7、.2.4、 输入面板类:MainGraph public class MainGraph private UserI nterface userI nterface ;/与主面板的通信 public MainGraph(Userlnterface userInterface); /MainGraph 的构造函数 public void launchMainGraph() ; / 加载主面板 2.2.5、 投票面板类:VoteGraph public class VoteGraph private UserI nterface userI nterface ;/与主面板的通信 public Vo

8、teGraph(UserInterface userInterface);/构造函数 public void launchVoteGraph() ; / 加载投票面板 2.2.6、 显示结果类:ResultGraph public class ResultGraph private UserI nterface userI nterface ;/与主面板的通信 public ResultGraph(UserInterface userInterface) ;/ 构造函数 public void launchResultGraph() ; / 加载显示结果的面板 3功能实现 3.1窗口显示 点击

9、运行后弹出一个窗口(Pan ell),要求用户输入候选人的个数(1-12)以及当选标 准(若为50%,只需输入50即可) 请辙人投票信息 常输人條議人tn令戟:门低: f 百甘IO : 1 假若依次输入12,50.点击确定后,窗口中会提示输入侯选人的姓名,候选人的编号是 从小到大依次生成的。 2 总迟黑却拧竺哥卑金城 请输人投票信息 碩确人慣谐人跑: 讳锚吁進标灌百 依次输入12人的姓名点击提交后,就会弹出投票的窗口 请为候选者投票 卩_: 1葺冥红窑 -爲刘罰莉 O鸥:工眾直 匚3号:部斉 C膚:車丹 |C 4号:爭陆 C 4垮住弘倉 J箱:掘三 C 号:李回 U贞号住cb丽 二q房:主並贺

10、 握立二|匚品不琴卑 假若共有7名投票人参与投票(每次投票完点击提交即为完成依次投票)投票过程如下: 投票者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号: :张三

11、10号 :王小慧 11号 :李四 12号:王龙贺 投票者7: 1 号: 吴红岩 2号 :刘莉莉 3号:邵芳 4号: :尹聪敏 7号:李泽 最终的投票结果如下图所示: 扶茸斤按票人兰选标薜; 3.2功能1 整个系统的UML时序图如下图所示: 片:nww Lto書rlnftrf孔悔 L_jEurxhH /候选者的编号 private int num 得票数量 private double voteResult ; / 得票率 private boolean isVote ; / 是否中选 Candidate(String name,int id) this . name= name; this .

12、 id = id; this . num= 0; this . voteResult = 0; this . isVote = false public String getName() return n ame public void setName(String name) this . name= name; public int getId() return id ; public void setId( int id) this . id = id; public int getNum() return num; public void setNum( int num) this .

13、 num= num; public double getVoteResult() return voteResult ; public void setVoteResult( double voteResult) this . voteResult = voteResult; public boolean isVote() return isVote ; public void setVote( boolean isVote) this . isVote = isVote; public String toString() return this . name; 2、Manager 类的详细代

14、码 import java.util.ArrayList; import java.util.List; public class Manager public int candidates ; / 候选人的数量 public int percentage ; / 当选标准(百分比) public int voters = 0;/ 投票人数量 Listlistc = new ArrayList(); public static void main(String args) Man ager m = new Man ager(); UserI nteface ui =new UserI ntef

15、ace(m); Mai nGraph mai nGraph =new Main Graph(ui); main Graph .laun chMa in Graph(); public void calculateResult() double d; for (int i = 0; i = percentage * 0.01) c.setVote( true ); 3、UserInterface类的详细代码: import java.awt.Color; import java.awt.Container; import java.awt.Font; import java.awt.event.

16、ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import iava.util.Vector; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; impo

17、rt javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.table.DefaultTableCelIRenderer; /* * author why * */ public class Userlnterfaceextends JFrame private static final long serialVersionUID= 1L; public Con tai ner

18、conten tPa nel ; / 当前界面控制器 public Manager manager; /利于数据通信,主要包括:候选人数量、当选标 准、投票者数量 public JPanel pl; public JPa nel p2; public JPanel p3; public public List list List /输入信息的面板 /投票面板 /显示投票结果的面板 _textfield = list_jradiobutt on new ArrayList(); =new ArrayList(); public Userl nterface(Ma nager m) super (

19、欢迎来到投票管理系统”); this . manager = m; this .setVisible( true ); this .setBounds(150, 30, 700, 700); EXIT_ON_CLOSE this .setDefaultCloseOperation(JFrame. this .setResizable( false ); this .setBackground(Color.black); contentPanel = this .getContentPane(); public void update() this .repaint(); this .setVi

20、sible( true ); public void addCandi( int num) JTextField text_ name; new JButton(提交”); JLabel label_ name; JButt on submit = Stri ng ss = int x1 = 80, x2 = 200, y = 150, w = 150, h = 20; int yy = y; for ( int i = 1; i = this .getHeight() - 250) y = yy; x1 = x1 + 300; x2 = x2 + 300; y += 50; text_nam

21、e = new JTextField(); / 新建输入姓名的文本框 label_ name = new JLabel(ss); label_name.setText( 候选人+ i +的姓名:”); 把输入姓名的文本框加入到list中 this .getHeight() - 150, 100, 20); label_ name.setBo un ds(x1, y, w, h); text_ name.setBo un ds(x2, y, w, h); list_textfield.add(text_ name);/ p1.add(label_ name); p1.add(text_ name

22、); this .update(); submit.setBounds( this .getWidth() / 3, submit.addAct ion Liste ner(new Action Liste ner() public void actionPerformed(ActionEvent e) for ( int i = 0; i list_textfield.size(); i+) Stri ng name = list_textfield.get(i).getText();/ 得到文本框 中输入的候选者姓名 Can didate candi =new Ca ndidate( na

23、me, i + 1);/ 根据输入的 姓名生成一个候选者 manager. listc .add(candi); / 把候选者加入到list 中 VoteGraph voteGraph = new VoteGraph(Userlnterface. this ); voteGraph.launchVoteGraph();/ 加载投票面板(Panel2) ); pl.add(submit); 4、MainGraph类的详细代码: import java.awt.Color; import java.awt.Container; import java.awt.Font; import iava.a

24、wt.HeadlessException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; /* * 系统的主界面,接受候选者人数,以及当选的标准 * author why * */ public class MainGraph private UserInterface u

25、serInterface ; / 与主面板的通信 public MainGraph(UserInterface userInterface) super (); this . userInterface = userInterface; public void launchMainGraph() userInterface userInterface userInterface userInterface . p1 = new JPanel(); . p1.setLayout( null ); . p1.setBackground(Color. orange); . contentPanel

26、.add( userInterface . p1); JLabel lab =new JLabel( 请输入投票信息 ); lab.setBounds(200, 30, 250, 50); int size = 25; lab.setFont( new Font( Serif , Font. PLAIN, size); JLabel label = new JLabel( 请输入候选人的个数 (1-12): ); final JTextField text =new JTextField();/ 输入候选人的个数 JLabel labelp = new JLabel( 请输当选标准 ( 百分比

27、 ): ); final JTextField pertage = new JTextField(); / 输入当选百分比 JButton b = new JButton( 确定 ); / 对候选人个数与百分比进行计较的按钮 label.setBounds(80, 100, 170, 20); labelp.setBounds(80, 130, 150, 20); text.setBounds(240, 100, 150, 20); pertage.setBounds(240, 130, 150, 20); b.setBounds(500, 130, 70, 20); b.addActionL

28、istener( new ActionListener() public void actionPerformed(ActionEvent e) userInterface . manager. candidates = Integer. parseInt (text.getText(); / 得到候选者的个数 userInterface .addCandi( userInterface . manager. candidates ); userInterface . manager. percentage = Integer. parseInt (pertage.getText(); / 得

29、到当选百分比 ); 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 .update(); 5、VoteGraph 类的详细代码: import java.awt.Color; import java.awt.Font; import java.awt.eve

30、nt.ActionEvent; import java.awt.event.ActionListener; import java.util.Iterator; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; public class VoteGraph private UserInterface userInterface ;/ 与主面板的通信 public VoteGraph(UserInterface use

31、rInterface) super (); this . userInterface = userInterface; public void launchVoteGraph() userInterface . list_jradiobutton .clear(); int x = 80, y = 150, w = 120, h = 20; int yy = y; JRadioButton jrb; userInterface . contentPanel .removeAll(); userInterface . p2 = new JPanel(); userInterface . cont

32、entPanel .add( userInterface . p2); userInterface . p2.setLayout( null ); userInterface . p2.setBackground(Color. orange); JLabel label =new JLabel( 请为候选者投票 ); label.setBounds(200, 100, 250, 50); int size = 25; label.setFont( new Font( Serif , Font. PLAIN, size); JButton voter_submit =new JButton( 提

33、交 ); voter_submit.setBounds(100, userInterface .getHeight() - 150, 100, 20); voter_submit.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) userInterface . manager. voters +; for ( int i = 0; i userInterface . manager. candidates ; i+) if ( userInterface . list_jradi

34、obutton .get(i).isSelected() userInterface . manager. listc .get(i).setNum( userInterface . manager. listc .get(i ).getNum() + 1); userInterface . contentPanel .remove( userInterface . p2); launchVoteGraph(); userInterface .update(); ); JButton result_submit = new JButton( 显示结果 ); result_submit.setB

35、ounds(370, userInterface .getHeight() - 150, 100, 20); result_submit.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) userInterface . manager.calculateResult(); ResultGraph result =new ResultGraph( userInterface ); result.launchResultGraph(); userInterface .update()

36、; ); Iterator iterator = userInterface . manager. listc .iterator(); for ( int i = 1; i = userInterface .getHeight() - 250) y = yy; x = x + 300; y += 50; jrb = new JRadioButton(i +号: + cand.getName(); jrb.setBounds(x, y, w, h); userInterface . list_jradiobutton.add(jrb); userInterface . p2.add(jrb);

37、 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 类的详细代码: import java.awt.Color; import java.awt.Font; import java.util.V

38、ector; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableCellRenderer; public class ResultGraph private UserInterface userInterface ;/ 与主面板的通信 public ResultGraph(UserInterface userInterface) super ();

39、 this . userInterface = userInterface; public void launchResultGraph() JLabel label =new JLabel( 最终结果 ); label.setBounds(300, 50, 250, 50); int size = 25; label.setFont(new Font( Serif , Font. PLAIN size); JLabel la = new JLabel(共有+ userInterface . manager. voters + 个投票人 当选标准:” + userInterface. manager. percentage + %); la.setBounds(200, 570, 250, 50); int si = 15; la.setFont( new Font( Serif , Font. PLAIN) si); JScrollPa ne scrollPa ne =new JScrollPa ne();/ 支持滚动 scrollPa ne.setBou nds(100, 150, 500, 380); String columnNames = 候选者编号,”姓名,”票数,得

温馨提示

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

评论

0/150

提交评论