Skip to content

Instantly share code, notes, and snippets.

@DreamVB
Created July 6, 2021 20:09
Show Gist options
  • Select an option

  • Save DreamVB/c00d408c6bc4666848baafb39ea04b60 to your computer and use it in GitHub Desktop.

Select an option

Save DreamVB/c00d408c6bc4666848baafb39ea04b60 to your computer and use it in GitHub Desktop.
Address Book Class
//Address Book Demo Class
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct address
{
unsigned int id;
std::string firstname;
std::string lastname;
std::string phone;
};
class AddressBook{
private:
std::vector<address> phonebook;
public:
enum TSortOption{
ByID = 0,
ByFirstName = 1,
ByLastname = 2
};
void sortByFirstName(TSortOption sort_type){
address temp;
unsigned len = phonebook.size();
//Bubble sort
for (unsigned int i = 0; i < len; i++){
for (unsigned j = i + 1; j < len; j++){
//sort by firstname
if (sort_type == ByFirstName){
if (phonebook[i].firstname[0] > phonebook[j].firstname[0]){
temp = phonebook[j];
phonebook[j] = phonebook[i];
phonebook[i] = temp;
}
}
//sort phone by lastname
if (sort_type == ByLastname){
if (phonebook[i].lastname[0] > phonebook[j].lastname[0]){
temp = phonebook[j];
phonebook[j] = phonebook[i];
phonebook[i] = temp;
}
}
//sort phone by id
if (sort_type == ByID){
if (phonebook[i].id > phonebook[i].id){
temp = phonebook[j];
phonebook[j] = phonebook[i];
phonebook[i] = temp;
}
}
}
}
}
void RemoveByID(unsigned int id){
//Remove phone book element by id
for (auto it = phonebook.begin(); it != phonebook.end(); it++){
if (it->id == id){
it = phonebook.erase(it);
break;
}
}
}
address at(unsigned id){
//Return a phone book info by it
if (id >= 0 && id <= phonebook.size()){
return phonebook[id];
}
throw std::runtime_error("Record Not Found");
}
void RemoveByFirstName(std::string s){
//Remove phone book element by Firstname
for (auto it = phonebook.begin(); it != phonebook.end(); it++){
if (it->firstname == s){
it = phonebook.erase(it);
break;
}
}
}
void edit(unsigned int id,
std::string fname, std::string lname, std::string phone){
//Edit a phone book element by id
for (auto it = phonebook.begin(); it != phonebook.end(); it++){
if (it->id == id){
it->firstname = fname;
it->lastname = lname;
it->phone = phone;
break;
}
}
}
address findById(unsigned int id){
address tmp;
//Find phone book by id
for (auto it = phonebook.begin(); it != phonebook.end(); it++){
if (it->id == id){
return *it;
break;
}
}
return tmp;
}
void add(unsigned int id, std::string fname,
std::string lname, std::string phone){
//Add a new phone book details
address details;
details.id = id;
details.firstname = fname;
details.lastname = lname;
details.phone = phone;
//Add new phone book details to phonebook vector
phonebook.push_back(details);
}
void print_book(void){
//Print all the contents of the phone book
std::cout << "-------------------------------------------" << std::endl;
for (auto addr : phonebook){
std::cout << "ID : " << addr.id << std::endl;
std::cout << "First Name : " << addr.firstname << std::endl;
std::cout << "Last Name : " << addr.lastname << std::endl;
std::cout << "Phone : " << addr.phone << std::endl;
std::cout << "-------------------------------------------" << std::endl;
}
}
unsigned int records(){
//Return the number of records in the phone book
return phonebook.size();
}
void clear(void){
//Clear the phone book
phonebook.clear();
}
};
int main(){
AddressBook people;
//Tittle
std::cout << ":: Phone Book ::" << std::endl;
//Add phone book items.
people.add(1, "Jack", "Brown", "25741 854229");
people.add(2, "Pete", "Jones", "36587 526884");
people.add(3, "John", "Jackson", "35741 965874");
people.add(6, "Adam", "Jones", "2058 987425");
people.print_book();
//Edit Jacks first name and phone
people.edit(1, "David", "Brown", "01352 815884");
//Delete two records.
people.RemoveByFirstName("Pete");
people.RemoveByFirstName("John");
std::cout << "Edit and delete by first name" << std::endl;
people.print_book();
std::cout << "Add two more records and sort" << std::endl;
//Add records
people.add(100, "Jay", "Tommas", "0800 658589");
people.add(101, "Clair", "Jackson", "98747 254789");
people.sortByFirstName(people.ByFirstName);
//Display
people.print_book();
std::cout << "Find and print record by id" << std::endl;
address f;
f = people.findById(6);
if (f.firstname != ""){
std::cout << "Name: " << f.firstname << " Phone: " << f.phone << std::endl;
}
people.clear();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment