diff --git a/d00/ex01/Contact.class.cpp b/d00/ex01/Contact.class.cpp index c1ffbe1..d23bcec 100644 --- a/d00/ex01/Contact.class.cpp +++ b/d00/ex01/Contact.class.cpp @@ -1,11 +1,11 @@ #include "Contact.class.hpp" Contact::Contact( void ) { - this->add_first("NOT DEFINED"); - this->add_last("NOT DEFINED"); - this->add_nick("NOT DEFINED"); - this->add_num("NOT DEFINED"); - this->add_secret("NOT DEFINED"); + this->add_first(""); + this->add_last(""); + this->add_nick(""); + this->add_num(""); + this->add_secret(""); return; } diff --git a/d00/ex01/PhoneBook.class.cpp b/d00/ex01/PhoneBook.class.cpp index 82bbba2..050e13b 100644 --- a/d00/ex01/PhoneBook.class.cpp +++ b/d00/ex01/PhoneBook.class.cpp @@ -2,6 +2,8 @@ #include "Contact.class.hpp" PhoneBook::PhoneBook( void ) { + + this->contact_count = 0; return; } @@ -9,18 +11,124 @@ PhoneBook::~PhoneBook( void ) { return; } - /* - Contact new_contact; +void PhoneBook::add_contact() { - new_contact.add_first(name); - new_contact.add_last("lamy"); - new_contact.add_nick("hugonosaure"); - new_contact.add_num("0123456789"); - new_contact.add_secret("je suis un dino"); + std::string str; + int it; - std::cout << "FIRST NAME : " << new_contact.get_first() << std::endl; - std::cout << "LAST NAME : " << new_contact.get_last() << std::endl; - std::cout << "NICKNAME : " << new_contact.get_nick() << std::endl; - std::cout << "NUMBER : " << new_contact.get_num() << std::endl; - std::cout << "SECRET : " << new_contact.get_secret() << std::endl; - */ + this->contact_count++; + it = this->contact_count; + if (it > 8) + it = 1; + it--; + + std::cout << B_CYAN "enter a first name pliz :" << std::endl; + while (contact[it].get_first().length() == 0) + { + std::cout << B_BLUE "-> " RESET; + std::getline(std::cin, str); + this->contact[it].add_first(str); + } + + std::cout << B_CYAN "enter a last name pliz :" << std::endl; + while (contact[it].get_last().length() == 0) + { + std::cout << B_BLUE "-> " RESET; + std::getline(std::cin, str); + this->contact[it].add_last(str); + } + + std::cout << B_CYAN "enter a nickname pliz :" << std::endl; + while (contact[it].get_nick().length() == 0) + { + std::cout << B_BLUE "-> " RESET; + std::getline(std::cin, str); + this->contact[it].add_nick(str); + } + + std::cout << B_CYAN "enter a number pliz :" << std::endl; + while (contact[it].get_num().length() == 0) + { + std::cout << B_BLUE "-> " RESET; + std::getline(std::cin, str); + this->contact[it].add_num(str); + } + + std::cout << B_CYAN "enter a secret of this contact pliz :" << std::endl; + while (contact[it].get_secret().length() == 0) + { + std::cout << B_BLUE "-> " RESET; + std::getline(std::cin, str); + this->contact[it].add_secret(str); + } +} + +std::string truncate(std::string str, size_t len) { + + if (str.length() > len) + return str.substr(0, len - 1) + "."; + return str; + +} + +void PhoneBook::print_phonebook() { + + std::cout << std::endl; + int it; + + it = this->contact_count; + if (it > 8) + it = 8; + it--; + + std::cout << " ------------------------------------------- " << std::endl; + std::cout << "|" << std::setw(10) << "index"; + std::cout << "|" << std::setw(10) << "first name"; + std::cout << "|" << std::setw(10) << "last name"; + std::cout << "|" << std::setw(10) << "nickname"; + std::cout << "|" << std::endl; + std::cout << " ---------- ---------- ---------- ---------- " << std::endl; + for (int i = 0; i <= it; i++) + { + std::cout << "|" << std::setw(10) << i; + std::cout << "|" << std::setw(10) << truncate(contact[i].get_first(), 10); + std::cout << "|" << std::setw(10) << truncate(contact[i].get_last(), 10); + std::cout << "|" << std::setw(10) << truncate(contact[i].get_nick(), 10); + std::cout << "|" << std::endl; + } + std::cout << " ------------------------------------------- " << std::endl; + std::cout << std::endl; +} + +void PhoneBook::print_contact( int id ) { + + std::cout << std::endl; + std::cout << B_CYAN "FIRST NAME : " << RESET << this->contact[id].get_first() << std::endl; + std::cout << B_CYAN " LAST NAME : " << RESET << this->contact[id].get_last() << std::endl; + std::cout << B_CYAN " NICKNAME : " << RESET << this->contact[id].get_nick() << std::endl; + std::cout << B_CYAN " NUMBER : " << RESET << this->contact[id].get_num() << std::endl; + std::cout << B_CYAN " SECRET : " << RESET << this->contact[id].get_secret() << std::endl; + +} + +void PhoneBook::search_contact() { + + std::string str; + std::stringstream convert; + int id; + + this->print_phonebook(); + id = -1; + std::cout << std::endl << B_CYAN "choose an index :" << std::endl; + while (id < 0 || id > this->contact_count - 1 || id > 7) + { + std::cout << B_BLUE "-> " RESET; + std::getline(std::cin, str); + convert << str; + convert >> id; + if (id < 0 || id > this->contact_count - 1 || id > 7) + std::cout << B_RED "sorry, not a valid index" RESET << std::endl; + } + this->print_contact(id); + +} diff --git a/d00/ex01/PhoneBook.class.hpp b/d00/ex01/PhoneBook.class.hpp index 03b7117..7235b0b 100644 --- a/d00/ex01/PhoneBook.class.hpp +++ b/d00/ex01/PhoneBook.class.hpp @@ -2,13 +2,37 @@ # define PHONEBOOK_CLASS_HPP #include "Contact.class.hpp" +#include "color.h" + #include +#include +#include class PhoneBook { + public: + PhoneBook(); ~PhoneBook(); - Contact contact; + + void add_contact(); + void search_contact(); + void print_phonebook(); + void print_contact( int id); + + private: + + Contact contact[8]; + int contact_count; + }; #endif + +/* + * class PhoneBook : + * add_contact + * print_phonebook + * search_by_index + * exit + */ diff --git a/d00/ex01/builds/Contact.class.o b/d00/ex01/builds/Contact.class.o index ad9941f..4126362 100644 Binary files a/d00/ex01/builds/Contact.class.o and b/d00/ex01/builds/Contact.class.o differ diff --git a/d00/ex01/builds/PhoneBook.class.o b/d00/ex01/builds/PhoneBook.class.o deleted file mode 100644 index b7bc3a2..0000000 Binary files a/d00/ex01/builds/PhoneBook.class.o and /dev/null differ diff --git a/d00/ex01/builds/main.o b/d00/ex01/builds/main.o index e2dd9a3..c964431 100644 Binary files a/d00/ex01/builds/main.o and b/d00/ex01/builds/main.o differ diff --git a/d00/ex01/color.h b/d00/ex01/color.h new file mode 100644 index 0000000..f40596b --- /dev/null +++ b/d00/ex01/color.h @@ -0,0 +1,25 @@ +#ifndef COLOR_H +# define COLOR_H + +# define GRAY "\e[0;30m" +# define RED "\e[0;31m" +# define GREEN "\e[0;32m" +# define YELLOW "\e[0;33m" +# define BLUE "\e[0;34m" +# define PURPLE "\e[0;35m" +# define CYAN "\e[0;36m" +# define WHITE "\e[0;37m" + +# define B_GRAY "\e[1;30m" +# define B_RED "\e[1;31m" +# define B_GREEN "\e[1;32m" +# define B_YELLOW "\e[1;33m" +# define B_BLUE "\e[1;34m" +# define B_PURPLE "\e[1;35m" +# define B_CYAN "\e[1;36m" +# define B_WHITE "\e[1;37m" + +# define RESET "\e[0m" + +#endif + diff --git a/d00/ex01/main.cpp b/d00/ex01/main.cpp index d9013eb..0726c04 100644 --- a/d00/ex01/main.cpp +++ b/d00/ex01/main.cpp @@ -1,4 +1,5 @@ #include "PhoneBook.class.hpp" +#include "color.h" # include # include @@ -6,22 +7,31 @@ int main() { std::string cmd; + PhoneBook YellowPage; while (1) { + std::cout << std::endl; + std::cout << B_YELLOW "enter a command: ADD / SEARCH / EXIT" << std::endl; + std::cout << std::endl << B_GREEN; std::getline(std::cin, cmd); + std::cout << RESET << std::endl; if (cmd.compare("ADD") == 0) - std::cout << "it works !" << std::endl; + YellowPage.add_contact(); + else if (cmd.compare("SEARCH") == 0) + YellowPage.search_contact(); + else if (cmd.compare("EXIT") == 0) + break; } - return 0; } -/* - * class PhoneBook : - * add_contact - * print_phonebook - * search_by_index - * exit - */ +// B_GRAY "\e[1;30m" +// B_RED "\e[1;31m" +// B_GREEN "\e[1;32m" +// B_YELLOW "\e[1;33m" +// B_BLUE "\e[1;34m" +// B_PURPLE "\e[1;35m" +// B_CYAN "\e[1;36m" +// B_WHITE "\e[1;37m" diff --git a/d00/ex01/phonebook b/d00/ex01/phonebook index d87fd62..4ccd5b1 100755 Binary files a/d00/ex01/phonebook and b/d00/ex01/phonebook differ