1
0

」 feat(Phonebook): normally finished 👍

This commit is contained in:
2024-11-03 18:52:08 +01:00
parent c7187f972e
commit 1c5639df7b
8 changed files with 186 additions and 31 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ compile_commands.json
.cache
.direnv
flake.lock
phonebook

View File

@ -6,7 +6,7 @@
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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))

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
};

View File

@ -6,59 +6,49 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <string>
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;
}

View File

@ -6,13 +6,38 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <PhoneBook.hpp>
#include <iostream>
#include <string>
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);
}

52
ex01/src/commands/add.cpp Normal file
View File

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* add.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/31 13:51:00 by adjoly #+# #+# */
/* Updated: 2024/11/03 18:06:18 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "PhoneBook.hpp"
#include <string>
#include <iostream>
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;
}

View File

@ -0,0 +1,74 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* search.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/03 16:51:39 by adjoly #+# #+# */
/* Updated: 2024/11/03 16:57:33 by adjoly ### ########.fr */
/* */
/* ************************************************************************* */
#include "PhoneBook.hpp"
#include <iostream>
#include <ostream>
#include <string>
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 ;
};

View File

@ -24,8 +24,8 @@
];
hardeningDisable = [ "all" ];
packages = with pkgs; [
gcc
clang
gcc11
clang_12
norminette
valgrind
git