Finishing the ex01

This commit is contained in:
Raphael 2024-10-20 21:38:50 +02:00
parent 1e42383953
commit e299deef16
5 changed files with 122 additions and 50 deletions

View file

@ -6,7 +6,7 @@
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ # # By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
# Updated: 2024/10/17 15:36:20 by rparodi ### ########.fr # # Updated: 2024/10/18 17:46:21 by rparodi ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -27,7 +27,8 @@ CXXFLAGS += -MMD -g3
# Flag to debug (TO REMOVE) # Flag to debug (TO REMOVE)
CXXFLAGS += -D DEBUG=1 CXXFLAGS += -D DEBUG=1
# Sources # Sources
SRC = ./sources/phonebook.cpp \ SRC = ./sources/contact.cpp\
./sources/phonebook.cpp \
./sources/main.cpp ./sources/main.cpp
# Objects # Objects

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 14:56:49 by rparodi #+# #+# */ /* Created: 2024/10/17 14:56:49 by rparodi #+# #+# */
/* Updated: 2024/10/18 16:02:09 by rparodi ### ########.fr */ /* Updated: 2024/10/20 21:29:16 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,16 +23,18 @@
#define MENU CLR_BLUE << "ADD" << CLR_RESET << ":\tAdding a new contact" << std::endl << CLR_BLUE << "SEARCH" << CLR_RESET << ":\tSearching a contact in the list" << std::endl << CLR_BLUE << "EXIT" << CLR_RESET << ":\texit the program" << std::endl #define MENU CLR_BLUE << "ADD" << CLR_RESET << ":\tAdding a new contact" << std::endl << CLR_BLUE << "SEARCH" << CLR_RESET << ":\tSearching a contact in the list" << std::endl << CLR_BLUE << "EXIT" << CLR_RESET << ":\texit the program" << std::endl
std::string get_input(const char *str); std::string get_input(const char *err_msg, std::string file, int line);
class Contact class Contact
{ {
public: public:
/*Contact();*/ /*Contact();*/
/*~Contact();*/ /*~Contact();*/
void init_new(int id); bool init_new(int id);
void print_all();
void print(); void print();
private: private:
std::string _get_number(const char *err_msg, std::string file, int line);
int _id; int _id;
std::string _first_name; std::string _first_name;
std::string _last_name; std::string _last_name;
@ -44,10 +46,11 @@ class Contact
class PhoneBook class PhoneBook
{ {
public: public:
void add(); void add(int id);
void search();
Contact array_contact[8]; Contact array_contact[8];
void search(int max);
private: private:
void searching_header();
}; };

View file

@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* contact.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 17:42:58 by rparodi #+# #+# */
/* Updated: 2024/10/20 21:31:16 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "phonebook.hpp"
std::string Contact::_get_number(const char *err_msg, std::string file, int line)
{
std::string to_ret = get_input(err_msg, file, line);
if (to_ret.length() != 10 || to_ret.find_first_not_of("0123456789") == 0)
{
std::cerr << CLR_RED << "Error:\t Thanks to give only 10 digits" << CLR_RESET << std::endl;
return "";
}
else
return to_ret;
}
bool Contact::init_new(int id)
{
_id = id;
std::cout << "First Name:\t";
_first_name = get_input("first_name", __FILE_NAME__, __LINE__);
std::cout << "Last Name:\t";
_last_name = get_input("last_name", __FILE_NAME__, __LINE__);
std::cout << "Nickname:\t";
_nickname = get_input("nickname", __FILE_NAME__, __LINE__);
std::cout << "Number:\t\t";
_number = _get_number("number", __FILE_NAME__, __LINE__);
if (_number.empty())
return false;
std::cout << "Secret:\t\t";
_secret = get_input("secret", __FILE_NAME__, __LINE__);
return true;
}
void Contact::print()
{
std::cout << CLR_GREY << "|" << CLR_RESET << std::setw(10) << _id;
if (_first_name.length() > 10)
std::cout << CLR_GREY << "|" << CLR_RESET << std::setw(9) << _first_name.substr(0, 9) << ".";
else
std::cout << CLR_GREY << "|" << CLR_RESET << std::setw(10) << _first_name;
if (_last_name.length() > 10)
std::cout << CLR_GREY << "|" << CLR_RESET << std::setw(9) << _last_name.substr(0, 9) << ".";
else
std::cout << CLR_GREY << "|" << CLR_RESET << std::setw(10) << _last_name;
if (_nickname.length() > 10)
std::cout << CLR_GREY << "|" << CLR_RESET << std::setw(9) << _nickname.substr(0, 9) << ".";
else
std::cout << CLR_GREY << "|" << CLR_RESET << std::setw(10) << _nickname;
std::cout << CLR_GREY << "|" << CLR_RESET << std::endl;
}
void Contact::print_all()
{
std::cout << CLR_GREY << "First Name:\t" << CLR_RESET << _first_name << std::endl;
std::cout << CLR_GREY << "Last Name:\t" << CLR_RESET << _last_name << std::endl;
std::cout << CLR_GREY << "Nickname:\t" << CLR_RESET << _nickname << std::endl;
std::cout << CLR_GREY << "Number:\t\t" << CLR_RESET << _number << std::endl;
std::cout << CLR_GREY << "Secret:\t\t" << CLR_RESET << _nickname << std::endl;
}

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 14:55:18 by rparodi #+# #+# */ /* Created: 2024/10/17 14:55:18 by rparodi #+# #+# */
/* Updated: 2024/10/18 15:58:15 by rparodi ### ########.fr */ /* Updated: 2024/10/20 21:38:07 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,7 +18,7 @@
* @param err_msg The message return in case of error * @param err_msg The message return in case of error
* @return the value of input * @return the value of input
*/ */
std::string get_input(const char *err_msg) std::string get_input(const char *err_msg, std::string file, int line)
{ {
std::string input; std::string input;
@ -26,10 +26,12 @@ std::string get_input(const char *err_msg)
{ {
if (!std::getline(std::cin, input)) if (!std::getline(std::cin, input))
throw std::runtime_error("getline can't read the input !\n"); throw std::runtime_error("getline can't read the input !\n");
else if (input.empty())
throw std::runtime_error("The input can't be empty !\n");
} }
catch (const std::exception& err) catch (const std::exception& err)
{ {
std::cerr << CLR_BOLD_RED << "\nError:\t" << err.what() << CLR_RESET << CLR_RED << "(" << err_msg << ")" << CLR_RESET << std::endl; std::cerr << CLR_BOLD_RED << "\nError:\t" << err.what() << CLR_RESET << CLR_RED << "(" << err_msg << " [" << file << ":" << line << "]" << ")" << CLR_RESET << std::endl;
exit(1); exit(1);
} }
return (input); return (input);
@ -44,13 +46,18 @@ int main()
while (true) while (true)
{ {
std::cout << MENU << std::endl; std::cout << MENU << std::endl;
input = get_input("SELECT MENU [main.cpp:38]"); input = get_input("Main menu", __FILE_NAME__, __LINE__);
if (strcmp(input.c_str(), "ADD") == 0) if (strcmp(input.c_str(), "ADD") == 0)
{ {
phonebook.array_contact[i].init_new(i); if (phonebook.array_contact[i].init_new(i) == true)
i++; i++;
} }
if (strcmp(input.c_str(), "SEARCH") == 0)
phonebook.search(i);
if (strcmp(input.c_str(), "EXIT") == 0) if (strcmp(input.c_str(), "EXIT") == 0)
{
std::cout << CLR_LIGHT_RED << "Leaving the program..." << CLR_RESET << std::endl;
exit(1); exit(1);
}
} }
} }

View file

@ -6,50 +6,41 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 15:09:21 by rparodi #+# #+# */ /* Created: 2024/10/17 15:09:21 by rparodi #+# #+# */
/* Updated: 2024/10/18 15:55:41 by rparodi ### ########.fr */ /* Updated: 2024/10/20 21:34:27 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "phonebook.hpp" #include "phonebook.hpp"
#include "color.hpp"
#include <stddef.h>
void Contact::init_new(int id) void PhoneBook::searching_header()
{ {
_id = id; std::cout << CLR_GREY << "|" << CLR_GOLD << std::setw(10)<< "ID" << CLR_RESET;
std::cout << "First Name:\t"; std::cout << CLR_GREY << "|" << CLR_GOLD << std::setw(10)<< "First Name" << CLR_RESET;
_first_name = get_input("first_name [phonebook.cpp:18]"); std::cout << CLR_GREY << "|" << CLR_GOLD << std::setw(10)<< "Last Name" << CLR_RESET;
std::cout << "Last Name:\t"; std::cout << CLR_GREY << "|" << CLR_GOLD << std::setw(10)<< "Nickname" << CLR_RESET;
_last_name = get_input("last_name [phonebook.cpp:19]"); std::cout << CLR_GREY << "|" << CLR_RESET << std::endl;
std::cout << "Nickname:\t";
_nickname = get_input("nickname [phonebook.cpp:20]");
std::cout << "Number:\t";
_number = get_input("number [phonebook.cpp:21]");
std::cout << "Secret:\t";
_secret = get_input("secret [phonebook.cpp:22]");
Contact::print();
} }
void PhoneBook::search(int max)
void Contact::print()
{ {
std::cout << "|" << std::setw(10) << _id; unsigned long long chosen;
if (_first_name.length() > 10) if (max == 0)
std::cout << "|" << std::setw(10) << _first_name.substr(0, 9) << "."; {
std::cout << CLR_RED << "\nError:\t Don't have any contact to search in (" << __FILE_NAME__ << ":" << __LINE__ << ")\n" << std::endl;
return;
}
searching_header();
for (int i = 0; i < max; i++)
array_contact[i].print();
std::cout << "\nGive me the contact on which you want more information:" << std::endl;
chosen = atoll(get_input("Search selection", __FILE_NAME__, __LINE__).c_str());
if (chosen >= 0 && chosen < (unsigned long long) max)
array_contact[chosen].print_all();
else else
std::cout << "|" << std::setw(10) << _first_name; {
if (_last_name.length() > 10) std::cout << CLR_RED << "\nError:\t The contact chosen doesn't exist (" << __FILE_NAME__ << ":" << __LINE__ << ")\n" << std::endl;
std::cout << "|" << std::setw(10) << _last_name.substr(0, 9) << "."; return ;
else }
std::cout << "|" << std::setw(10) << _last_name; std::cout << std::endl;
if (_nickname.length() > 10)
std::cout << "|" << std::setw(10) << _nickname.substr(0, 9) << ".";
else
std::cout << "|" << std::setw(10) << _nickname;
if (_number.length() > 10)
std::cout << "|" << std::setw(10) << _number.substr(0, 9) << ".";
else
std::cout << "|" << std::setw(10) << _number;
if (_secret.length() > 10)
std::cout << "|" << std::setw(10) << _secret.substr(0, 9) << ".";
else
std::cout << "|" << std::setw(10) << _secret;
std::cout << "|" << std::endl;
} }