色婷婷狠狠18禁久久YY,CHINESE性内射高清国产,国产女人18毛片水真多1,国产AV在线观看

java用戶管理系統和聊天室

錢諍諍1年前6瀏覽0評論

Java是一種面向對象編程語言,在網絡應用開發中得到廣泛應用。其中用戶管理系統和聊天室是非常常用的兩種應用。

用戶管理系統用于管理系統中的用戶,包括用戶的注冊、登錄、修改密碼等功能。以下是一個簡單的Java用戶管理系統代碼示例:

public class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
public class UserManager {
ListuserList = new ArrayList<>();
public void addUser(User user) {
userList.add(user);
}
public User getUser(String username) {
for (User user : userList) {
if (user.getUsername().equals(username)) {
return user;
}
}
return null;
}
public boolean loginUser(String username, String password) {
User user = getUser(username);
if (user != null && user.getPassword().equals(password)) {
return true;
}
return false;
}
}

聊天室是一個實時通信的應用,用戶可以在聊天室中實時發送消息和接收消息。以下是一個簡單的Java聊天室代碼示例:

public class ChatRoom {
private Listclients = new ArrayList<>();
private ServerSocket serverSocket;
public ChatRoom(int port) {
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
}
public void start() {
System.out.println("聊天室已啟動,等待客戶端連接...");
while (true) {
try {
Socket client = serverSocket.accept();
clients.add(client);
System.out.println("客戶端已連接:" + client.getInetAddress().getHostAddress());
new Thread(() ->{
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
String message = null;
while ((message = reader.readLine()) != null) {
sendMessage(message, client);
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void sendMessage(String message, Socket sourceClient) {
System.out.println("客戶端發送消息:" + message);
for (Socket client : clients) {
if (client != sourceClient) {
try {
PrintStream printStream = new PrintStream(client.getOutputStream());
printStream.println(message);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}