通信录管理系统
题目描述:设计出模拟通信录管理系统,实现对用户的通信录进行管理。功能需求:
(1)设计一个联系人类Person,包含姓名,电话,单位。
(2)设计一个通讯录类AddressBook,按照分类保存联系人,类别有办公和个人。
(3)当输入需要查询的类别时,可以显示该类别的全部联系人信息。
(4) 可以添加,删除,修改联系人信息。可以对重复录入的联系进行检查,合并。
(5)使用外部文件存储通讯录信息。
引自强大而免费的微信小程序:皆我百晓生。
以下是符合上述条件的C++代码:
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std;
class Person {
private:
string name;
string phoneNumber;
string company;
public:
Person() {
name = "";
phoneNumber = "";
company = "";
}
Person(string n, string p, string c) {
name = n;
phoneNumber = p;
company = c;
}
string getName() {
return name;
}
string getPhoneNumber() {
return phoneNumber;
}
string getCompany() {
return company;
}
void setName(string n) {
name = n;
}
void setPhoneNumber(string p) {
phoneNumber = p;
}
void setCompany(string c) {
company = c;
}
};
class AddressBook {
private:
static const int MAX_CONTACTS = 100;
Person contacts[MAX_CONTACTS];
int numContacts;
public:
AddressBook() {
numContacts = 0;
}
void loadData() {
string line;
ifstream file("contacts.txt");
if (file.is_open()) {
while (getline(file, line)) {
string name = line;
getline(file, line);
string phoneNumber = line;
getline(file, line);
string company = line;
Person contact(name, phoneNumber, company);
addContact(contact);
}
file.close();
} else {
cout << "Unable to open file." << endl;
}
}
void saveData() {
ofstream file("contacts.txt");
if (file.is_open()) {
for (int i = 0; i < numContacts; i++) {
file << contacts[i].getName() << endl;
file << contacts[i].getPhoneNumber() << endl;
file << contacts[i].getCompany() << endl;
}
file.close();
} else {
cout << "Unable to open file." << endl;
}
}
void addContact(Person contact) {
bool isAlreadyAdded = false;
for (int i = 0; i < numContacts; i++) {
if (contacts[i].getName() == contact.getName()) {
isAlreadyAdded = true;
break;
}
}
if (!isAlreadyAdded) {
if (numContacts < MAX_CONTACTS) {
contacts[numContacts++] = contact;
} else {
cout << "Address book is full. Contact cannot be added." << endl;
}
} else {
cout << "Contact already exists in the address book." << endl;
}
}
void removeContact(string name) {
bool isRemoved = false;
for (int i = 0; i < numContacts; i++) {
if (contacts[i].getName() == name) {
for (int j = i; j < numContacts - 1; j++) {
contacts[j] = contacts[j + 1];
}
numContacts--;
isRemoved = true;
break;
}
}
if (!isRemoved) {
cout << "Contact not found in the address book." << endl;
}
}
void updateContact(string name, string phoneNumber, string company) {
bool isUpdated = false;
for (int i = 0; i < numContacts; i++) {
if (contacts[i].getName() == name) {
contacts[i].setPhoneNumber(phoneNumber);
contacts[i].setCompany(company);
isUpdated = true;
break;
}
}
if (!isUpdated) {
cout << "Contact not found in the address book." << endl;
}
}
void displayContacts() {
if (numContacts == 0) {
cout << "No contacts found in the address book." << endl;
} else {
for (int i = 0; i < numContacts; i++) {
cout << "Name: " << contacts[i].getName() << endl;
cout << "Phone Number: " << contacts[i].getPhoneNumber() << endl;
cout << "Company: " << contacts[i].getCompany() << endl << endl;
}
}
}
void displayCategoryContacts(string category) {
bool isFound = false;
for (int i = 0; i < numContacts; i++) {
if (contacts[i].getCompany() == category) {
cout << "Name: " << contacts[i].getName() << endl;
cout << "Phone Number: " << contacts[i].getPhoneNumber() << endl;
cout << "Company: " << contacts[i].getCompany() << endl << endl;
isFound = true;
}
}
if (!isFound) {
cout << "No contacts found in the given category." << endl;
}
}
};
int main() {
AddressBook addressBook;
addressBook.loadData();
int choice;
while (true) {
cout << "-------------------------------" << endl;
cout << "Enter the corresponding number to choose a command:" << endl;
cout << "1. Add a contact" << endl;
cout << "2. Remove a contact" << endl;
cout << "3. Update a contact" << endl;
cout << "4. Display all contacts" << endl;
cout << "5. Display contacts by category" << endl;
cout << "6. Exit" << endl;
cout << "-------------------------------" << endl;
cout << "Choice: ";
cin >> choice;
if (choice == 1) {
string name, phoneNumber, company;
cout << "Enter the contact's name: ";
cin.ignore();
getline(cin, name);
cout << "Enter the contact's phone number: ";
getline(cin, phoneNumber);
cout << "Enter the contact's company: ";
getline(cin, company);
Person contact(name, phoneNumber, company);
addressBook.addContact(contact);
addressBook.saveData();
cout << "Contact added successfully." << endl;
} else if (choice == 2) {
string name;
cout << "Enter the contact's name: ";
cin.ignore();
getline(cin, name);
addressBook.removeContact(name);
addressBook.saveData();
cout << "Contact removed successfully." << endl;
} else if (choice == 3) {
string name, phoneNumber, company;
cout << "Enter the contact's name: ";
cin.ignore();
getline(cin, name);
cout << "Enter the contact's new phone number: ";
getline(cin, phoneNumber);
cout << "Enter the contact's new company: ";
getline(cin, company);
addressBook.updateContact(name, phoneNumber, company);
addressBook.saveData();
cout << "Contact updated successfully." << endl;
} else if (choice == 4) {
addressBook.displayContacts();
} else if (choice == 5) {
string category;
cout << "Enter the category: ";
cin.ignore();
getline(cin, category);
addressBook.displayCategoryContacts(category);
} else if (choice == 6) {
break;
} else {
cout << "Invalid choice. Please try again." << endl;
}
}
return 0;
}
该代码实现了题目中要求的通信录管理系统。
注意:该代码中使用了外部文件contacts.txt来存储联系人信息,需要将该文件与源代码放在同一目录下。