当前位置:文档之家› 投票管理系统

投票管理系统

投票管理系统1 题目描述投票选举过程:(1)用户从键盘输入候选人的个数(提示用户输入人数为1-12)、当选标准(例:若为50%,只需输入50即可)。

点击确定后,由用户从键盘输入每一个候选人的姓名,提交后即可生成候选人的信息(包括姓名和编号)。

(2)投票者就开始投票,对投票者没有姓名和编号的存储,只有结果的存储。

(3)以表格的形式展示选举结果,包括:候选者的编号、姓名、所得票数、得票率、是否中选,并附投票人的个数和当选标准。

2类设计2.1 类图Manager:控制整个系统,并对投票结果以及是否当选进行控制;UserInterface:控制整个系统的展示;MainGraph:接收候选者人数、姓名、当选标准信息;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;//候选者姓名privateint id; //候选者的编号privateint num; // 得票数量privatedouble voteResult; //得票率privateboolean isVote; //是否中选Candidate(String name, int id) ;//Candidate 的构造方法public String getName() ;publicvoid setName(String name) ;publicint getId() ;publicvoid setId(int id) ;publicint getNum() ;publicvoid setNum(int num) ;publicdouble getVoteResult() ;publicvoid setVoteResult(double voteResult) ;publicboolean isVote();publicvoid setVote(boolean isVote) ;}2.2.2、管理类:Manager类publicclass Manager {publicint candidates; // 候选人的数量publicint percentage; // 当选标准(百分比)publicint voters = 0; // 投票人数量List<Candidate>listc ;// 装载候选者的容器publicstaticvoid main(String[] args) ;publicvoid calculateResult() ;// 计算结果}2.2.3、窗口的操作类UserInterfacepublicclass UserInterface extends JFrame {privatestaticfinallong serialVersionUID = 1L;public Container contentPanel; // 当前界面控制器public Manager manager; //利于数据通信,主要包括:候选人数量、当选标准、投票者数量public JPanel p1; //输入信息的面板public JPanel p2; //投票面板public JPanel p3; //显示投票结果的面板public List<JTextField>list_textfield ;//装载候选人姓名的容器public List<JRadioButton>list_jradiobutton ;//投票public UserInterface(Manager m) ;publicvoid update() ;//窗口中增删组建后立即更新publicvoid addCandi(int num);//panel1输入侯选者姓名完成后,生成候选者对象}2.2.4、输入面板类:MainGraphpublicclass MainGraph{private UserInterface userInterface; //与主面板的通信public MainGraph(UserInterfaceuserInterface) ;//MainGraph的构造函数publicvoid launchMainGraph() ;//加载主面板}2.2.5、投票面板类:VoteGraphpublicclass VoteGraph {private UserInterface userInterface;//与主面板的通信public VoteGraph(UserInterfaceuserInterface) ;//构造函数publicvoid launchVoteGraph();//加载投票面板}2.2.6、显示结果类:ResultGraphpublicclass ResultGraph {private UserInterface userInterface;//与主面板的通信public ResultGraph(UserInterfaceuserInterface) ;//构造函数publicvoid launchResultGraph() ;//加载显示结果的面板}3 功能实现3.1 窗口显示点击运行后弹出一个窗口(Panel1),要求用户输入候选人的个数(1-12)以及当选标准(若为50%,只需输入50即可)假若依次输入12,50.点击确定后,窗口中会提示输入侯选人的姓名,候选人的编号是从小到大依次生成的。

依次输入12人的姓名点击提交后,就会弹出投票的窗口假若共有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号:张三10号:王小慧11号:李四12号:王龙贺投票者7:1号:吴红岩2号:刘莉莉3号:邵芳4号:尹聪敏7号:李泽最终的投票结果如下图所示:3.2 功能1整个系统的UML时序图如下图所示:附录:程序源代码1、Candidate 的详细代码:publicclass Candidate {private String name;//候选者姓名privateint id; //候选者的编号privateint num; // 得票数量privatedouble voteResult; //得票率privateboolean isVote; //是否中选Candidate(String name, int id) { = name;this.id = id;this.num = 0;this.voteResult = 0;this.isVote = false;}public String getName() {return name;}publicvoid setName(String name) { = name;}publicint getId() {return id;}publicvoid setId(int id) {this.id = id;}publicint getNum() {return num;}publicvoid setNum(int num) {this.num = num;}publicdouble getVoteResult() {return voteResult;}publicvoid setVoteResult(double voteResult) { this.voteResult = voteResult;}publicboolean isVote() {return isVote;}publicvoid setVote(boolean isVote) {this.isVote = isVote;}public String toString() {;}}2、Manager类的详细代码import java.util.ArrayList;import java.util.List;publicclass Manager {publicint candidates; // 候选人的数量publicint percentage; // 当选标准(百分比)publicint voters = 0; // 投票人数量List<Candidate>listc = new ArrayList<Candidate>();publicstaticvoid main(String[] args) {Manager m = new Manager();UserInterfaceui = new UserInterface(m);MainGraphmainGraph = new MainGraph(ui);unchMainGraph();}publicvoid calculateResult() {double d;for (int i = 0; i <candidates; 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类的详细代码:import java.awt.Color;import java.awt.Container;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Vector;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JRadioButton;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextField;import javax.swing.SwingConstants;import javax.swing.table.DefaultTableCellRenderer;/**** @author why**/publicclass UserInterface extends JFrame {privatestaticfinallong serialVersionUID = 1L;public Container contentPanel; // 当前界面控制器public Manager manager; //利于数据通信,主要包括:候选人数量、当选标准、投票者数量public JPanel p1; //输入信息的面板public JPanel p2; //投票面板public JPanel p3; //显示投票结果的面板public List<JTextField>list_textfield = new ArrayList<JTextField>();public List<JRadioButton>list_jradiobutton = new ArrayList<JRadioButton>();public UserInterface(Manager m) {super("欢迎来到投票管理系统");this.manager = m;this.setVisible(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);}publicvoid addCandi(int num) {JTextFieldtext_name;JLabellabel_name;JButton submit = new JButton("提交");String ss = "";int x1 = 80, x2 = 200, y = 150, w = 150, h = 20;int yy = 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 = new JTextField(); // 新建输入姓名的文本框label_name = new JLabel(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.addActionListener(new ActionListener() {publicvoid actionPerformed(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中}VoteGraphvoteGraph = new VoteGraph(UserInterface.this);unchVoteGraph(); // 加载投票面板(Panel2)}});p1.add(submit);}}4、MainGraph类的详细代码:import java.awt.Color;import java.awt.Container;import java.awt.Font;import java.awt.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**/publicclass MainGraph{private UserInterface userInterface; //与主面板的通信public MainGraph(UserInterfaceuserInterface) {super();erInterface = userInterface;}publicvoid launchMainGraph() {userInterface.p1 = new JPanel();userInterface.p1.setLayout(null);userInterface.p1.setBackground(Color.orange);userInterface.contentPanel.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(); // 输入候选人的个数JLabellabelp = new JLabel("请输当选标准(百分比):");final JTextFieldpertage = 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.addActionListener(new ActionListener() {publicvoid actionPerformed(ActionEvent e) {userInterface.manager.candidates =Integer.parseInt(text.getText()); // 得到候选者的个数userInterface.addCandi(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.update();}}5、VoteGraph类的详细代码:import java.awt.Color;import java.awt.Font;import java.awt.event.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;publicclass VoteGraph {private UserInterface userInterface;//与主面板的通信public VoteGraph(UserInterfaceuserInterface) {super();erInterface = userInterface;}publicvoid launchVoteGraph() {userInterface.list_jradiobutton.clear();int x = 80, y = 150, w = 120, h = 20;int yy = y;JRadioButtonjrb;userInterface.contentPanel.removeAll();userInterface.p2 = new JPanel();userInterface.contentPanel.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));JButtonvoter_submit = new JButton("提交");voter_submit.setBounds(100, userInterface.getHeight() - 150, 100, 20);voter_submit.addActionListener(new ActionListener() {publicvoid actionPerformed(ActionEvent e) {userInterface.manager.voters++;for (int i = 0; i <userInterface.manager.candidates; i++) {if (userInterface.list_jradiobutton.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 = new JButton("显示结果");result_submit.setBounds(370, userInterface.getHeight() - 150, 100, 20);result_submit.addActionListener(new ActionListener() {publicvoid actionPerformed(ActionEvent e) {userInterface.manager.calculateResult();ResultGraph result = new ResultGraph(userInterface);unchResultGraph();userInterface.update();}});Iterator<Candidate> iterator = 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 = new JRadioButton(i + "号: " + cand.getName());jrb.setBounds(x, y, w, h);userInterface.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类的详细代码:import java.awt.Color;import java.awt.Font;import java.util.Vector;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.table.DefaultTableCellRenderer;publicclass ResultGraph {private UserInterface userInterface;//与主面板的通信public ResultGraph(UserInterfaceuserInterface) {super();erInterface = userInterface;}publicvoid 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));JScrollPanescrollPane = new JScrollPane(); // 支持滚动scrollPane.setBounds(100, 150, 500, 380);String[] columnNames = { "候选者编号", "姓名", "票数", "得票率", "是否中选" };Vector<String>columnNameV = new Vector<String>(); // 获得表头for (int column = 0; column < 5; column++) {columnNameV.add(columnNames[column]);}VectortableValueV = new Vector();for (int row = 0; row <userInterface.manager.candidates; row++) // 获得数据{Candidate c = userInterface.manager.listc.get(row);VectorrowV = new Vector();for (int column = 0; column <columnNames.length; column++) {switch (column) {case 0:rowV.add(c.getId());break;case 1:rowV.add(c.getName());break;case 2:rowV.add(c.getNum());break;case 3:rowV.add(c.getVoteResult() + "%");break;case 4:rowV.add(c.isVote());break;}}tableValueV.add(rowV);}final JTable table = new JTable(tableValueV, columnNameV); // 自定义的表格table.setRowHeight(30);table.setBackground(Color.yellow);// 使表格中的文字居中对齐DefaultTableCellRenderer r = new DefaultTableCellRenderer();r.setHorizontalAlignment(JLabel.CENTER);table.setDefaultRenderer(Object.class, r);scrollPane.setViewportView(table);scrollPane.setBackground(Color.magenta);userInterface.contentPanel.removeAll();userInterface.p3 = new JPanel();userInterface.p3.setBackground(Color.orange);userInterface.p3.setLayout(null);userInterface.p3.add(label);userInterface.p3.add(la);userInterface.p3.add(scrollPane);userInterface.contentPanel.add(userInterface.p3);userInterface.update();}}。

相关主题