diff --git a/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..9455d85 --- /dev/null +++ b/ex01/Makefile @@ -0,0 +1,27 @@ +CXX = c++ +CXXFLAGS = -std=c++98 -Wall -Wextra -Werror -g +SRCDIR = src +INCDIR = include +OBJDIR = obj +BINDIR = bin +EXECUTABLE = contacts + +SRCS = $(wildcard $(SRCDIR)/*.cpp) +OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS)) +INCS = -I$(INCDIR) + +all: $(BINDIR)/$(EXECUTABLE) + +$(OBJDIR)/%.o: $(SRCDIR)/%.cpp + $(CXX) $(CXXFLAGS) $(INCS) -c $< -o $@ + +$(BINDIR)/$(EXECUTABLE): $(OBJS) + $(CXX) $(CXXFLAGS) $^ -o $@ + +clean: + rm -rf $(OBJDIR)/*.o + +fclean: clean + rm -fr $(BINDIR)/$(EXECUTABLE) + +re: fclean $(BINDIR)/$(EXECUTABLE) diff --git a/ex01/include/Contact.hpp b/ex01/include/Contact.hpp new file mode 100644 index 0000000..df3392a --- /dev/null +++ b/ex01/include/Contact.hpp @@ -0,0 +1,17 @@ +#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(std::string first_name, std::string last_name, std::string nickname, std::string phone_number, std::string darkest_secret); + + std::string to_string_partial() const; + std::string to_string_complete() const; +}; diff --git a/ex01/include/PhoneBook.hpp b/ex01/include/PhoneBook.hpp new file mode 100644 index 0000000..6782f01 --- /dev/null +++ b/ex01/include/PhoneBook.hpp @@ -0,0 +1,17 @@ +#include "Contact.hpp" +#include + +class PhoneBook { + private: + Contact *contacts[8]; + + public: + size_t len; + + PhoneBook(); + ~PhoneBook(); + + void add_contact(Contact *new_contact); + void display_contacts(); + Contact* search(int index); +}; diff --git a/ex01/include/utils.hpp b/ex01/include/utils.hpp new file mode 100644 index 0000000..aabb5da --- /dev/null +++ b/ex01/include/utils.hpp @@ -0,0 +1,3 @@ +#include + +std::string truncated(const std::string str); diff --git a/ex01/src/Contact.cpp b/ex01/src/Contact.cpp new file mode 100644 index 0000000..eac2a41 --- /dev/null +++ b/ex01/src/Contact.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include +#include +#include "../include/Contact.hpp" +#include "../include/utils.hpp" + +Contact::Contact(std::string first_name, std::string last_name, std::string nickname, std::string phone_number, std::string darkest_secret) +{ + this->first_name = std::string(first_name); + this->last_name = std::string(last_name); + this->nickname = std::string(nickname); + this->phone_number = std::string(phone_number); + this->darkest_secret = std::string(darkest_secret); +} + +std::string Contact::to_string_partial() const +{ + std::stringstream ss; + std::string short_first_name; + std::string short_last_name; + std::string short_nickname; + std::string short_phone_number; + std::string short_darkest_secret; + + short_first_name = truncated(this->first_name); + short_last_name = truncated(this->last_name); + short_nickname = truncated(this->nickname); + short_phone_number = truncated(this->phone_number); + short_darkest_secret = truncated(this->darkest_secret); + + ss << std::setw(10) << short_first_name << "|" + << std::setw(10) << short_last_name << "|" + << std::setw(10) << short_nickname << "|" + << std::setw(10) << short_phone_number << "|" + << std::setw(10) << short_darkest_secret; + + return(ss.str()); +} + +std::string Contact::to_string_complete() const +{ + std::stringstream ss; + + ss << std::setw(10) << this->first_name << "|" + << std::setw(10) << this->last_name << "|" + << std::setw(10) << this->nickname << "|" + << std::setw(10) << this->phone_number << "|" + << std::setw(10) << this->darkest_secret << "|"; + + return(ss.str()); +} diff --git a/ex01/src/PhoneBook.cpp b/ex01/src/PhoneBook.cpp new file mode 100644 index 0000000..838b38c --- /dev/null +++ b/ex01/src/PhoneBook.cpp @@ -0,0 +1,66 @@ +#include "../include/PhoneBook.hpp" +#include +#include +#include +#include +#include + +PhoneBook::PhoneBook() +{ + this->len = 0; +} + +PhoneBook::~PhoneBook() +{ + for (size_t i = 0; i < this->len; i++) + delete this->contacts[i]; +} + +void PhoneBook::add_contact(Contact *new_contact) +{ + size_t i; + + if (this->len < 8) + { + this->contacts[this->len] = new_contact; + this->len++; + } + else + { + for (i = this->len; i != 0; i--) { + this->contacts[i] = this->contacts[i - 1]; + } + this->contacts[0] = new_contact; + } +} + + +void PhoneBook::display_contacts() +{ + size_t i; + + std::cout + << " index|" + << "first name|" + << "last name |" + << "nickname |" + << "phone num.|" + << "darkest s." + << std::endl; + + for (i = 0; i < this->len; i++) + { + std::cout + << std::setw(10) << i + << "|" + << this->contacts[i]->to_string_partial() + << std::endl; + } +} + +Contact* PhoneBook::search(int index) +{ + if ((size_t) index > this->len || index < 0) + return (NULL); + return (this->contacts[index]); +} diff --git a/ex01/src/main.cpp b/ex01/src/main.cpp new file mode 100644 index 0000000..9072da9 --- /dev/null +++ b/ex01/src/main.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include +#include +#include +#include "../include/PhoneBook.hpp" + +int get_input(std::string &in, std::string out) +{ + while (in.size() == 0) + { + std::cout << out; + if (!std::getline(std::cin, in)) + return (1); + } + return (0); +} + +Contact* read_contact() +{ + std::string first_name; + std::string last_name; + std::string nickname; + std::string phone_number; + std::string darkest_secret; + + if (get_input(first_name, "first_name: ") + || get_input(last_name, "last_name: ") + || 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)); + +} + +Contact* search(PhoneBook phone_book) +{ + int index; + std::string index_str; + Contact* contact; + + contact = NULL; + while (contact == NULL) + { + if (get_input(index_str, "index: ")) + return (NULL); + index = std::atoi(index_str.c_str()); + contact = phone_book.search(index); + } + return (contact); +} + +int main() +{ + std::string command; + Contact* contact; + PhoneBook phone_book; + + phone_book = PhoneBook(); + while (true) + { + std::cout << "command: "; + if (!std::getline(std::cin, command)) + return (0); + if (command == "ADD") + { + contact = read_contact(); + if (contact == NULL) + return (0); + phone_book.add_contact(contact); + } + else if (command == "SEARCH") + { + phone_book.display_contacts(); + if (phone_book.len > 0) + { + contact = search(phone_book); + if (contact == NULL) + return (0); + std::cout << contact->to_string_complete() << std::endl; + } + } + else if (command == "EXIT") + { + return (0); + } + else { + std::cerr << "Command not found" << std::endl; + } + } + return (0); +} diff --git a/ex01/src/utils.cpp b/ex01/src/utils.cpp new file mode 100644 index 0000000..9117ab3 --- /dev/null +++ b/ex01/src/utils.cpp @@ -0,0 +1,11 @@ +#include + +std::string truncated(const std::string str) +{ + std::string out; + + out = str.substr(0, 10); + if (str.length() > 10) + (out)[9] = '.'; + return (out); +}