用Java写1、利用ArrayList读入guest数据表; 2、建立简单的用户表管理系统,系统主要功能如下: ----------用户信息管理系统--------- 1 添加用户 2 修改用户 3 删除用户 4 用户列表 5 退出
时间: 2023-12-20 14:32:26 浏览: 90
以下是用Java实现的读取guest数据表和用户信息管理系统的代码示例:
1. 利用ArrayList读入guest数据表:
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class GuestData {
public static void main(String[] args) {
List<String> guestList = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader("guest.txt"))) {
String line;
while ((line = br.readLine()) != null) {
guestList.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
// 打印读取到的guest数据表
for (String guest : guestList) {
System.out.println(guest);
}
}
}
```
2. 建立简单的用户表管理系统:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class UserManagementSystem {
private static List<String> userList = new ArrayList<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("--用户信息管理系统--");
System.out.println("1 添加用户");
System.out.println("2 修改用户");
System.out.println("3 删除用户");
System.out.println("4 用户列表");
System.out.println("5 退出");
System.out.print("请选择操作:");
choice = scanner.nextInt();
switch (choice) {
case 1:
addUser(scanner);
break;
case 2:
modifyUser(scanner);
break;
case 3:
deleteUser(scanner);
break;
case 4:
listUsers();
break;
case 5:
System.out.println("退出系统");
break;
default:
System.out.println("无效的选择");
break;
}
} while (choice != 5);
}
private static void addUser(Scanner scanner) {
System.out.print("请输入用户名:");
String username = scanner.next();
userList.add(username);
System.out.println("用户添加成功");
}
private static void modifyUser(Scanner scanner) {
System.out.print("请输入要修改的用户名:");
String oldUsername = scanner.next();
System.out.print("请输入新的用户名:");
String newUsername = scanner.next();
int index = userList.indexOf(oldUsername);
if (index != -1) {
userList.set(index, newUsername);
System.out.println("用户修改成功");
} else {
System.out.println("用户不存在");
}
}
private static void deleteUser(Scanner scanner) {
System.out.print("请输入要删除的用户名:");
String username = scanner.next();
if (userList.remove(username)) {
System.out.println("用户删除成功");
} else {
System.out.println("用户不存在");
}
}
private static void listUsers() {
System.out.println("用户列表:");
for (String user : userList) {
System.out.println(user);
}
}
}
```
阅读全文