利用java,写一个身份证管理系统,要求能够增加信息、删除信息、查询信息、修改信息、将信息保存为文件等功能
时间: 2023-06-20 13:03:55 浏览: 179
好的,以下是一个简单的身份证管理系统的Java代码,实现了增加信息、删除信息、查询信息、修改信息、将信息保存为文件等功能。
```java
import java.io.*;
import java.util.*;
class Person implements Serializable { // Person类实现了序列化接口Serializable
private String name;
private String id;
public Person(String name, String id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public String toString() { // 重写toString方法
return "Name: " + name + ", ID: " + id;
}
}
class PersonManager {
private List<Person> persons = new ArrayList<Person>();
private Scanner scanner = new Scanner(System.in);
public void addPerson() {
System.out.print("Enter name: ");
String name = scanner.nextLine();
System.out.print("Enter ID: ");
String id = scanner.nextLine();
persons.add(new Person(name, id));
System.out.println("Add successfully.");
}
public void deletePerson() {
System.out.print("Enter the ID that you want to delete: ");
String id = scanner.nextLine();
boolean flag = false;
for (int i = 0; i < persons.size(); i++) {
if (persons.get(i).getId().equals(id)) {
persons.remove(i);
flag = true;
break;
}
}
if (flag) {
System.out.println("Delete successfully.");
} else {
System.out.println("The ID is not exist.");
}
}
public void queryPerson() {
System.out.print("Enter the ID that you want to query: ");
String id = scanner.nextLine();
boolean flag = false;
for (Person person : persons) {
if (person.getId().equals(id)) {
System.out.println(person);
flag = true;
break;
}
}
if (!flag) {
System.out.println("The ID is not exist.");
}
}
public void modifyPerson() {
System.out.print("Enter the ID that you want to modify: ");
String id = scanner.nextLine();
boolean flag = false;
for (Person person : persons) {
if (person.getId().equals(id)) {
System.out.print("Enter new name: ");
String name = scanner.nextLine();
person = new Person(name, id);
flag = true;
break;
}
}
if (flag) {
System.out.println("Modify successfully.");
} else {
System.out.println("The ID is not exist.");
}
}
public void saveToFile() {
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("persons.txt"));
out.writeObject(persons);
out.close();
System.out.println("Save successfully.");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
public void loadFromFile() {
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("persons.txt"));
persons = (List<Person>) in.readObject();
in.close();
System.out.println("Load successfully.");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
public class IdCardManagementSystem {
private static PersonManager manager = new PersonManager();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.println("1. Add person");
System.out.println("2. Delete person");
System.out.println("3. Query person");
System.out.println("4. Modify person");
System.out.println("5. Save to file");
System.out.println("6. Load from file");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
String choice = scanner.nextLine();
switch (choice) {
case "1":
manager.addPerson();
break;
case "2":
manager.deletePerson();
break;
case "3":
manager.queryPerson();
break;
case "4":
manager.modifyPerson();
break;
case "5":
manager.saveToFile();
break;
case "6":
manager.loadFromFile();
break;
case "0":
System.exit(0);
break;
default:
System.out.println("Invalid choice.");
break;
}
}
}
}
```
这个身份证管理系统使用了面向对象的思想,定义了一个Person类来表示一个人的信息,然后定义了一个PersonManager类来管理Person对象,实现了增加、删除、查询、修改、保存到文件、从文件读取等功能。在主函数中,利用一个循环来不断接受用户输入,并根据用户的选择来调用PersonManager对象的相应方法。
阅读全文