bit better

This commit is contained in:
whaffman 2025-03-31 14:58:39 +02:00
parent b6a6402774
commit 31ed280bb9
6 changed files with 166 additions and 69 deletions

View File

@ -6,33 +6,32 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/03/19 16:52:26 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 <iostream> #include <iostream>
#include <cctype> #include <cctype>
#include <string>
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int i; std::string str;
int j;
i = 1;
if (argc < 2) if (argc < 2)
{ {
std::cout << "* LOUD AND UNBEARABLE FEEDBACK NOISE *" << std::endl; std::cout << "* LOUD AND UNBEARABLE FEEDBACK NOISE *" << std::endl;
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }
while (i < argc)
for (int i = 1; i < argc; i++)
{ {
j = 0; str = argv[i];
while (argv[i][j]) for (char c : str)
{ {
std::cout << (char)std::toupper(argv[i][j]); std::cout << (char)toupper(c);
j++;
} }
i++; std::cout << " ";
} }
std::cout << std::endl; std::cout << std::endl;
} }

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/03/20 10:48:38 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 class Contact
{ {
public: private:
std::string firstName; std::string firstName;
std::string lastName; std::string lastName;
std::string nickname; std::string nickname;
std::string phoneNumber;
std::string darkestSecret; std::string darkestSecret;
public:
Contact(); 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(); 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);
}; };

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/03/20 10:53:48 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 "Contact.hpp"
#include <string> #include <string>
#define MAX_CONTACTS 2 #define MAX_CONTACTS 8
class PhoneBook { class PhoneBook {
public: public:
PhoneBook(); PhoneBook();
void printContact(const int i) const; void run();
void printAllContacts() const;
void searchContact() const;
void addContact();
private: private:
Contact _contacts[MAX_CONTACTS]; Contact _contacts[MAX_CONTACTS];
int _contactCount; int _contactCount;
void printContact(const int i) const;
void printAllContacts() const;
std::string truncate(std::string str) const; std::string truncate(std::string str) const;
void searchContact() const;
void addContact();
}; };

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/03/20 10:44:49 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 <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
Contact::Contact() Contact::Contact() {}
{
} 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(std::string fn, std::string ln, std::string nn, std::string ds) {}
{
firstName = fn;
lastName = ln;
nickname = nn;
darkestSecret = ds;
}
Contact Contact::newContact() Contact Contact::newContact()
{ {
std::string firstName; std::string firstName;
std::string lastName; std::string lastName;
std::string nickname; std::string nickname;
std::string phoneNumber;
std::string darkestSecret; std::string darkestSecret;
std::cout << "---------------------------------------------" << std::endl; std::cout << "---------------------------------------------" << std::endl;
@ -42,9 +36,57 @@ Contact Contact::newContact()
std::getline(std::cin, lastName); std::getline(std::cin, lastName);
std::cout << std::setw(16) << "nickname: "; std::cout << std::setw(16) << "nickname: ";
std::getline(std::cin, 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::cout << std::setw(16) << "darkest secret: ";
std::getline(std::cin, darkestSecret); std::getline(std::cin, darkestSecret);
std::cout << "---------------------------------------------" << std::endl; 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;
} }

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/03/20 11:02:32 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 <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
PhoneBook::PhoneBook() PhoneBook::PhoneBook()
{ {
_contactCount = 0; _contactCount = 0;
@ -56,13 +55,12 @@ void PhoneBook::printAllContacts() const
for (int i = 0; i < _contactCount; i++) for (int i = 0; i < _contactCount; i++)
{ {
std::cout << std::setw(10) << i << "|"; std::cout << std::setw(10) << i << "|";
std::cout << std::setw(10) << truncate(_contacts[i].firstName) << "|"; std::cout << std::setw(10) << truncate(_contacts[i].getFirstName()) << "|";
std::cout << std::setw(10) << truncate(_contacts[i].lastName) << "|"; std::cout << std::setw(10) << truncate(_contacts[i].getLastName()) << "|";
std::cout << std::setw(10) << truncate(_contacts[i].nickname) << std::endl; std::cout << std::setw(10) << truncate(_contacts[i].getNickname()) << std::endl;
} }
} }
void PhoneBook::printContact(const int i) const void PhoneBook::printContact(const int i) const
{ {
if (i < 0 || i >= _contactCount) if (i < 0 || i >= _contactCount)
@ -71,10 +69,11 @@ void PhoneBook::printContact(const int i) const
return; return;
} }
std::cout << std::setw(16) << "First name: " << _contacts[i].firstName << std::endl; std::cout << std::setw(16) << "First name: " << _contacts[i].getFirstName() << std::endl;
std::cout << std::setw(16) << "Last name: " << _contacts[i].lastName << std::endl; std::cout << std::setw(16) << "Last name: " << _contacts[i].getLastName() << std::endl;
std::cout << std::setw(16) << "Nickname: " << _contacts[i].nickname << std::endl; std::cout << std::setw(16) << "Nickname: " << _contacts[i].getNickname() << std::endl;
std::cout << std::setw(16) << "Darkest secret: " << _contacts[i].darkestSecret << 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; return;
} }
@ -82,7 +81,6 @@ void PhoneBook::printContact(const int i) const
void PhoneBook::searchContact() const void PhoneBook::searchContact() const
{ {
std::string command; std::string command;
int index;
if (_contactCount == 0) if (_contactCount == 0)
{ {
@ -94,13 +92,60 @@ void PhoneBook::searchContact() const
printAllContacts(); printAllContacts();
std::cout << "---------------------------------------------" << std::endl; std::cout << "---------------------------------------------" << std::endl;
int index = -1;
while (true)
{
std::cout << "Enter the index of the contact you want to see: "; std::cout << "Enter the index of the contact you want to see: ";
std::getline(std::cin, command); std::getline(std::cin, command);
std::cout << std::endl; std::cout << std::endl;
index = std::stoi(command); 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; std::cout << "---------------------------------------------" << std::endl;
printContact(index); printContact(index);
std::cout << "---------------------------------------------" << std::endl; std::cout << "---------------------------------------------" << std::endl;
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;
}
}

View File

@ -1,25 +1,20 @@
#include <iostream> /* ************************************************************************** */
#include <string> /* */
/* :::::::: */
/* main.cpp :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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" #include "PhoneBook.hpp"
int main(void) int main(void)
{ {
std::string command;
PhoneBook phoneBook; PhoneBook phoneBook;
phoneBook.run();
while (true) return (0);
{
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;
}
} }