利用java,写一个身份证管理系统,要求实现添加信息、删除信息、修改信息、查询信息、将录入的信息输出、将录入的信息保存为文件等功能,身份证信息要求包括姓名、性别、出生日期、身份证号码、出生地址等信息
时间: 2023-12-22 07:03:31 浏览: 143
基于JAVA的个人信息管理系统
4星 · 用户满意度95%
好的,以下是一个简单的身份证管理系统的Java代码实现:
```java
import java.io.*;
import java.util.*;
class IDCard {
private String name;
private String gender;
private String birthdate;
private String idNumber;
private String address;
public IDCard(String name, String gender, String birthdate, String idNumber, String address) {
this.name = name;
this.gender = gender;
this.birthdate = birthdate;
this.idNumber = idNumber;
this.address = address;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public String getBirthdate() {
return birthdate;
}
public String getIdNumber() {
return idNumber;
}
public String getAddress() {
return address;
}
public void setName(String name) {
this.name = name;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setBirthdate(String birthdate) {
this.birthdate = birthdate;
}
public void setIdNumber(String idNumber) {
this.idNumber = idNumber;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "姓名:" + name + " 性别:" + gender + " 出生日期:" + birthdate + " 身份证号码:" + idNumber + " 出生地址:" + address;
}
}
public class IDCardManagementSystem {
private static final Scanner sc = new Scanner(System.in);
private static final List<IDCard> idCardList = new ArrayList<>();
public static void main(String[] args) {
while (true) {
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.println("6. 将录入的信息保存为文件");
System.out.println("0. 退出");
System.out.println("----------------------");
System.out.print("请输入要进行的操作:");
int choice = sc.nextInt();
switch (choice) {
case 0:
System.out.println("退出程序!");
System.exit(0);
case 1:
addIDCard();
break;
case 2:
deleteIDCard();
break;
case 3:
updateIDCard();
break;
case 4:
queryIDCard();
break;
case 5:
outputIDCardList();
break;
case 6:
saveIDCardListToFile();
break;
default:
System.out.println("输入有误,请重新输入!");
}
}
}
private static void addIDCard() {
System.out.print("请输入姓名:");
String name = sc.next();
System.out.print("请输入性别:");
String gender = sc.next();
System.out.print("请输入出生日期:");
String birthdate = sc.next();
System.out.print("请输入身份证号码:");
String idNumber = sc.next();
System.out.print("请输入出生地址:");
String address = sc.next();
IDCard idCard = new IDCard(name, gender, birthdate, idNumber, address);
idCardList.add(idCard);
System.out.println("添加成功!");
}
private static void deleteIDCard() {
System.out.print("请输入要删除的身份证号码:");
String idNumber = sc.next();
boolean flag = false;
for (IDCard idCard : idCardList) {
if (idCard.getIdNumber().equals(idNumber)) {
idCardList.remove(idCard);
flag = true;
break;
}
}
if (flag) {
System.out.println("删除成功!");
} else {
System.out.println("未找到该身份证号码,删除失败!");
}
}
private static void updateIDCard() {
System.out.print("请输入要修改的身份证号码:");
String idNumber = sc.next();
boolean flag = false;
for (IDCard idCard : idCardList) {
if (idCard.getIdNumber().equals(idNumber)) {
System.out.print("请输入姓名:");
idCard.setName(sc.next());
System.out.print("请输入性别:");
idCard.setGender(sc.next());
System.out.print("请输入出生日期:");
idCard.setBirthdate(sc.next());
System.out.print("请输入身份证号码:");
idCard.setIdNumber(sc.next());
System.out.print("请输入出生地址:");
idCard.setAddress(sc.next());
flag = true;
break;
}
}
if (flag) {
System.out.println("修改成功!");
} else {
System.out.println("未找到该身份证号码,修改失败!");
}
}
private static void queryIDCard() {
System.out.print("请输入要查询的身份证号码:");
String idNumber = sc.next();
boolean flag = false;
for (IDCard idCard : idCardList) {
if (idCard.getIdNumber().equals(idNumber)) {
System.out.println(idCard);
flag = true;
break;
}
}
if (!flag) {
System.out.println("未找到该身份证号码!");
}
}
private static void outputIDCardList() {
for (IDCard idCard : idCardList) {
System.out.println(idCard);
}
}
private static void saveIDCardListToFile() {
try {
FileWriter fw = new FileWriter("IDCardList.txt");
for (IDCard idCard : idCardList) {
fw.write(idCard.toString() + "\n");
}
fw.close();
System.out.println("保存成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
以上代码实现了一个简单的身份证管理系统,包括添加信息、删除信息、修改信息、查询信息、将录入的信息输出、将录入的信息保存为文件等功能。你可以根据自己的需求进行修改和扩展。
阅读全文