This commit is contained in:
Camille Chauvet 2023-05-22 12:06:23 +00:00
parent 11eb368caf
commit 29364df310
9 changed files with 7 additions and 208 deletions

View File

@ -1,60 +0,0 @@
#include <ios>
#include <ostream>
#include <sstream>
#include <string>
#include <iostream>
#include <iomanip>
#include "Contact.hpp"
#include "utils.hpp"
Contact::Contact(std::string first_name, std::string last_name, std::string nickname, std::string phone_number)
{
this->nickname = nickname;
this->last_name = last_name;
this->first_name = first_name;
this->phone_number = phone_number;
}
Contact::Contact()
{
this->first_name = "";
this->last_name = "";
this->phone_number = "";
this->phone_number = "";
}
Contact::~Contact()
{
}
Contact &Contact::operator=(const Contact &contact)
{
this->first_name = contact.first_name;
this->last_name = contact.last_name;
this->nickname = contact.nickname;
this->phone_number = contact.phone_number;
return (*this);
}
bool Contact::isValid() const
{
return (!(this->first_name == "" || this->last_name == "" || this->nickname == "" || this->phone_number == ""));
}
std::string Contact::to_string() const
{
std::stringstream ss;
ss << std::setw(10) << truncated(this->first_name) << "|"
<< std::setw(10) << truncated(this->last_name) << "|"
<< std::setw(10) << truncated(this->nickname) << "|"
<< std::setw(10) << truncated(this->phone_number);
return(ss.str());
}
std::ostream& operator<<(std::ostream& os, const Contact& contact)
{
os << contact.to_string() << std::endl;
return (os);
}

View File

@ -1,22 +0,0 @@
#include <string>
#include <iostream>
class Contact {
private:
std::string first_name;
std::string last_name;
std::string nickname;
std::string phone_number;
public:
Contact(std::string first_name, std::string last_name, std::string nickname, std::string phone_number);
Contact();
~Contact();
Contact &operator=(const Contact &contact);
bool isValid() const;
std::string to_string() const;
};
std::ostream& operator<<(std::ostream& os, const Contact& fixed);

View File

@ -1,36 +0,0 @@
#include "PhoneBook.hpp"
#include <cstddef>
#include <iostream>
#include <string>
PhoneBook::PhoneBook()
{
this->len = 0;
}
void PhoneBook::add_contact(Contact new_contact)
{
size_t i;
if (this->len < 8)
{
this->contacts[this->len] = new_contact;
this->len++;
}
else {
for (i = this->len; i < 0; i--) {
this->contacts[i] = this->contacts[i - 1];
}
this->contacts[0] = new_contact;
}
}
void PhoneBook::display_contacts()
{
size_t i;
for (i = 0; i < this->len; i++)
{
std::cout << this->contacts[i];
}
}

View File

@ -1,13 +0,0 @@
#include "Contact.hpp"
#include <cstddef>
class PhoneBook {
private:
Contact contacts[8];
size_t len;
public:
PhoneBook();
void add_contact(Contact new_contact);
void display_contacts();
};

Binary file not shown.

View File

@ -1,63 +0,0 @@
#include <iostream>
#include <bits/stdc++.h>
#include <string>
#include "PhoneBook.hpp"
Contact read_contact()
{
std::string first_name;
std::string last_name;
std::string nickname;
std::string phone_number;
std::cout << "first_name: ";
std::getline(std::cin, first_name);
std::cout << "last_name: ";
std::getline(std::cin, last_name);
std::cout << "nickname: ";
std::getline(std::cin, nickname);
std::cout << "phone_number: ";
std::getline(std::cin, phone_number);
return Contact(first_name, last_name, nickname, phone_number);
}
int main()
{
std::string command;
Contact new_contact;
PhoneBook phone_book;
phone_book = PhoneBook();
while (true)
{
std::cout << "command: ";
std::getline(std::cin, command);
if (command == "ADD")
{
new_contact = read_contact();
if (new_contact.isValid())
phone_book.add_contact(new_contact);
else
{
std::cerr << "Contact Error !" << std::endl;
return (1);
}
}
else if (command == "SEARCH")
{
phone_book.display_contacts();
}
else if (command == "EXIT")
{
return (0);
}
else
{
std::cerr << "Command not found" << std::endl;
return (2);
}
}
return (0);
}

7
ex01/t Normal file
View File

@ -0,0 +1,7 @@
ADD
fffffffffff
fff
f
f
f
SEARCH

View File

@ -1,11 +0,0 @@
#include <string>
std::string truncated(const std::string str)
{
std::string out;
out = str.substr(0, 10);
if (str.length() > 10)
out[9] = '.';
return (out);
}

View File

@ -1,3 +0,0 @@
#include <string>
std::string truncated(const std::string str);