fix
This commit is contained in:
54
ex01/src/Contact.cpp
Normal file
54
ex01/src/Contact.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
#include <ios>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include "../include/Contact.hpp"
|
||||
#include "../include/utils.hpp"
|
||||
|
||||
Contact::Contact(std::string first_name, std::string last_name, std::string nickname, std::string phone_number, std::string darkest_secret)
|
||||
{
|
||||
this->first_name = std::string(first_name);
|
||||
this->last_name = std::string(last_name);
|
||||
this->nickname = std::string(nickname);
|
||||
this->phone_number = std::string(phone_number);
|
||||
this->darkest_secret = std::string(darkest_secret);
|
||||
}
|
||||
|
||||
std::string Contact::to_string_partial() const
|
||||
{
|
||||
std::stringstream ss;
|
||||
std::string short_first_name;
|
||||
std::string short_last_name;
|
||||
std::string short_nickname;
|
||||
std::string short_phone_number;
|
||||
std::string short_darkest_secret;
|
||||
|
||||
short_first_name = truncated(this->first_name);
|
||||
short_last_name = truncated(this->last_name);
|
||||
short_nickname = truncated(this->nickname);
|
||||
short_phone_number = truncated(this->phone_number);
|
||||
short_darkest_secret = truncated(this->darkest_secret);
|
||||
|
||||
ss << std::setw(10) << short_first_name << "|"
|
||||
<< std::setw(10) << short_last_name << "|"
|
||||
<< std::setw(10) << short_nickname << "|"
|
||||
<< std::setw(10) << short_phone_number << "|"
|
||||
<< std::setw(10) << short_darkest_secret;
|
||||
|
||||
return(ss.str());
|
||||
}
|
||||
|
||||
std::string Contact::to_string_complete() const
|
||||
{
|
||||
std::stringstream ss;
|
||||
|
||||
ss << std::setw(10) << this->first_name << "|"
|
||||
<< std::setw(10) << this->last_name << "|"
|
||||
<< std::setw(10) << this->nickname << "|"
|
||||
<< std::setw(10) << this->phone_number << "|"
|
||||
<< std::setw(10) << this->darkest_secret << "|";
|
||||
|
||||
return(ss.str());
|
||||
}
|
66
ex01/src/PhoneBook.cpp
Normal file
66
ex01/src/PhoneBook.cpp
Normal file
@ -0,0 +1,66 @@
|
||||
#include "../include/PhoneBook.hpp"
|
||||
#include <cstddef>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
PhoneBook::PhoneBook()
|
||||
{
|
||||
this->len = 0;
|
||||
}
|
||||
|
||||
PhoneBook::~PhoneBook()
|
||||
{
|
||||
for (size_t i = 0; i < this->len; i++)
|
||||
delete this->contacts[i];
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
std::cout
|
||||
<< " index|"
|
||||
<< "first name|"
|
||||
<< "last name |"
|
||||
<< "nickname |"
|
||||
<< "phone num.|"
|
||||
<< "darkest s."
|
||||
<< std::endl;
|
||||
|
||||
for (i = 0; i < this->len; i++)
|
||||
{
|
||||
std::cout
|
||||
<< std::setw(10) << i
|
||||
<< "|"
|
||||
<< this->contacts[i]->to_string_partial()
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
Contact* PhoneBook::search(int index)
|
||||
{
|
||||
if ((size_t) index > this->len || index < 0)
|
||||
return (NULL);
|
||||
return (this->contacts[index]);
|
||||
}
|
96
ex01/src/main.cpp
Normal file
96
ex01/src/main.cpp
Normal file
@ -0,0 +1,96 @@
|
||||
#include <cstddef>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <bits/stdc++.h>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include "../include/PhoneBook.hpp"
|
||||
|
||||
int get_input(std::string &in, std::string out)
|
||||
{
|
||||
while (in.size() == 0)
|
||||
{
|
||||
std::cout << out;
|
||||
if (!std::getline(std::cin, in))
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
Contact* read_contact()
|
||||
{
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
std::string nickname;
|
||||
std::string phone_number;
|
||||
std::string darkest_secret;
|
||||
|
||||
if (get_input(first_name, "first_name: ")
|
||||
|| get_input(last_name, "last_name: ")
|
||||
|| get_input(nickname, "nickname: ")
|
||||
|| get_input(phone_number, "phone_number: ")
|
||||
|| get_input(darkest_secret, "darkest_secret: "))
|
||||
return (NULL);
|
||||
|
||||
return (new Contact(first_name, last_name, nickname, phone_number, darkest_secret));
|
||||
|
||||
}
|
||||
|
||||
Contact* search(PhoneBook phone_book)
|
||||
{
|
||||
int index;
|
||||
std::string index_str;
|
||||
Contact* contact;
|
||||
|
||||
contact = NULL;
|
||||
while (contact == NULL)
|
||||
{
|
||||
if (get_input(index_str, "index: "))
|
||||
return (NULL);
|
||||
index = std::atoi(index_str.c_str());
|
||||
contact = phone_book.search(index);
|
||||
}
|
||||
return (contact);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string command;
|
||||
Contact* contact;
|
||||
PhoneBook phone_book;
|
||||
|
||||
phone_book = PhoneBook();
|
||||
while (true)
|
||||
{
|
||||
std::cout << "command: ";
|
||||
if (!std::getline(std::cin, command))
|
||||
return (0);
|
||||
if (command == "ADD")
|
||||
{
|
||||
contact = read_contact();
|
||||
if (contact == NULL)
|
||||
return (0);
|
||||
phone_book.add_contact(contact);
|
||||
}
|
||||
else if (command == "SEARCH")
|
||||
{
|
||||
phone_book.display_contacts();
|
||||
if (phone_book.len > 0)
|
||||
{
|
||||
contact = search(phone_book);
|
||||
if (contact == NULL)
|
||||
return (0);
|
||||
std::cout << contact->to_string_complete() << std::endl;
|
||||
}
|
||||
}
|
||||
else if (command == "EXIT")
|
||||
{
|
||||
return (0);
|
||||
}
|
||||
else {
|
||||
std::cerr << "Command not found" << std::endl;
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
11
ex01/src/utils.cpp
Normal file
11
ex01/src/utils.cpp
Normal file
@ -0,0 +1,11 @@
|
||||
#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);
|
||||
}
|
Reference in New Issue
Block a user