用java设计一个投票系统
时间: 2023-12-17 21:03:55 浏览: 194
好的,那我们来设计一个简单的投票系统吧。首先,我们需要确定投票系统的功能和要求。
1. 系统可以创建多个投票活动,每个活动包括投票标题、投票选项等信息。
2. 用户可以选择参与某个投票活动,并进行投票。
3. 系统可以对每个投票选项进行统计,并给出投票结果。
有了以上的需求,我们就可以开始设计投票系统了。
1. 投票活动类
首先,我们需要设计一个投票活动类。该类应该包括投票标题、投票选项、参与人数等属性,以及添加选项、投票等方法。
```java
public class VoteActivity {
private String title; // 投票标题
private List<String> options; // 投票选项
private Map<String, Integer> results; // 投票结果
public VoteActivity(String title, List<String> options) {
this.title = title;
this.options = options;
this.results = new HashMap<>();
for (String option : options) {
this.results.put(option, 0);
}
}
public String getTitle() {
return title;
}
public List<String> getOptions() {
return options;
}
public void addOption(String option) {
options.add(option);
results.put(option, 0);
}
public void vote(String option) {
if (options.contains(option)) {
results.put(option, results.get(option) + 1);
}
}
public Map<String, Integer> getResults() {
return results;
}
}
```
2. 投票系统类
接下来,我们需要设计一个投票系统类。该类应该包括创建活动、参与活动、投票等方法。
```java
public class VoteSystem {
private List<VoteActivity> activities; // 投票活动列表
public VoteSystem() {
this.activities = new ArrayList<>();
}
public void createActivity(String title, List<String> options) {
VoteActivity activity = new VoteActivity(title, options);
activities.add(activity);
}
public List<VoteActivity> getActivities() {
return activities;
}
public void participate(int activityIndex, String option) {
VoteActivity activity = activities.get(activityIndex);
activity.vote(option);
}
public void vote(int activityIndex, String option) {
VoteActivity activity = activities.get(activityIndex);
activity.vote(option);
}
public Map<String, Integer> getResult(int activityIndex) {
VoteActivity activity = activities.get(activityIndex);
return activity.getResults();
}
}
```
3. 测试投票系统
最后,我们需要编写测试代码,来测试投票系统的功能。
```java
public class TestVoteSystem {
public static void main(String[] args) {
VoteSystem voteSystem = new VoteSystem();
voteSystem.createActivity("Which is your favorite color?", Arrays.asList("Red", "Green", "Blue"));
voteSystem.createActivity("Which is your favorite animal?", Arrays.asList("Dog", "Cat", "Horse"));
List<VoteActivity> activities = voteSystem.getActivities();
for (int i = 0; i < activities.size(); i++) {
VoteActivity activity = activities.get(i);
System.out.println(activity.getTitle());
List<String> options = activity.getOptions();
for (int j = 0; j < options.size(); j++) {
System.out.println((j + 1) + ". " + options.get(j));
}
Scanner scanner = new Scanner(System.in);
System.out.print("Please input your choice: ");
String choice = scanner.nextLine();
voteSystem.participate(i, options.get(Integer.parseInt(choice) - 1));
System.out.println("Thank you for voting!");
}
for (int i = 0; i < activities.size(); i++) {
VoteActivity activity = activities.get(i);
System.out.println(activity.getTitle());
Map<String, Integer> results = voteSystem.getResult(i);
for (Map.Entry<String, Integer> entry : results.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
}
```
以上就是一个简单的投票系统的实现,供参考。
阅读全文