From 31ed280bb9e162f14451815b4dd8d688e15ee315 Mon Sep 17 00:00:00 2001 From: whaffman Date: Mon, 31 Mar 2025 14:58:39 +0200 Subject: [PATCH] bit better --- ex00/src/megaphone.cpp | 19 +++++----- ex01/inc/Contact.hpp | 21 +++++++++-- ex01/inc/PhoneBook.hpp | 15 ++++---- ex01/src/Contact.cpp | 68 +++++++++++++++++++++++++++++------- ex01/src/PhoneBook.cpp | 79 +++++++++++++++++++++++++++++++++--------- ex01/src/main.cpp | 33 ++++++++---------- 6 files changed, 166 insertions(+), 69 deletions(-) diff --git a/ex00/src/megaphone.cpp b/ex00/src/megaphone.cpp index 400e4bc..77971a0 100644 --- a/ex00/src/megaphone.cpp +++ b/ex00/src/megaphone.cpp @@ -6,33 +6,32 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/19 16:52:26 by whaffman #+# #+# */ -/* Updated: 2025/03/19 18:16:03 by whaffman ######## odam.nl */ +/* Updated: 2025/03/31 13:38:09 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include #include +#include int main(int argc, char **argv) { - int i; - int j; + std::string str; - i = 1; if (argc < 2) { std::cout << "* LOUD AND UNBEARABLE FEEDBACK NOISE *" << std::endl; return (EXIT_SUCCESS); } - while (i < argc) + + for (int i = 1; i < argc; i++) { - j = 0; - while (argv[i][j]) + str = argv[i]; + for (char c : str) { - std::cout << (char)std::toupper(argv[i][j]); - j++; + std::cout << (char)toupper(c); } - i++; + std::cout << " "; } std::cout << std::endl; } diff --git a/ex01/inc/Contact.hpp b/ex01/inc/Contact.hpp index 3161307..d65152b 100644 --- a/ex01/inc/Contact.hpp +++ b/ex01/inc/Contact.hpp @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/20 10:48:38 by whaffman #+# #+# */ -/* Updated: 2025/03/21 10:53:49 by whaffman ######## odam.nl */ +/* Updated: 2025/03/31 14:46:59 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -16,13 +16,28 @@ class Contact { - public: + private: std::string firstName; std::string lastName; std::string nickname; + std::string phoneNumber; std::string darkestSecret; + public: Contact(); - Contact(std::string fn, std::string ln, std::string nn, std::string ds); + Contact(const std::string &fn, const std::string &ln, const std::string &nn, const std::string &pn, const std::string &ds); + static Contact newContact(); + + const std::string &getFirstName() const; + const std::string &getLastName() const; + const std::string &getNickname() const; + const std::string &getPhoneNumber() const; + const std::string &getDarkestSecret() const; + + void setFirstName(const std::string &fn); + void setLastName(const std::string &ln); + void setNickname(const std::string &nn); + void setPhoneNumber(const std::string &pn); + void setDarkestSecret(const std::string &ds); }; \ No newline at end of file diff --git a/ex01/inc/PhoneBook.hpp b/ex01/inc/PhoneBook.hpp index 270e397..882ea21 100644 --- a/ex01/inc/PhoneBook.hpp +++ b/ex01/inc/PhoneBook.hpp @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/20 10:53:48 by whaffman #+# #+# */ -/* Updated: 2025/03/21 10:55:03 by whaffman ######## odam.nl */ +/* Updated: 2025/03/31 14:40:58 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -15,20 +15,21 @@ #include "Contact.hpp" #include -#define MAX_CONTACTS 2 +#define MAX_CONTACTS 8 class PhoneBook { public: PhoneBook(); - void printContact(const int i) const; - void printAllContacts() const; - void searchContact() const; - void addContact(); + void run(); private: Contact _contacts[MAX_CONTACTS]; int _contactCount; - + void printContact(const int i) const; + void printAllContacts() const; std::string truncate(std::string str) const; + void searchContact() const; + void addContact(); + }; diff --git a/ex01/src/Contact.cpp b/ex01/src/Contact.cpp index 6848c24..d69b827 100644 --- a/ex01/src/Contact.cpp +++ b/ex01/src/Contact.cpp @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/20 10:44:49 by whaffman #+# #+# */ -/* Updated: 2025/03/21 10:46:44 by whaffman ######## odam.nl */ +/* Updated: 2025/03/31 14:40:31 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,24 +14,18 @@ #include #include -Contact::Contact() -{ +Contact::Contact() {} -} - -Contact::Contact(std::string fn, std::string ln, std::string nn, std::string ds) -{ - firstName = fn; - lastName = ln; - nickname = nn; - darkestSecret = ds; -} +Contact::Contact(const std::string &fn, const std::string &ln, const std::string &nn, const std::string &pn, const std::string &ds) + : firstName(fn), lastName(ln), nickname(nn), phoneNumber(pn), darkestSecret(ds) + {} Contact Contact::newContact() { std::string firstName; std::string lastName; std::string nickname; + std::string phoneNumber; std::string darkestSecret; std::cout << "---------------------------------------------" << std::endl; @@ -42,9 +36,57 @@ Contact Contact::newContact() std::getline(std::cin, lastName); std::cout << std::setw(16) << "nickname: "; std::getline(std::cin, nickname); + std::cout << std::setw(16) << "phone number: "; + std::getline(std::cin, phoneNumber); std::cout << std::setw(16) << "darkest secret: "; std::getline(std::cin, darkestSecret); std::cout << "---------------------------------------------" << std::endl; - return (Contact(firstName, lastName, nickname, darkestSecret)); + return (Contact(firstName, lastName, nickname, phoneNumber, darkestSecret)); +} + +const std::string &Contact::getFirstName() const +{ + return (firstName); +} + +const std::string &Contact::getLastName() const +{ + return (lastName); +} + +const std::string &Contact::getPhoneNumber() const +{ + return (phoneNumber); +} + +const std::string &Contact::getNickname() const +{ + return (nickname); +} + +const std::string &Contact::getDarkestSecret() const +{ + return (darkestSecret); +} + +void Contact::setFirstName(const std::string &fn) +{ + firstName = fn; +} +void Contact::setLastName(const std::string &ln) +{ + lastName = ln; +} +void Contact::setPhoneNumber(const std::string &pn) +{ + phoneNumber = pn; +} +void Contact::setNickname(const std::string &nn) +{ + nickname = nn; +} +void Contact::setDarkestSecret(const std::string &ds) +{ + darkestSecret = ds; } \ No newline at end of file diff --git a/ex01/src/PhoneBook.cpp b/ex01/src/PhoneBook.cpp index bfc7d32..442ebb1 100644 --- a/ex01/src/PhoneBook.cpp +++ b/ex01/src/PhoneBook.cpp @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/20 11:02:32 by whaffman #+# #+# */ -/* Updated: 2025/03/21 10:56:49 by whaffman ######## odam.nl */ +/* Updated: 2025/03/31 14:58:09 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -15,7 +15,6 @@ #include #include - PhoneBook::PhoneBook() { _contactCount = 0; @@ -51,18 +50,17 @@ void PhoneBook::printAllContacts() const std::cout << std::setw(10) << "Index" << "|"; std::cout << std::setw(10) << "First name" << "|"; std::cout << std::setw(10) << "Last name" << "|"; - std::cout << std::setw(10) << "Nickname" << std::endl; + std::cout << std::setw(10) << "Nickname" << std::endl; for (int i = 0; i < _contactCount; i++) { std::cout << std::setw(10) << i << "|"; - std::cout << std::setw(10) << truncate(_contacts[i].firstName) << "|"; - std::cout << std::setw(10) << truncate(_contacts[i].lastName) << "|"; - std::cout << std::setw(10) << truncate(_contacts[i].nickname) << std::endl; + std::cout << std::setw(10) << truncate(_contacts[i].getFirstName()) << "|"; + std::cout << std::setw(10) << truncate(_contacts[i].getLastName()) << "|"; + std::cout << std::setw(10) << truncate(_contacts[i].getNickname()) << std::endl; } } - void PhoneBook::printContact(const int i) const { if (i < 0 || i >= _contactCount) @@ -71,18 +69,18 @@ void PhoneBook::printContact(const int i) const return; } - std::cout << std::setw(16) << "First name: " << _contacts[i].firstName << std::endl; - std::cout << std::setw(16) << "Last name: " << _contacts[i].lastName << std::endl; - std::cout << std::setw(16) << "Nickname: " << _contacts[i].nickname << std::endl; - std::cout << std::setw(16) << "Darkest secret: " << _contacts[i].darkestSecret << std::endl; + std::cout << std::setw(16) << "First name: " << _contacts[i].getFirstName() << std::endl; + std::cout << std::setw(16) << "Last name: " << _contacts[i].getLastName() << std::endl; + std::cout << std::setw(16) << "Nickname: " << _contacts[i].getNickname() << std::endl; + std::cout << std::setw(16) << "Phone number: " << _contacts[i].getPhoneNumber() << std::endl; + std::cout << std::setw(16) << "Darkest secret: " << _contacts[i].getDarkestSecret() << std::endl; return; } void PhoneBook::searchContact() const { - std::string command; - int index; + std::string command; if (_contactCount == 0) { @@ -94,13 +92,60 @@ void PhoneBook::searchContact() const printAllContacts(); std::cout << "---------------------------------------------" << std::endl; - std::cout << "Enter the index of the contact you want to see: "; - std::getline(std::cin, command); - std::cout << std::endl; - index = std::stoi(command); + int index = -1; + while (true) + { + std::cout << "Enter the index of the contact you want to see: "; + std::getline(std::cin, command); + std::cout << std::endl; + if (command == "EXIT") + break; + if (command.empty()) + { + std::cout << "Invalid index" << std::endl; + continue; + } + try + { + index = std::stoi(command); //0dsfsdf is also valid.... + } + catch (const std::invalid_argument &e) + { + std::cout << "Invalid index" << std::endl; + continue; + } + catch (const std::out_of_range &e) + { + std::cout << "Index out of range" << std::endl; + continue; + } + if (index >= 0 && index < _contactCount) + { + break; + } + } std::cout << "---------------------------------------------" << std::endl; printContact(index); std::cout << "---------------------------------------------" << std::endl; std::cout << std::endl; } + +void PhoneBook::run() +{ + std::string command; + + while (true) + { + std::cout << "Enter a command: "; + getline(std::cin, command); + if (command == "EXIT") + break; + else if (command == "ADD") + addContact(); + else if (command == "SEARCH") + searchContact(); + else + std::cout << "Invalid command" << std::endl; + } +} \ No newline at end of file diff --git a/ex01/src/main.cpp b/ex01/src/main.cpp index 0030d48..e9cbebb 100644 --- a/ex01/src/main.cpp +++ b/ex01/src/main.cpp @@ -1,25 +1,20 @@ -#include -#include +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* main.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/03/31 14:43:46 by whaffman #+# #+# */ +/* Updated: 2025/03/31 14:43:47 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ -#include "Contact.hpp" #include "PhoneBook.hpp" int main(void) { - std::string command; - PhoneBook phoneBook; - - while (true) - { - std::cout << "Enter a command: "; - getline(std::cin, command); - if (command == "EXIT") - break; - else if (command == "ADD") - phoneBook.addContact(); - else if (command == "SEARCH") - phoneBook.searchContact(); - else - std::cout << "Invalid command" << std::endl; - } + PhoneBook phoneBook; + phoneBook.run(); + return (0); } \ No newline at end of file