This commit is contained in:
Angel Ortigosa Perez
2025-09-07 10:02:26 +02:00
commit 95c25afc03
8 changed files with 438 additions and 0 deletions

68
ex01/Contact/Contact.cpp Normal file
View File

@@ -0,0 +1,68 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Contact.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/04 00:27:59 by aortigos #+# #+# */
/* Updated: 2025/09/05 16:16:35 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Contact.hpp"
Contact::Contact()
{
this->name = "NULL";
}
std::string Contact::getName()
{
return (this->name);
}
std::string Contact::getSurname()
{
return (this->surname);
}
std::string Contact::getNickname()
{
return (this->nickname);
}
std::string Contact::getPhoneNumber()
{
return (this->phoneNumber);
}
std::string Contact::getSecret()
{
return (this->secret);
}
void Contact::setName(std::string str)
{
this->name = str;
}
void Contact::setSurname(std::string str)
{
this->surname = str;
}
void Contact::setNickname(std::string str)
{
this->nickname = str;
}
void Contact::setPhoneNumber(std::string str)
{
this->phoneNumber = str;
}
void Contact::setSecret(std::string str)
{
this->secret = str;
}

43
ex01/Contact/Contact.hpp Normal file
View File

@@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Contact.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/02 21:57:21 by aortigos #+# #+# */
/* Updated: 2025/09/04 14:02:27 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CONTACT_HPP
# define CONTACT_HPP
# include <iostream>
class Contact
{
private:
std::string name;
std::string surname;
std::string nickname;
std::string phoneNumber;
std::string secret;
public:
Contact();
void setName(std::string str);
void setSurname(std::string str);
void setNickname(std::string str);
void setPhoneNumber(std::string str);
void setSecret(std::string str);
std::string getName();
std::string getSurname();
std::string getNickname();
std::string getPhoneNumber();
std::string getSecret();
};
#endif

26
ex01/Makefile Normal file
View File

@@ -0,0 +1,26 @@
NAME=phonebook
SRCS=main.cpp Contact/Contact.cpp Phonebook/Phonebook.cpp
OBJS=$(SRCS:.cpp=.o)
CC=g++
CFLAGS=-Wall -Wextra -Werror -std=c++98
all: $(NAME)
$(NAME): $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o $(NAME)
%.o: %.cpp
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f $(OBJS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY: all clean fclean re

View File

@@ -0,0 +1,166 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Phonebook.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/04 00:44:08 by aortigos #+# #+# */
/* Updated: 2025/09/06 15:43:46 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Phonebook.hpp"
Phonebook::Phonebook()
{
this->index = 0;
}
void Phonebook::addContacts()
{
std::string str;
str = "";
while (str == "")
{
std::cout << "Introduzca el nombre: ";
std::getline(std::cin, str);
}
this->contacts[this->index % 8].setName(str);
str = "";
while (str == "")
{
std::cout << "Introduzca el apellido: ";
std::getline(std::cin, str);
}
this->contacts[this->index % 8].setSurname(str);
str = "";
while (str == "")
{
std::cout << "Introduzca el nickname: ";
std::getline(std::cin, str);
}
this->contacts[this->index % 8].setNickname(str);
str = "";
while (str == "")
{
std::cout << "Introduzca el telefono: ";
std::getline(std::cin, str);
}
this->contacts[this->index % 8].setPhoneNumber(str);
str = "";
while (str == "")
{
std::cout << "Introduzca el secreto: ";
std::getline(std::cin, str);
}
this->contacts[this->index % 8].setSecret(str);
this->index++;
}
static void print_name(std::string str)
{
int i;
int j;
i = 9;
j = 0;
while (str[j] != '\0')
j++;
while (i >= j && j <= 10)
{
std::cout << " ";
i--;
}
i = 0;
while (str[i] != '\0' && i <= 9)
{
if (i == 9 && j > 10)
std::cout << ".";
else
std::cout << str[i];
i++;
}
std::cout << "|";
}
void Phonebook::print_table()
{
int i;
int j;
std::string c;
i = 0;
j = 0;
std::cout << "---------------------------------------------" << std::endl;
std::cout << "| Index| Nombre| Apellido| Nickname|" << std::endl;
std::cout << "---------------------------------------------" << std::endl;
if (this->index >= 9)
j = 8;
else
j = this->index;
while (i < j)
{
c = std::string(1, i + '0');
std::cout << "|";
print_name(c);
print_name(this->contacts[i].getName());
print_name(this->contacts[i].getSurname());
print_name(this->contacts[i].getNickname());
std::cout << std::endl;
i++;
}
std::cout << "---------------------------------------------" << std::endl;
}
void Phonebook::find_contact(int i)
{
if(this->contacts[i].getName() != "NULL")
{
std::cout << "----------------------------------" << std::endl;
std::cout << "Nombre: " << this->contacts[i].getName() << std::endl;
std::cout << "Apellido: " << this->contacts[i].getSurname() << std::endl;
std::cout << "Nickname: " << this->contacts[i].getNickname() << std::endl;
std::cout << "Numero: " << this->contacts[i].getPhoneNumber() << std::endl;
std::cout << "Secreto: " << this->contacts[i].getSecret() << std::endl;
std::cout << "----------------------------------" << std::endl;
} else {
std::cout << "Contacto no encontrado" << std::endl;
}
}
void Phonebook::showContacts()
{
int i;
std::string str;
i = 0;
str = "";
if (this->index == 0)
{
std::cout << "No hay contactos." << std::endl;
return ;
}
print_table();
while (str == "" || str.length() != 1 || (str[0] < '0' || str[0] > '7'))
{
std::cout << "Introduzca el index a buscar: ";
std::getline(std::cin, str);
}
i = str[0] - '0';
find_contact(i);
}

View File

@@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Phonebook.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/04 00:42:04 by aortigos #+# #+# */
/* Updated: 2025/09/06 15:38:25 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PHONEBOOK_HPP
# define PHONEBOOK_HPP
# include <iostream>
# include <sstream>
# include "../Contact/Contact.hpp"
class Phonebook
{
private:
int index;
Contact contacts[8];
public:
Phonebook();
void addContacts();
void showContacts();
void print_table();
void find_contact(int i);
};
#endif

34
ex01/main.cpp Normal file
View File

@@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/02 21:54:41 by aortigos #+# #+# */
/* Updated: 2025/09/06 15:49:04 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Phonebook/Phonebook.hpp"
int main()
{
Phonebook ph;
std::string str;
str = "";
while (str != "EXIT")
{
std::cout << "Phonebook > ";
std::getline(std::cin, str);
if (str == "ADD")
ph.addContacts();
else if (str == "SHOW")
ph.showContacts();
std::cout << std::endl;
}
return (0);
}