请用java代码写一个功能,要求: 1、客户端: (1)对三位候选人,可以投票和查询;投票针对某个人投票,查询显示三位候选人的票数; (2)设计一个简单输入界面,可进行投票和查询选择和投票时选择候选人; (3)收到服务器发回的信息要显示; 2、服务器: (1)设置数据结构保存当前三位候选人初始信息姓名、编号、票数等信息; (2)收到 投票请求 后,给该候选人票数加1,返回投票成功信息;如失败,返回投票失败提示信息及原因; (3)收到 查询请求 后,查询成功,返回三位候选人票数;查询失败的话,返回查询识别提示信息及原因。
时间: 2023-06-13 15:03:16 浏览: 73
以下是一个简单的 Java 代码实现,其中客户端和服务器端分别在不同的类中实现,使用了 Socket 编程实现客户端和服务器之间的通信。
客户端代码:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class VoteClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 8888);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String input;
while ((input = reader.readLine()) != null) {
out.println(input);
String response = in.readLine();
System.out.println(response);
if (response.equals("bye")) {
break;
}
}
}
}
```
服务器端代码:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
public class VoteServer {
private static Map<Integer, Candidate> candidates = new HashMap<>();
static {
candidates.put(1, new Candidate(1, "Alice", 0));
candidates.put(2, new Candidate(2, "Bob", 0));
candidates.put(3, new Candidate(3, "Charlie", 0));
}
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
while (true) {
Socket socket = serverSocket.accept();
new Thread(new VoteHandler(socket)).start();
}
}
private static class VoteHandler implements Runnable {
private Socket socket;
public VoteHandler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String input;
while ((input = in.readLine()) != null) {
String[] tokens = input.split(" ");
if (tokens[0].equals("VOTE")) {
int candidateId = Integer.parseInt(tokens[1]);
Candidate candidate = candidates.get(candidateId);
if (candidate != null) {
candidate.voteCount++;
out.println("投票成功");
} else {
out.println("投票失败:候选人不存在");
}
} else if (tokens[0].equals("QUERY")) {
StringBuilder sb = new StringBuilder();
for (Candidate candidate : candidates.values()) {
sb.append(candidate.name).append(": ").append(candidate.voteCount).append("\n");
}
out.println(sb.toString());
} else {
out.println("未知命令");
}
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static class Candidate {
private int id;
private String name;
private int voteCount;
public Candidate(int id, String name, int voteCount) {
this.id = id;
this.name = name;
this.voteCount = voteCount;
}
}
}
```
运行客户端代码后,可以通过以下命令进行投票和查询:
- 投票:输入 "VOTE 候选人编号",例如 "VOTE 1" 表示给编号为 1 的候选人投票;
- 查询:输入 "QUERY",即可查询所有候选人的票数。
服务器端会返回相应的结果,客户端将其打印在控制台上。
阅读全文