Fix: finishing this exercice

This commit is contained in:
Raphael 2024-10-22 14:10:16 +02:00
parent b7b92dfbcf
commit 88823cbbf1
5 changed files with 101 additions and 39 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/18 17:46:21 by rparodi ### ########.fr # # Updated: 2024/10/21 15:58:19 by rparodi ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #

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/20 21:29:16 by rparodi ### ########.fr */ /* Updated: 2024/10/22 14:06:11 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,7 +16,6 @@
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
#include <string> #include <string>
#include <stddef.h>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include "color.hpp" #include "color.hpp"
@ -28,13 +27,17 @@ std::string get_input(const char *err_msg, std::string file, int line);
class Contact class Contact
{ {
public: public:
/*Contact();*/ Contact();
/*~Contact();*/ ~Contact();
bool init_new(int id); void init_new(int id);
void print_all(); void print_all();
void print(); void print();
bool getUsed();
void setUsed();
void clear();
private: private:
std::string _get_number(const char *err_msg, std::string file, int line); std::string _get_number(const char *err_msg, std::string file, int line);
bool _used;
int _id; int _id;
std::string _first_name; std::string _first_name;
std::string _last_name; std::string _last_name;
@ -46,12 +49,13 @@ class Contact
class PhoneBook class PhoneBook
{ {
public: public:
PhoneBook();
~PhoneBook();
void add(int id); void add(int id);
Contact array_contact[8]; Contact array_contact[8];
void search(int max); void search();
private: private:
void searching_header(); void searching_header();
}; };
#endif #endif

View file

@ -6,12 +6,44 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 17:42:58 by rparodi #+# #+# */ /* Created: 2024/10/18 17:42:58 by rparodi #+# #+# */
/* Updated: 2024/10/20 21:31:16 by rparodi ### ########.fr */ /* Updated: 2024/10/22 14:05:15 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "phonebook.hpp" #include "phonebook.hpp"
Contact::Contact()
{
_used = false;
_id = 0;
_first_name = "";
_last_name = "";
_nickname = "";
_number = "";
_secret = "";
}
Contact::~Contact()
{
return;
}
bool Contact::getUsed()
{
return _used;
}
void Contact::clear()
{
_used = false;
_id = 0;
_first_name = "";
_last_name = "";
_nickname = "";
_number = "";
_secret = "";
}
std::string Contact::_get_number(const char *err_msg, std::string file, int line) std::string Contact::_get_number(const char *err_msg, std::string file, int line)
{ {
std::string to_ret = get_input(err_msg, file, line); std::string to_ret = get_input(err_msg, file, line);
@ -24,26 +56,41 @@ std::string Contact::_get_number(const char *err_msg, std::string file, int line
return to_ret; return to_ret;
} }
bool Contact::init_new(int id) void Contact::init_new(int id)
{ {
_id = id; _id = id;
_used = true;
while (_first_name.empty() == true)
{
std::cout << "First Name:\t"; std::cout << "First Name:\t";
_first_name = get_input("first_name", __FILE_NAME__, __LINE__); _first_name = get_input("first_name", __FILE_NAME__, __LINE__);
}
while (_last_name.empty() == true)
{
std::cout << "Last Name:\t"; std::cout << "Last Name:\t";
_last_name = get_input("last_name", __FILE_NAME__, __LINE__); _last_name = get_input("last_name", __FILE_NAME__, __LINE__);
}
while (_nickname.empty() == true)
{
std::cout << "Nickname:\t"; std::cout << "Nickname:\t";
_nickname = get_input("nickname", __FILE_NAME__, __LINE__); _nickname = get_input("nickname", __FILE_NAME__, __LINE__);
}
while (_number.empty() == true)
{
std::cout << "Number:\t\t"; std::cout << "Number:\t\t";
_number = _get_number("number", __FILE_NAME__, __LINE__); _number = _get_number("number", __FILE_NAME__, __LINE__);
if (_number.empty()) }
return false; while (_secret.empty() == true)
{
std::cout << "Secret:\t\t"; std::cout << "Secret:\t\t";
_secret = get_input("secret", __FILE_NAME__, __LINE__); _secret = get_input("secret", __FILE_NAME__, __LINE__);
return true; }
} }
void Contact::print() void Contact::print()
{ {
if (!_used) return;
std::cout << CLR_GREY << "|" << CLR_RESET << std::setw(10) << _id; std::cout << CLR_GREY << "|" << CLR_RESET << std::setw(10) << _id;
if (_first_name.length() > 10) if (_first_name.length() > 10)
std::cout << CLR_GREY << "|" << CLR_RESET << std::setw(9) << _first_name.substr(0, 9) << "."; std::cout << CLR_GREY << "|" << CLR_RESET << std::setw(9) << _first_name.substr(0, 9) << ".";

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/20 21:38:07 by rparodi ### ########.fr */ /* Updated: 2024/10/22 14:05:46 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,14 +26,14 @@ std::string get_input(const char *err_msg, std::string file, int line)
{ {
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 << " [" << file << ":" << line << "]" << ")" << 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);
} }
if (input.empty() == true)
return "";
return (input); return (input);
} }
@ -47,14 +47,16 @@ int main()
{ {
std::cout << MENU << std::endl; std::cout << MENU << std::endl;
input = get_input("Main menu", __FILE_NAME__, __LINE__); input = get_input("Main menu", __FILE_NAME__, __LINE__);
if (strcmp(input.c_str(), "ADD") == 0) if (input == "ADD")
{ {
if (phonebook.array_contact[i].init_new(i) == true) if (phonebook.array_contact[i].getUsed() == true)
i++; phonebook.array_contact[i].clear();
phonebook.array_contact[i].init_new(i);
i = (i + 1) % 8;
} }
if (strcmp(input.c_str(), "SEARCH") == 0) if (input == "SEARCH")
phonebook.search(i); phonebook.search();
if (strcmp(input.c_str(), "EXIT") == 0) if (input == "EXIT")
{ {
std::cout << CLR_LIGHT_RED << "Leaving the program..." << CLR_RESET << std::endl; std::cout << CLR_LIGHT_RED << "Leaving the program..." << CLR_RESET << std::endl;
exit(1); exit(1);

View file

@ -6,13 +6,22 @@
/* 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/20 21:34:27 by rparodi ### ########.fr */ /* Updated: 2024/10/21 17:13:54 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "phonebook.hpp" #include "phonebook.hpp"
#include "color.hpp"
#include <stddef.h> PhoneBook::PhoneBook()
{
for (int i = 0; i < 8; i++)
array_contact[i] = Contact();
}
PhoneBook::~PhoneBook()
{
return;
}
void PhoneBook::searching_header() void PhoneBook::searching_header()
{ {
@ -22,20 +31,20 @@ void PhoneBook::searching_header()
std::cout << CLR_GREY << "|" << CLR_GOLD << std::setw(10)<< "Nickname" << CLR_RESET; std::cout << CLR_GREY << "|" << CLR_GOLD << std::setw(10)<< "Nickname" << CLR_RESET;
std::cout << CLR_GREY << "|" << CLR_RESET << std::endl; std::cout << CLR_GREY << "|" << CLR_RESET << std::endl;
} }
void PhoneBook::search(int max) void PhoneBook::search()
{ {
unsigned long long chosen; unsigned long long chosen;
if (max == 0) if (!array_contact[0].getUsed())
{ {
std::cout << CLR_RED << "\nError:\t Don't have any contact to search in (" << __FILE_NAME__ << ":" << __LINE__ << ")\n" << std::endl; std::cout << CLR_RED << "\nError:\t Don't have any contact to search in (" << __FILE_NAME__ << ":" << __LINE__ << ")\n" << std::endl;
return; return;
} }
searching_header(); searching_header();
for (int i = 0; i < max; i++) for (int i = 0; i < 8; i++)
array_contact[i].print(); array_contact[i].print();
std::cout << "\nGive me the contact on which you want more information:" << std::endl; 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()); chosen = atoll(get_input("Search selection", __FILE_NAME__, __LINE__).c_str());
if (chosen >= 0 && chosen < (unsigned long long) max) if (chosen >= 0 && chosen < 8 && array_contact[chosen].getUsed())
array_contact[chosen].print_all(); array_contact[chosen].print_all();
else else
{ {