Novice village boss -- address book management system

Posted by daz_effect on Tue, 18 Jan 2022 11:16:07 +0100

Address book management system

According to the introduction video of the dark horse, I knocked it by hand. The following is its handout, with some modifications. See the end of the article for the complete code.

1. System requirements

Address book is a tool that can record the information of relatives and friends.

This tutorial mainly uses C + + to implement an address book management system

The functions to be realized in the system are as follows:

  • Add contact: add a new person to the address book. The information includes (name, gender, age, contact number, home address) and records up to 1000 people
  • Show contacts: displays all contact information in the address book
  • Delete contact: delete the specified contact by name
  • Find contact: view the specified contact information by name
  • Modify contact: modify the specified contact according to the name
  • Empty contact: clear all information in the address book
  • Exit address book: exit the currently used address book

2. Menu function

Function Description: the interface for users to select functions

The menu interface effect is shown in the figure below:

Steps:

  • The encapsulated function displays this interface, such as void showMenu()
  • Invoke encapsulated functions in the main function

code:

#include<iostream>
using namespace std;

//Menu interface
void showMenu()
{
	cout << "***************************" << endl;
	cout << "*****  1,Add a Contact   *****" << endl;
	cout << "*****  2,Show contacts  *****" << endl;
	cout << "*****  3,Delete Contact   *****" << endl;
	cout << "*****  4,find contact   *****" << endl;
	cout << "*****  5,Modify contact  *****" << endl;
	cout << "*****  6,Empty contacts  *****" << endl;
	cout << "*****  0,Exit address book  *****" << endl;
	cout << "***************************" << endl;
}

int main() {

	showMenu();

	system("pause");

	return 0;
}

3. Exit function

Function Description: exit the address book system

Idea: according to different choices of users, you can choose switch branch structure to build the whole architecture

When the user selects 0, it will exit. If the user selects other options, it will not exit the program

code:

int main() {

	int select = 0;

	while (true)
	{
		showMenu();

		cin >> select;
		
		switch (select)
		{
		case 1:  //Add a Contact 
			break;
		case 2:  //Show contacts
			break;
		case 3:  //Delete Contact 
			break;
		case 4:  //find contact 
			break;
		case 5:  //Modify contact
			break;
		case 6:  //Empty contacts
			break;
		case 0:  //Exit address book
			cout << "Welcome to use next time" << endl;
			system("pause");
			return 0;
			break;
		default:
			break;
		}
	}

	system("pause");

	return 0;
}

4. Add contact

Function Description:

Realize the function of adding contacts, the maximum number of contacts is 1000, and the contact information includes (name, gender, age, contact phone number and home address)

Implementation steps for adding contacts:

  • Design contact structure
  • Design address book structure
  • Create address book in main function
  • Encapsulate add contact function
  • Test add contact function

4.1 design contact structure

Contact information includes: name, gender, age, contact number and home address

The design is as follows:

#Include < string > / / String header file
//Contact structure
struct Person
{
	string m_Name; //full name
	int m_Sex; //Gender: 1 male and 2 female
	int m_Age; //Age
	string m_Phone; //Telephone
	string m_Addr; //address
};

4.2 design address book structure

During design, an array of contacts with a capacity of 1000 can be maintained in the address book structure, and the number of contacts in the current address book can be recorded

The design is as follows

#define MAX 1000 / / maximum number of people

//Address book structure
struct Addressbooks
{
	struct Person personArray[MAX]; //Contact array saved in address book
	int m_Size; //Number of people in the address book
};

4.3 create address book in main function

After the add contact function is encapsulated, create an address book variable in the main function, which is the address book we need to maintain all the time

mian Add function starting position:

	//Create address book
	Addressbooks abs;
	//Initialize the number of people in the address book
	abs.m_Size = 0;

4.4 encapsulating and adding contact function

Idea: before adding a contact, first judge whether the address book is full. If it is full, it will not be added. If it is not full, add the new contact information to the address book one by one

Add contact Code:

//1. Add contact information
void addPerson(Addressbooks *abs)
{
	//Determine if the phone book is full
	if (abs->m_Size == MAX)
	{
		cout << "The address book is full and cannot be added" << endl;
		return;
	}
	else
	{
		//full name
		string name;
		cout << "Please enter your name:" << endl;
		cin >> name;
		abs->personArray[abs->m_Size].m_Name = name;

		cout << "Please enter gender:" << endl;
		cout << "1 -- male" << endl;
		cout << "2 -- female" << endl;

		//Gender
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->personArray[abs->m_Size].m_Sex = sex;
				break;
			}
			cout << "Input error, please re-enter";
		}

		//Age
		cout << "Please enter age:" << endl;
		int age = 0;
		cin >> age;
		abs->personArray[abs->m_Size].m_Age = age;

		//contact number
		cout << "Please enter the contact number:" << endl;
		string phone = "";
		cin >> phone;
		abs->personArray[abs->m_Size].m_Phone = phone;

		//Home address
		cout << "Please enter your home address:" << endl;
		string address;
		cin >> address;
		abs->personArray[abs->m_Size].m_Addr = address;

		//Number of contacts updated
		abs->m_Size++;

		cout << "Added successfully" << endl;
		system("pause");
		system("cls");
	}
}

4.5 test add contact function

In the selection interface, if the player selects 1, it means to add a contact. We can test this function

In the switch case statement, add the following to case1:

case 1:  //Add a Contact 
	addPerson(&abs);
	break;

5. Show contacts

Function Description: displays the existing contact information in the address book

Display contact implementation steps:

  • Encapsulate display contact function
  • Test display contact function

5.1 encapsulated display contact function

Idea: if there is no person in the current address book, it will prompt that the record is empty and the number of people is greater than 0, and the information in the address book will be displayed

Display contact Code:

//2. Show all contact information
void showPerson(Addressbooks * abs)
{
	if (abs->m_Size == 0)
	{
		cout << "Current record is empty" << endl;
	}
	else
	{
		for (int i = 0; i < abs->m_Size; i++)
		{
			cout << "full name:" << abs->personArray[i].m_Name << "\t";
			cout << "Gender:" << (abs->personArray[i].m_Sex == 1 ? "male" : "female") << "\t";
			cout << "Age:" << abs->personArray[i].m_Age << "\t";
			cout << "Telephone:" << abs->personArray[i].m_Phone << "\t";
			cout << "Address:" << abs->personArray[i].m_Addr << endl;
		}
	}
	
	system("pause");
	system("cls");

}

5.2 test and display contact function

In the switch case statement, add case 2

case 2:  //Show contacts
	showPerson(&abs);
	break;

6. Delete contact

Function Description: delete the specified contact by name

To delete a contact:

  • Package to detect whether the contact exists
  • Encapsulate delete contact function
  • Test delete contact function

6.1 whether the package inspection contact exists

Design idea:

Before deleting a contact, we need to judge whether the contact entered by the user exists. If it exists, it will prompt the user that there is no contact to delete

Therefore, we can package the detection of whether a contact exists into a function. If it exists, it returns the position of the contact in the address book, and if it does not exist, it returns - 1

Check whether the contact code exists:

//Judge whether there is a person to query. If there is, return the index position in the array. If not, return - 1
int isExist(Addressbooks * abs, string name)
{
	for (int i = 0; i < abs->m_Size; i++)
	{
		if (abs->personArray[i].m_Name == name)
		{
			return i;
		}
	}
	return -1;
}

6.2 encapsulating and deleting contact function

Judge whether there is this person in the address book according to the contact entered by the user

Find it and delete it, and prompt that the deletion is successful

No prompt found, no one found.

//3. Delete specified contact information
void deletePerson(Addressbooks * abs)
{
	cout << "Please enter the contact you want to delete" << endl;
	string name;
	cin >> name;

	int ret = isExist(abs, name);
	if (ret != -1)
	{
		for (int i = ret; i < abs->m_Size; i++)
		{
			abs->personArray[i] = abs->personArray[i + 1];
		}
         abs->m_Size--;
		cout << "Delete succeeded" << endl;
	}
	else
	{
		cout << "No one was found" << endl;
	}

	system("pause");
	system("cls");
}

6.3 test the function of deleting contacts

In the switch case statement, add the following to case3:

case 3:  //Delete Contact 
	deletePerson(&abs);
	break;

7. Find contacts

Function Description: view the specified contact information by name

Find contact implementation steps

  • Encapsulate find contact function
  • Test find specified contact

7.1 encapsulating the contact search function

Implementation idea: judge whether the contact specified by the user exists. If there is a display information, it will prompt that there is no such person.

Find contact Code:

//4. Find specified contact information
void findPerson(Addressbooks * abs)
{
	cout << "Please enter the contact you want to find" << endl;
	string name;
	cin >> name;

	int ret = isExist(abs, name);
	if (ret != -1)
	{
		cout << "full name:" << abs->personArray[ret].m_Name << "\t";
		cout << "Gender:" << abs->personArray[ret].m_Sex << "\t";
		cout << "Age:" << abs->personArray[ret].m_Age << "\t";
		cout << "Telephone:" << abs->personArray[ret].m_Phone << "\t";
		cout << "Address:" << abs->personArray[ret].m_Addr << endl;
	}
	else
	{
		cout << "No one was found" << endl;
	}

	system("pause");
	system("cls");

}

7.2 test find designated contact

In the switch case statement, add the following to case4:

case 4:  //find contact 
	findPerson(&abs);
	break;

8. Modify contact

Function Description: re modify the specified contact according to the name

Implementation steps of modifying contact

  • Encapsulating and modifying contact functions
  • Test and modify contact function

8.1 encapsulating and modifying contact functions

Implementation idea: find the contact entered by the user. If the search is successful, modify it, and if the search fails, you will be prompted that there is no such person

Modify contact Code:

//5. Modify the specified contact information
void modifyPerson(Addressbooks * abs)
{
	cout << "Please enter the contact you want to modify" << endl;
	string name;
	cin >> name;

	int ret = isExist(abs, name);
	if (ret != -1)
	{
		//full name
		string name;
		cout << "Please enter your name:" << endl;
		cin >> name;
		abs->personArray[ret].m_Name = name;

		cout << "Please enter gender:" << endl;
		cout << "1 -- male" << endl;
		cout << "2 -- female" << endl;

		//Gender
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->personArray[ret].m_Sex = sex;
				break;
			}
			cout << "Input error, please re-enter";
		}

		//Age
		cout << "Please enter age:" << endl;
		int age = 0;
		cin >> age;
		abs->personArray[ret].m_Age = age;

		//contact number
		cout << "Please enter the contact number:" << endl;
		string phone = "";
		cin >> phone;
		abs->personArray[ret].m_Phone = phone;

		//Home address
		cout << "Please enter your home address:" << endl;
		string address;
		cin >> address;
		abs->personArray[ret].m_Addr = address;

		cout << "Modified successfully" << endl;
	}
	else
	{
		cout << "No one was found" << endl;
	}

	system("pause");
	system("cls");

}

8.2 test and modify contact function

In the switch case statement, add the following to case 5:

case 5:  //Modify contact
	modifyPerson(&abs);
	break;

9. Empty contacts

Function Description: clear all information in the address book

Empty contact implementation steps

  • Encapsulate empty contact function
  • Test empty contacts

9.1 encapsulate empty contact function

Implementation idea: clear all contact information in the address book. Just set the number of contacts recorded in the address book to 0 and clear it logically.

Empty contact Code:

//6. Empty all contacts
void cleanPerson(Addressbooks * abs)
{
	abs->m_Size = 0;
	cout << "The address book has been emptied" << endl;
	system("pause");
	system("cls");
}

9.2 test empty contact

In the switch case statement, add the following to case 6:

case 6:  //Empty contacts
	cleanPerson(&abs);
	break;

So far, congratulations on passing the novice village

Please visit the website to download the complete code: No points are required to download
https://download.csdn.net/download/qq_43993647/76324464

Topics: C++