C++20 基于文本文件的类对象增删查改系统
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <ranges>
#include <string>
#include <sstream>
#include <filesystem>
namespace fs = std::filesystem;
class Person {
public:
Person() = default;
Person(int id, const std::string& name, int age, const std::string& email)
: id(id), name(name), age(age), email(email) {
}
std::string serialize() const {
std::ostringstream oss;
oss << id << "," << name << "," << age << "," << email;
return oss.str();
}
static Person deserialize(const std::string& data) {
std::istringstream iss(data);
std::string token;
std::vector<std::string> tokens;
while (std::getline(iss, token, ',')) {
tokens.push_back(token);
}
if (tokens.size() != 4) {
throw std::runtime_error("Invalid data format");
}
int id = std::stoi(tokens[0]);
int age = std::stoi(tokens[2]);
return Person(id, tokens[1], age, tokens[3]);
}
friend std::ostream& operator<<(std::ostream& os, const Person& p) {
os << "ID: " << p.id << ", Name: " << p.name
<< ", Age: " << p.age << ", Email: " << p.email;
return os;
}
int getId() const { return id; }
void setName(const std::string& newName) { name = newName; }
void setAge(int newAge) { age = newAge; }
void setEmail(const std::string& newEmail) { email = newEmail; }
private:
int id;
std::string name;
int age;
std::string email;
};
class DataManager {
public:
explicit DataManager(const std::string& filename) : filename(filename) {
loadFromFile();
}
~DataManager() {
saveToFile();
}
void addPerson(const Person& person) {
persons.push_back(person);
saveToFile();
}
bool deletePerson(int id) {
auto it = std::ranges::find_if(persons, [id](const Person& p) {
return p.getId() == id;
});
if (it != persons.end()) {
persons.erase(it);
saveToFile();
return true;
}
return false;
}
Person* findPerson(int id) {
auto it = std::ranges::find_if(persons, [id](const Person& p) {
return p.getId() == id;
});
if (it != persons.end()) {
return &(*it);
}
return nullptr;
}
bool updatePerson(int id, const std::string& name, int age, const std::string& email) {
Person* person = findPerson(id);
if (person) {
person->setName(name);
person->setAge(age);
person->setEmail(email);
saveToFile();
return true;
}
return false;
}
void displayAll() const {
if (persons.empty()) {
std::cout << "No persons in the database.\n";
return;
}
for (const auto& person : persons) {
std::cout << person << '\n';
}
}
const std::vector<Person>& getAllPersons() const {
return persons;
}
private:
std::vector<Person> persons;
std::string filename;
void loadFromFile() {
if (!fs::exists(filename)) {
return;
}
std::ifstream file(filename);
if (!file.is_open()) {
throw std::runtime_error("Cannot open file: " + filename);
}
std::string line;
while (std::getline(file, line)) {
if (!line.empty()) {
try {
persons.push_back(Person::deserialize(line));
}
catch (const std::exception& e) {
std::cerr << "Error parsing line: " << line << " - " << e.what() << '\n';
}
}
}
file.close();
}
void saveToFile() {
std::ofstream file(filename);
if (!file.is_open()) {
throw std::runtime_error("Cannot open file: " + filename);
}
for (const auto& person : persons) {
file << person.serialize() << '\n';
}
file.close();
}
};
void displayMenu() {
std::cout << "\n=== Person Management System ===\n";
std::cout << "1. Add Person\n";
std::cout << "2. Delete Person\n";
std::cout << "3. Find Person\n";
std::cout << "4. Update Person\n";
std::cout << "5. Display All Persons\n";
std::cout << "6. Exit\n";
std::cout << "Choose an option: ";
}
int main() {
DataManager dm("persons.txt");
int choice;
do {
displayMenu();
std::cin >> choice;
std::cin.ignore();
switch (choice) {
case 1: {
int id, age;
std::string name, email;
std::cout << "Enter ID: ";
std::cin >> id;
std::cin.ignore();
std::cout << "Enter Name: ";
std::getline(std::cin, name);
std::cout << "Enter Age: ";
std::cin >> age;
std::cin.ignore();
std::cout << "Enter Email: ";
std::getline(std::cin, email);
dm.addPerson(Person(id, name, age, email));
std::cout << "Person added successfully.\n";
break;
}
case 2: {
int id;
std::cout << "Enter ID to delete: ";
std::cin >> id;
if (dm.deletePerson(id)) {
std::cout << "Person deleted successfully.\n";
}
else {
std::cout << "Person not found.\n";
}
break;
}
case 3: {
int id;
std::cout << "Enter ID to find: ";
std::cin >> id;
Person* person = dm.findPerson(id);
if (person) {
std::cout << "Person found: " << *person << '\n';
}
else {
std::cout << "Person not found.\n";
}
break;
}
case 4: {
int id, age;
std::string name, email;
std::cout << "Enter ID to update: ";
std::cin >> id;
std::cin.ignore();
std::cout << "Enter new Name: ";
std::getline(std::cin, name);
std::cout << "Enter new Age: ";
std::cin >> age;
std::cin.ignore();
std::cout << "Enter new Email: ";
std::getline(std::cin, email);
if (dm.updatePerson(id, name, age, email)) {
std::cout << "Person updated successfully.\n";
}
else {
std::cout << "Person not found.\n";
}
break;
}
case 5:
dm.displayAll();
break;
case 6:
std::cout << "Exiting...\n";
break;
default:
std::cout << "Invalid option. Please try again.\n";
}
} while (choice != 6);
return 0;
}