diff --git a/.gitignore b/.gitignore index f1ef84a..c153e08 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ compile_commands.json .cache .direnv flake.lock +phonebook diff --git a/ex01/Makefile b/ex01/Makefile index 0d89583..46dd0c0 100644 --- a/ex01/Makefile +++ b/ex01/Makefile @@ -6,7 +6,7 @@ # By: adjoly +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/25 16:09:27 by adjoly #+# #+# # -# Updated: 2024/10/30 17:08:54 by adjoly ### ########.fr # +# Updated: 2024/11/03 16:35:01 by adjoly ### ########.fr # # # # **************************************************************************** # @@ -21,7 +21,9 @@ SRCSDIR = src/ INCLUDES = includes/ SRCS = PhoneBook.cpp \ - Contact.cpp + Contact.cpp \ + commands/add.cpp \ + commands/search.cpp OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o)) diff --git a/ex01/includes/PhoneBook.hpp b/ex01/includes/PhoneBook.hpp index 5e0f68a..2443712 100644 --- a/ex01/includes/PhoneBook.hpp +++ b/ex01/includes/PhoneBook.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/26 00:41:10 by adjoly #+# #+# */ -/* Updated: 2024/10/31 12:37:10 by adjoly ### ########.fr */ +/* Updated: 2024/11/03 18:19:21 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,20 @@ #include "Contact.hpp" +#define uint8_t unsigned char + class PhoneBook { private: Contact _contact[8]; - public: + uint8_t _index; + void _add(void); + void _search(void); + +/* Search internal function */ + void __printContacts(void); + std::string __formatstr(std::string str); + void __printContact(uint8_t index); + public: + PhoneBook(void); }; diff --git a/ex01/src/Contact.cpp b/ex01/src/Contact.cpp index f3b2ff7..7e7a74c 100644 --- a/ex01/src/Contact.cpp +++ b/ex01/src/Contact.cpp @@ -6,59 +6,49 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/30 17:05:12 by adjoly #+# #+# */ -/* Updated: 2024/10/31 13:33:56 by adjoly ### ########.fr */ +/* Updated: 2024/11/03 16:52:59 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "Contact.hpp" #include -std::string Contact::getName() -{ +std::string Contact::getName() { return (Contact::_name); } -std::string Contact::getLastName() -{ +std::string Contact::getLastName() { return (Contact::_lastName); } -std::string Contact::getNickname() -{ +std::string Contact::getNickname() { return (Contact::_nickname); } -std::string Contact::getPhoneNbr() -{ +std::string Contact::getPhoneNbr() { return (Contact::_phoneNbr); } -std::string Contact::getSecret() -{ +std::string Contact::getSecret() { return (Contact::_darkestSecret); } -void Contact::setName(std::string newName) -{ +void Contact::setName(std::string newName) { _name = newName; } -void Contact::setLastName(std::string newLastName) -{ +void Contact::setLastName(std::string newLastName) { _lastName = newLastName; } -void Contact::setNickname(std::string newNickname) -{ +void Contact::setNickname(std::string newNickname) { _nickname = newNickname; } -void Contact::setPhoneNbr(std::string newPhoneNbr) -{ +void Contact::setPhoneNbr(std::string newPhoneNbr) { _phoneNbr = newPhoneNbr; } -void Contact::setSecret(std::string newSecret) -{ +void Contact::setSecret(std::string newSecret) { _darkestSecret = newSecret; } diff --git a/ex01/src/PhoneBook.cpp b/ex01/src/PhoneBook.cpp index af5d9c2..ecd93ef 100644 --- a/ex01/src/PhoneBook.cpp +++ b/ex01/src/PhoneBook.cpp @@ -6,13 +6,38 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/26 00:20:32 by adjoly #+# #+# */ -/* Updated: 2024/10/31 13:27:11 by adjoly ### ########.fr */ +/* Updated: 2024/11/03 18:50:17 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include +#include +#include -int main(void) -{ - +PhoneBook::PhoneBook(void) { + std::string input; + + this->_index = 0; + while (42) { + std::cout << "Enter a command (ADD, SEARCH, EXIT) : "; + input.clear(); + if (!std::getline(std::cin, input)) + return ; + if (!input.compare("ADD")) + this->_add(); + else if (!input.compare("SEARCH")) + this->_search(); + else if (!input.compare("EXIT")) + return ; + else if (input.empty()) + continue ; + else + std::cout << "Not a valid command" << std::endl; + } +} + +int main(void) { + PhoneBook PhoneBook; + + return (0); } diff --git a/ex01/src/commands/add.cpp b/ex01/src/commands/add.cpp new file mode 100644 index 0000000..de297a2 --- /dev/null +++ b/ex01/src/commands/add.cpp @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* add.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/31 13:51:00 by adjoly #+# #+# */ +/* Updated: 2024/11/03 18:06:18 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "PhoneBook.hpp" +#include +#include + +std::string getInput(std::string info) { + std::string input; + + std::cout << "Enter the " << info << " of the new contact : "; + std::getline(std::cin, input); + return (input); +} + +void PhoneBook::_add(void) { + + std::string input; + + input = getInput("name"); + if (input.empty()) + return ; + this->_contact[this->_index].setName(input); + input = getInput("last name"); + if (input.empty()) + return ; + this->_contact[this->_index].setLastName(input); + input = getInput("nickname"); + if (input.empty()) + return ; + this->_contact[this->_index].setNickname(input); + input = getInput("phone number"); + if (input.empty()) + return ; + this->_contact[this->_index].setPhoneNbr(input); + input = getInput("darkest secret"); + if (input.empty()) + return ; + this->_contact[this->_index].setSecret(input); + this->_index++; + if (this->_index >= 8) + this->_index = 0; +} diff --git a/ex01/src/commands/search.cpp b/ex01/src/commands/search.cpp new file mode 100644 index 0000000..81f8bd4 --- /dev/null +++ b/ex01/src/commands/search.cpp @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* search.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/03 16:51:39 by adjoly #+# #+# */ +/* Updated: 2024/11/03 16:57:33 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************* */ + +#include "PhoneBook.hpp" +#include +#include +#include + +std::string PhoneBook::__formatstr(std::string str) { + if (str.size() > 10) { + str.resize(9); + str.append("."); + } + else { + while (str.size() < 10) + str.append(" "); + } + return (str); +} + +void PhoneBook::__printContacts(void) { + uint8_t i; + + i = 0; + std::cout << "|----------|----------|----------|----------|" << std::endl; + while (i < 8) + { + std::cout << "| " << (int)i + 1 << " "; + std::cout << "|" << __formatstr(this->_contact[i].getName()); + std::cout << "|" << __formatstr(this->_contact[i].getLastName()); + std::cout << "|" << __formatstr(this->_contact[i].getNickname()); + std::cout << "|" << std::endl; + i++; + } + std::cout << "|----------|----------|----------|----------|" << std::endl; +} + +void PhoneBook::__printContact(uint8_t index) { + std::cout << "Name : " << this->_contact[index].getName() << std::endl; + std::cout << "Last name : " << this->_contact[index].getLastName() << std::endl; + std::cout << "Nickname : " << this->_contact[index].getNickname() << std::endl; + std::cout << "Phone number: " << this->_contact[index].getPhoneNbr() << std::endl; + std::cout << "Darkest secret : " << this->_contact[index].getSecret() << std::endl; +} + +void PhoneBook::_search(void) { + std::string input; + long long index; + + __printContacts(); + std::cout << "Enter a contact index : "; + if (!std::getline(std::cin, input)) + return ; + index = input[0] - '1'; + if (index >= 0 && index <= 7) { + if (this->_contact[index].getName().empty()) { + std::cout << "Contact is empty" << std::endl; + return ; + } + __printContact(index); + } + else + std::cout << "Not a valid index" << std::endl; + return ; +}; diff --git a/flake.nix b/flake.nix index 6ffe0b5..cb53ef0 100644 --- a/flake.nix +++ b/flake.nix @@ -24,8 +24,8 @@ ]; hardeningDisable = [ "all" ]; packages = with pkgs; [ - gcc - clang + gcc11 + clang_12 norminette valgrind git