diff --git a/ex01/Makefile b/ex01/Makefile index 9455d85..783ce99 100644 --- a/ex01/Makefile +++ b/ex01/Makefile @@ -1,27 +1,24 @@ CXX = c++ -CXXFLAGS = -std=c++98 -Wall -Wextra -Werror -g +CPPFLAGS = -std=c++98 -Wall -Wextra -Werror -g SRCDIR = src -INCDIR = include OBJDIR = obj -BINDIR = bin -EXECUTABLE = contacts +NAME = contacts SRCS = $(wildcard $(SRCDIR)/*.cpp) OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS)) -INCS = -I$(INCDIR) -all: $(BINDIR)/$(EXECUTABLE) +all: $(NAME) $(OBJDIR)/%.o: $(SRCDIR)/%.cpp - $(CXX) $(CXXFLAGS) $(INCS) -c $< -o $@ + $(CXX) $(CPPFLAGS) -c $< -o $@ -$(BINDIR)/$(EXECUTABLE): $(OBJS) - $(CXX) $(CXXFLAGS) $^ -o $@ +$(NAME): $(OBJS) + $(CXX) $(CPPFLAGS) $^ -o $@ clean: rm -rf $(OBJDIR)/*.o fclean: clean - rm -fr $(BINDIR)/$(EXECUTABLE) + rm -fr $(NAME) -re: fclean $(BINDIR)/$(EXECUTABLE) +re: fclean all diff --git a/ex01/src/Contact.cpp b/ex01/src/Contact.cpp index eac2a41..7e101da 100644 --- a/ex01/src/Contact.cpp +++ b/ex01/src/Contact.cpp @@ -4,8 +4,8 @@ #include #include #include -#include "../include/Contact.hpp" -#include "../include/utils.hpp" +#include "./Contact.hpp" +#include "./utils.hpp" Contact::Contact(std::string first_name, std::string last_name, std::string nickname, std::string phone_number, std::string darkest_secret) { @@ -16,6 +16,30 @@ Contact::Contact(std::string first_name, std::string last_name, std::string nick this->darkest_secret = std::string(darkest_secret); } +Contact::Contact() +{ + this->darkest_secret = ""; + this->phone_number = ""; + this->nickname = ""; + this->last_name = ""; + this->first_name = ""; +} + +Contact& Contact::operator=(Contact& src) +{ + this->darkest_secret = src.darkest_secret; + this->phone_number = src.phone_number; + this->nickname = src.nickname; + this->last_name = src.last_name; + this->first_name = src.first_name; + return (*this); +} + +Contact::~Contact() +{ + std::cout << "je suis finit: " << this << std::endl; +} + std::string Contact::to_string_partial() const { std::stringstream ss; diff --git a/ex01/src/Contact.hpp b/ex01/src/Contact.hpp new file mode 100644 index 0000000..a48ac02 --- /dev/null +++ b/ex01/src/Contact.hpp @@ -0,0 +1,21 @@ +#include +#include + +class Contact { + private: + std::string first_name; + std::string last_name; + std::string nickname; + std::string phone_number; + std::string darkest_secret; + + public: + Contact(); + Contact(std::string first_name, std::string last_name, std::string nickname, std::string phone_number, std::string darkest_secret); + ~Contact(); + + Contact &operator=(Contact &src); + + std::string to_string_partial() const; + std::string to_string_complete() const; +}; diff --git a/ex01/src/PhoneBook.cpp b/ex01/src/PhoneBook.cpp index 76d04d8..8a618bb 100644 --- a/ex01/src/PhoneBook.cpp +++ b/ex01/src/PhoneBook.cpp @@ -1,4 +1,4 @@ -#include "../include/PhoneBook.hpp" +#include "./PhoneBook.hpp" #include #include #include @@ -8,33 +8,19 @@ PhoneBook::PhoneBook() { + Contact contact; this->len = 0; + this->number = 0; + for (size_t i = 0; i < 8; i++) + this->contacts[i] = contact; } -PhoneBook::~PhoneBook() +void PhoneBook::add_contact(Contact &new_contact) { - for (size_t i = 0; i < this->len; i++) - delete this->contacts[i]; -} - -void PhoneBook::add_contact(Contact *new_contact) -{ - size_t i; - - std::cout << std::endl << this->len << std::endl; + this->contacts[this->number % 8] = new_contact; if (this->len < 8) - { - this->contacts[this->len] = new_contact; this->len++; - } - else - { - delete this->contacts[0]; - for (i = this->len - 1; i > 0; i--) { - this->contacts[i] = this->contacts[i - 1]; - } - this->contacts[0] = new_contact; - } + this->number++; } @@ -56,7 +42,7 @@ void PhoneBook::display_contacts() std::cout << std::setw(10) << i << "|" - << this->contacts[i]->to_string_partial() + << this->contacts[i].to_string_partial() << std::endl; } } @@ -65,5 +51,5 @@ Contact* PhoneBook::search(int index) { if ((size_t) index > this->len || index < 0) return (NULL); - return (this->contacts[index]); + return (&this->contacts[index]); } diff --git a/ex01/src/PhoneBook.hpp b/ex01/src/PhoneBook.hpp new file mode 100644 index 0000000..6201266 --- /dev/null +++ b/ex01/src/PhoneBook.hpp @@ -0,0 +1,17 @@ +#include "Contact.hpp" +#include + +class PhoneBook { + private: + Contact contacts[8]; + size_t number; + + public: + size_t len; + + PhoneBook(); + + void add_contact(Contact &new_contact); + void display_contacts(); + Contact* search(int index); +}; diff --git a/ex01/src/main.cpp b/ex01/src/main.cpp index 6be9bcc..7fd2223 100644 --- a/ex01/src/main.cpp +++ b/ex01/src/main.cpp @@ -6,7 +6,7 @@ #include #include #include -#include "../include/PhoneBook.hpp" +#include "./PhoneBook.hpp" int get_input(std::string &in, std::string out) { @@ -19,7 +19,7 @@ int get_input(std::string &in, std::string out) return (0); } -Contact* read_contact() +int read_contact(Contact& contact) { std::string first_name; std::string last_name; @@ -32,9 +32,10 @@ Contact* read_contact() || get_input(nickname, "nickname: ") || get_input(phone_number, "phone_number: ") || get_input(darkest_secret, "darkest_secret: ")) - return (NULL); - - return (new Contact(first_name, last_name, nickname, phone_number, darkest_secret)); + return (1); + Contact new_contact = Contact(first_name, last_name, nickname, phone_number, darkest_secret); + contact = new_contact; + return (0); } Contact* search(PhoneBook &phone_book) @@ -57,7 +58,8 @@ Contact* search(PhoneBook &phone_book) int main() { std::string command; - Contact* contact; + Contact contact; + Contact *result; PhoneBook phone_book; while (true) @@ -67,9 +69,9 @@ int main() return (0); if (command == "ADD") { - contact = read_contact(); - if (contact == NULL) + if (read_contact(contact)) return (0); + std::cout << std::endl << "dasd" << std::endl; phone_book.add_contact(contact); } else if (command == "SEARCH") @@ -77,10 +79,10 @@ int main() phone_book.display_contacts(); if (phone_book.len > 0) { - contact = search(phone_book); - if (contact == NULL) + result = search(phone_book); + if (result == NULL) return (0); - std::cout << contact->to_string_complete() << std::endl; + std::cout << result->to_string_complete() << std::endl; } } else if (command == "EXIT") diff --git a/ex01/src/utils.hpp b/ex01/src/utils.hpp new file mode 100644 index 0000000..aabb5da --- /dev/null +++ b/ex01/src/utils.hpp @@ -0,0 +1,3 @@ +#include + +std::string truncated(const std::string str);