summaryrefslogtreecommitdiff
path: root/graphs/cpp/address_book
diff options
context:
space:
mode:
authorMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:08:27 +0200
committerMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:08:27 +0200
commitc9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch)
tree3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /graphs/cpp/address_book
add: graphs et rushs
Diffstat (limited to 'graphs/cpp/address_book')
-rw-r--r--graphs/cpp/address_book/address_book.cc75
-rw-r--r--graphs/cpp/address_book/address_book.hh25
-rw-r--r--graphs/cpp/address_book/contact_details.cc38
-rw-r--r--graphs/cpp/address_book/contact_details.hh15
-rw-r--r--graphs/cpp/address_book/main.cc42
5 files changed, 195 insertions, 0 deletions
diff --git a/graphs/cpp/address_book/address_book.cc b/graphs/cpp/address_book/address_book.cc
new file mode 100644
index 0000000..92e74cf
--- /dev/null
+++ b/graphs/cpp/address_book/address_book.cc
@@ -0,0 +1,75 @@
+//
+// Created by martial.simon on 2/26/25.
+//
+
+#include "address_book.hh"
+
+#include <iostream>
+#include <stdexcept>
+bool AddressBook::add(const std::string& full_name, const std::string& email,
+ const std::string& number)
+{
+ try
+ {
+ ContactDetails details = ContactDetails::makeDetails(number, email);
+ if (full_name.empty())
+ return false;
+ contact_map_.insert({ full_name, details });
+ return true;
+ }
+ catch (std::invalid_argument& e)
+ {
+ return false;
+ }
+}
+std::vector<ContactDetails> AddressBook::find(const std::string& full_name)
+{
+ std::vector<ContactDetails> res;
+ for (auto detail = contact_map_.find(full_name);
+ detail != contact_map_.end() && detail->first == full_name; detail++)
+ {
+ res.push_back(detail->second);
+ }
+ return res;
+}
+std::size_t AddressBook::count(const std::string& full_name)
+{
+ size_t res = 0;
+ for (auto detail = contact_map_.find(full_name);
+ detail != contact_map_.end(); detail++)
+ {
+ res++;
+ }
+ return res;
+}
+bool AddressBook::remove(const std::string& full_name, std::size_t index)
+{
+ auto range = contact_map_.equal_range(full_name);
+ for (auto contact = range.first; contact != range.second; contact++)
+ {
+ if (contact->first == full_name)
+ {
+ if (index == 0)
+ {
+ contact_map_.erase(contact);
+ return true;
+ }
+ index--;
+ }
+ }
+ return false;
+}
+void AddressBook::remove_all(const std::string& full_name)
+{
+ auto range = contact_map_.equal_range(full_name);
+ contact_map_.erase(range.first, range.second);
+}
+std::ostream& operator<<(std::ostream& os, const AddressBook& b)
+{
+ os << b.contact_map_.size() << " contact(s) in the address book.\n";
+ for (auto& pair : b.contact_map_)
+ {
+ os << "- " << pair.first << ": " << pair.second;
+ }
+ return os;
+} \ No newline at end of file
diff --git a/graphs/cpp/address_book/address_book.hh b/graphs/cpp/address_book/address_book.hh
new file mode 100644
index 0000000..9e59ac9
--- /dev/null
+++ b/graphs/cpp/address_book/address_book.hh
@@ -0,0 +1,25 @@
+//
+// Created by martial.simon on 2/26/25.
+//
+
+#pragma once
+#include <map>
+#include <string>
+#include <vector>
+
+#include "contact_details.hh"
+
+class AddressBook
+{
+public:
+ bool add(const std::string& full_name, const std::string& email,
+ const std::string& number);
+ std::vector<ContactDetails> find(const std::string& full_name);
+ std::size_t count(const std::string& full_name);
+ bool remove(const std::string& full_name, std::size_t index);
+ void remove_all(const std::string& full_name);
+ friend std::ostream& operator<<(std::ostream& os, const AddressBook& b);
+
+private:
+ std::multimap<std::string, ContactDetails> contact_map_;
+};
diff --git a/graphs/cpp/address_book/contact_details.cc b/graphs/cpp/address_book/contact_details.cc
new file mode 100644
index 0000000..3880d14
--- /dev/null
+++ b/graphs/cpp/address_book/contact_details.cc
@@ -0,0 +1,38 @@
+//
+// Created by martial.simon on 2/26/25.
+//
+
+#include "contact_details.hh"
+
+#include <iostream>
+#include <stdexcept>
+ContactDetails ContactDetails::makeDetails(const std::string& telephone_number,
+ const std::string& personal_email)
+{
+ for (auto digit : telephone_number)
+ {
+ if (!isdigit(digit))
+ throw std::invalid_argument{
+ "Phone number must only contain digits."
+ };
+ }
+ size_t i;
+ for (i = 0; i < personal_email.length(); ++i)
+ {
+ if (personal_email[i] == '@')
+ break;
+ }
+ if (i == personal_email.length())
+ throw std::invalid_argument{ "Email must contain at least one '@'." };
+ return ContactDetails{ telephone_number, personal_email };
+}
+std::ostream& operator<<(std::ostream& os, const ContactDetails& dt)
+{
+ os << dt.phone_ << ", " << dt.email_ << "\n";
+ return os;
+}
+ContactDetails::ContactDetails(const std::string& telephone_number,
+ const std::string& personal_email)
+ : phone_{ telephone_number }
+ , email_{ personal_email }
+{} \ No newline at end of file
diff --git a/graphs/cpp/address_book/contact_details.hh b/graphs/cpp/address_book/contact_details.hh
new file mode 100644
index 0000000..57a6438
--- /dev/null
+++ b/graphs/cpp/address_book/contact_details.hh
@@ -0,0 +1,15 @@
+#pragma once
+#include <string>
+
+struct ContactDetails
+{
+ static ContactDetails makeDetails(const std::string& telephone_number,
+ const std::string& personal_email);
+ friend std::ostream& operator<<(std::ostream& os, const ContactDetails& dt);
+
+private:
+ const std::string phone_;
+ const std::string email_;
+ ContactDetails(const std::string& telephone_number,
+ const std::string& personal_email);
+}; \ No newline at end of file
diff --git a/graphs/cpp/address_book/main.cc b/graphs/cpp/address_book/main.cc
new file mode 100644
index 0000000..ffdb324
--- /dev/null
+++ b/graphs/cpp/address_book/main.cc
@@ -0,0 +1,42 @@
+#include <iostream>
+#include <string>
+#include <vector>
+
+#include "address_book.hh"
+
+int main()
+{
+ AddressBook b = AddressBook();
+
+ std::cout << "Adding multiple contacts to the address book.\n";
+
+ b.add("Cyril Berger", "cyril.berger@epita.fr", "33612983402");
+ b.add("Thibault Allancon", "thibault.allancon@epita.fr", "33612983409");
+ b.add("Cyril Berger", "cyril.berger@epita.fr", "33628359602");
+ b.add("Hugo Wahl", "hugo.wahl@epita.fr", "3398560923");
+ b.add("Dominique Michel", "dominique.michel@epita.fr", "3345096792");
+
+ std::cout << "Searching for Maya El Gemayel:\n";
+ auto v = b.find("Maya El Gemayel");
+ for (auto it = v.begin(); it != v.end(); it++)
+ std::cout << *it;
+
+ std::cout << "Searching for Thibault Allancon:\n";
+ v = b.find("Thibault Allancon");
+ for (auto it = v.begin(); it != v.end(); it++)
+ std::cout << *it;
+
+ std::cout << "Searching for Cyril Berger:\n";
+ v = b.find("Cyril Berger");
+ for (auto it = v.begin(); it != v.end(); it++)
+ std::cout << *it;
+
+ std::cout << b;
+
+ std::cout << "Erasing the second Cyril Berger.\n";
+ b.remove("Cyril Berger", 1);
+
+ std::cout << b;
+
+ return 0;
+}