投票选举

有一个班采用民主投票方法推选班长,班长候选人共4位,每个人姓名及代号分别为“张三  1;李四  2;王五  3;赵六  4”。程序操作员将每张选票上所填的代号(1、2、3或4)循环输入电脑,输入数字0结束输入,然后将所有候选人的得票情况显示出来,并显示最终当选者的信息,具体要求如下:

A、要求用面向对象方法,编写学生类Student,将候选人姓名、代号和票数保存到类Student中,并实现相应的getXXX 和 setXXX方法。

B、输入数据前,显示出各位候选人的代号及姓名(提示,建立一个候选人类型数组)。

C、循环执行接收键盘输入的班长候选人代号,直到输入的数字为0,结束选票的输入工作。

D、在接收每次输入的选票后要求验证该选票是否有效,即如果输入的数不是0、1、2、3、4这5个数字之一,或者输入的是一串字母,应显示出错误提示信息“此选票无效,请输入正确的候选人代号!”,并继续等待输入。

E、输入结束后显示所有候选人的得票情况,如参考样例所示。

F、输出最终当选者的相关信息,如参考样例所示。

1:张三【0票】
2:李四【0票】
3:王五【0票】
4:赵六【0票】
请输入班长候选人代号(数字0结束):1
请输入班长候选人代号(数字0结束):1
请输入班长候选人代号(数字0结束):1
请输入班长候选人代号(数字0结束):2
请输入班长候选人代号(数字0结束):3
请输入班长候选人代号(数字0结束):4
请输入班长候选人代号(数字0结束):5
此选票无效,请输入正确的候选人代号!
请输入班长候选人代号(数字0结束):hello
此选票无效,请输入正确的候选人代号!
请输入班长候选人代号(数字0结束):0
    1:张三【4票】
    2:李四【1票】
    3:王五【1票】
    4:赵六【1票】

投票最终结果:张三同学,最后以4票当选班长!

 

 

1、 建立学生类,这个类里面需要保存有编号、姓名、票数。

 1 package com.baidu.demo.vote;
 2 
 3 public class Student implements Comparable<Student>{
 4     private long sid;
 5     private String name;
 6     private int vote;
 7     public Student(long sid,String name,int vote) {
 8         this.sid = sid;
 9         this.name = name;
10         this.vote = vote;
11     }
12     @Override
13     public String toString() {
14         return "【" + this.sid + "】" + this.name + "、票数" + this.vote;
15     }
16     @Override
17     public int compareTo(Student student) {
18         return student.vote - this.vote;
19     }
20     public long getSid() {
21         return this.sid;
22     }
23     public void setSid(long sid) {
24         this.sid = sid;
25     }
26     public String getName() {
27         return this.name;
28     }
29     public void setName(String name) {
30         this.name = name;
31     }
32     public int getVote() {
33         return this.vote;
34     }
35     public void setVote(int vote) {
36         this.vote = vote;
37     }
38 }

 

2、 定义投票处理的业务接口

1 package com.baidu.demo.service;
2 
3 import com.baidu.demo.vote.Student;
4 
5 public interface IVoteService {
6     public boolean increase(long sid);
7     public Student[] result();
8     public Student[] getData();
9 }

3、 定义VoteServiceImpl子类。

 1 package com.baidu.demo.service;
 2 
 3 import java.util.Arrays;
 4 import com.baidu.demo.vote.Student;
 5 
 6 public class VoteServiceImpl implements IVoteService {
 7     private Student[] students = new Student[] {
 8         new Student(1, "张三", 0),
 9         new Student(2, "李四", 0),
10         new Student(3, "王五", 0),
11         new Student(4, "赵六", 0)
12     };
13     
14     @Override
15     public boolean increase(long sid) {
16         for(int x=0;x<students.length;x++) {
17             if(this.students[x].getSid() == sid) {
18                 this.students[x].setVote(this.students[x].getVote() + 1);
19                 return true;
20             }
21         }
22         return false;
23     }
24 
25     @Override
26     public Student[] result() {
27         Arrays.sort(this.students);
28         return this.students;
29     }
30 
31     @Override
32     public Student[] getData() {
33         return this.students;
34     }
35 }

4、 定义工厂类

 1 package com.baidu.demo.factory;
 2 
 3 import com.baidu.demo.service.IVoteService;
 4 import com.baidu.demo.service.VoteServiceImpl;
 5 
 6 public class Factory {
 7     private Factory() {}
 8     public static IVoteService getInstance() {
 9         return new VoteServiceImpl();
10     }
11 }

5、 定义一个工具类

 1 package com.baidu.demo.util;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.IOException;
 5 import java.io.InputStreamReader;
 6 
 7 public class InputUtil {
 8     private static final BufferedReader INPUT = new BufferedReader(new InputStreamReader(System.in));
 9     private InputUtil() {}
10     public static String getString(String prompt) {
11         String str = null;
12         boolean flag = true;
13         while(flag) {
14             System.out.println(prompt);
15             try {
16                 str = INPUT.readLine().trim();
17                 if(!"".equals(str)) {
18                     flag = false;
19                 }else {
20                     System.out.println("输入的内容不能为空!");
21                 }    
22             } catch (Exception e) {
23                 System.out.println("输入的内容不能为空!");
24             }
25         }
26         return str;
27     }
28     
29     public static int getInt(String prompt) { 
30         BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)) ;
31         int num = 0 ;
32         boolean flag = true ;
33         while (flag) {
34             System.out.print(prompt); // 打印提示信息
35             String str = null ; 
36             try {
37                 str = buf.readLine() ;
38                 if (str.matches("\d+")) {
39                     num = Integer.parseInt(str) ;
40                     flag = false ;
41                 } else {
42                     System.out.println("输入的内容不是数字!");
43                 }
44             } catch (IOException e) {
45                 System.out.println("输入的内容不是数字!");
46             }
47         }
48         return num ;
49     }
50 }

6、 定义一个菜单的信息显示类

 1 package com.baidu.demo.menu;
 2 
 3 import com.baidu.demo.factory.Factory;
 4 import com.baidu.demo.service.IVoteService;
 5 import com.baidu.demo.vote.Student;
 6 import com.baidu.demo.util.InputUtil;
 7 
 8 public class Menu {
 9     private IVoteService voteService;
10     public Menu() {
11         this.voteService = Factory.getInstance();
12         this.vote();
13     }
14     public void vote() {
15         Student student[] = this.voteService.getData();
16         for(Student temp: student) {
17             System.out.println(temp.getSid()+":"+temp.getName()+"【"+temp.getVote()+"】");
18         }
19         int num = 10;
20         while (num != 0) {
21             num = InputUtil.getInt("请输入班长候选人带好(数字0结束):");
22             if(num != 0) {
23                 if(!this.voteService.increase(num)) {
24                     System.out.println("此选票无效,请输入正确的候选代号!");
25                 }
26             }
27         }
28         System.out.println("投票最终结果:");
29         student = this.voteService.result();
30         System.out.println(student[0].getName()+"同学,以"+student[0].getVote()+"票当选班长。");
31     }
32 }

7、 定义测试类

1 package com.baidu.demo;
2 
3 import com.baidu.demo.menu.Menu;
4 
5 public class IOCaseDemo {
6     public static void main(String[] args) {
7         new Menu();
8     }
9 }
原文地址:https://www.cnblogs.com/sunzhongyu008/p/11214785.html