Add ex01 solution
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 4 Oct 2024 10:22:19 +0000 (12:22 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 4 Oct 2024 10:22:19 +0000 (12:22 +0200)
ex01/Makefile [new file with mode: 0644]
ex01/contact.cpp [new file with mode: 0644]
ex01/contact.h [new file with mode: 0644]
ex01/main.cpp [new file with mode: 0644]
ex01/phone_book.cpp [new file with mode: 0644]
ex01/phone_book.h [new file with mode: 0644]

diff --git a/ex01/Makefile b/ex01/Makefile
new file mode 100644 (file)
index 0000000..7c33144
--- /dev/null
@@ -0,0 +1,54 @@
+CC := c++
+CFLAGS = -std=c++98 -Wall -Wextra -Werror -Wpedantic
+
+ifneq ("$(wildcard .debug)","")
+       CFLAGS += -g
+endif
+
+RM := rm -f
+
+INCDIR := inc
+INCDIR += $(addsuffix /inc, $(SUBPROJECTS));
+ifneq ($(INCLUDE),)
+       INCLUDE := $(addprefix -I, $(INCDIR))
+endif
+
+SRCDIR := .
+
+SOURCES :=     main.cpp                \
+                       phone_book.cpp  \
+                       contact.cpp             \
+
+SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES))
+
+OBJECTS := $(SOURCES:.cpp=.o)
+
+NAME := phonebook
+
+all : $(NAME)
+
+debug : .debug
+       $(MAKE) all
+
+nodebug :
+       $(RM) .debug
+       $(MAKE) re
+
+.% :
+       $(MAKE) fclean
+       touch $@
+
+$(NAME) : $(OBJECTS)
+       $(CC) $(CFLAGS) -o $@ $^ $(LINKS)
+
+%.o : %.cpp
+       $(CC) $(CFLAGS) -o $@ -c $< $(INCLUDE)
+
+clean :
+       $(RM) $(OBJECTS)
+
+fclean : clean
+       $(RM) $(NAME)
+
+re : fclean
+       $(MAKE) all
diff --git a/ex01/contact.cpp b/ex01/contact.cpp
new file mode 100644 (file)
index 0000000..a4f308f
--- /dev/null
@@ -0,0 +1,54 @@
+#include "contact.h"
+#include <iostream>
+#include <iomanip>
+
+Contact::Contact(void)
+{
+}
+
+Contact        &Contact::operator=(const Contact &contact)
+{
+               m_first_name = contact.m_first_name;
+               m_last_name = contact.m_last_name;
+               m_nickname = contact.m_nickname;
+               m_number = contact.m_number;
+               m_secret = contact.m_secret;
+               return (*this);
+}
+
+Contact::Contact(const Contact &contact)
+{
+       *this = contact;
+}
+
+Contact::~Contact(void)
+{
+}
+
+static void    print_field(std::string &str)
+{
+       if (str.size() <= 10)
+               std::cout << std::setw(10) << str;
+       else
+               std::cout << str.substr(0, 9) << '.';
+       std::cout << '|';
+}
+
+void   Contact::short_print(size_t index)
+{
+       std::cout << '|' << std::setw(10) << index << '|';
+       print_field(m_first_name);
+       print_field(m_last_name);
+       print_field(m_nickname);
+       print_field(m_number);
+       std::cout << '\n';
+}
+
+void   Contact::long_print(void)
+{
+       std::cout << "First name: " << m_first_name << '\n';
+       std::cout << "Last name: " << m_last_name << '\n';
+       std::cout << "Nickname: " << m_nickname << '\n';
+       std::cout << "Phone number: " << m_number << '\n';
+       std::cout << "Darkest secret: " << m_secret << '\n';
+}
diff --git a/ex01/contact.h b/ex01/contact.h
new file mode 100644 (file)
index 0000000..bf80091
--- /dev/null
@@ -0,0 +1,24 @@
+#ifndef CONTACT_H
+# define CONTACT_H
+
+#include <string>
+
+class  Contact
+{
+       public:
+               std::string     m_first_name;
+               std::string     m_last_name;
+               std::string     m_nickname;
+               std::string     m_number;
+               std::string     m_secret;
+
+               Contact(void);
+               Contact(const Contact &contact);
+               ~Contact(void);
+               Contact &operator=(const Contact &contact);
+
+               void short_print(size_t index);
+               void long_print(void);
+};
+
+#endif //CONTACT_H
diff --git a/ex01/main.cpp b/ex01/main.cpp
new file mode 100644 (file)
index 0000000..046b016
--- /dev/null
@@ -0,0 +1,21 @@
+#include "phone_book.h"
+#include <iostream>
+#include <string>
+
+int    main(void)
+{
+       std::string     line;
+       PhoneBook       phone_book;
+
+       while (line != "EXIT")
+       {
+               std::getline(std::cin, line);
+               if (line == "SEARCH")
+                       phone_book.search();
+               else if (line == "ADD")
+                       phone_book.add();
+               else if (line != "EXIT")
+                       std::cout << "The only commands you can use are \"ADD\", \"SEARCH\" and \"EXIT\"\n";
+       }
+       return (0);
+}
diff --git a/ex01/phone_book.cpp b/ex01/phone_book.cpp
new file mode 100644 (file)
index 0000000..38bca8d
--- /dev/null
@@ -0,0 +1,92 @@
+#include "phone_book.h"
+#include <iostream>
+#include <cstdlib>
+#include <string>
+
+PhoneBook::PhoneBook(void):
+       m_size(0), m_last_added(7)
+{
+}
+
+PhoneBook      &PhoneBook::operator=(const PhoneBook &phone_book)
+{
+       m_size = phone_book.m_size;
+       m_last_added = phone_book.m_last_added;
+       for (size_t i = 0; i < phone_book.m_size; ++i)
+               m_contacts[i] = phone_book.m_contacts[i];
+       return (*this);
+}
+
+PhoneBook::PhoneBook(const PhoneBook &phone_book)
+{
+       *this = phone_book;
+}
+
+PhoneBook::~PhoneBook(void)
+{
+}
+
+static void    get_contact_info(const std::string &str, std::string &member)
+{
+       while (true)
+       {
+               std::cout << str << ": ";
+               std::getline(std::cin, member);
+               if (!member.empty())
+                       return ;
+               std::cout << '"' << str << '"' << " cannot be empty\n";
+       }
+}
+
+void   PhoneBook::add(void)
+{
+       if (m_size < 8)
+               ++m_size;
+       m_last_added = (m_last_added + 1) % 8;
+       std::cout << "Please fill the following fields for new contact:\n";
+       get_contact_info("First name", m_contacts[m_last_added].m_first_name);
+       get_contact_info("Last name", m_contacts[m_last_added].m_last_name);
+       get_contact_info("Nickname", m_contacts[m_last_added].m_nickname);
+       get_contact_info("Phone number", m_contacts[m_last_added].m_number);
+       get_contact_info("Darkest secret", m_contacts[m_last_added].m_secret);
+       return ;
+}
+
+std::string    itos(size_t num)
+{
+       std::string     res;
+
+       if (num == 0)
+               return ("0");
+       while (num > 0)
+       {
+               res.insert(0, 1, num % 10 + '0');
+               num /= 10;
+       }
+       return (res);
+}
+
+void   PhoneBook::search(void)
+{
+       std::string     wanted_ind_str;
+       size_t          wanted_ind;
+
+       if (m_size == 0)
+       {
+               std::cout << "There are no contacts to be shown yet.\n";
+               return ;
+       }
+       for (size_t i = m_size; i > 0; --i)
+               m_contacts[(i + m_last_added) % m_size].short_print(m_size - i + 1);
+       while (true)
+       {
+               std::cout << "Which contact do you want to see (1 - " << m_size << "): ";
+               std::getline(std::cin, wanted_ind_str);
+               wanted_ind = std::atoi(wanted_ind_str.c_str());
+               if (1 <= wanted_ind && wanted_ind <= m_size && itos(wanted_ind) == wanted_ind_str)
+                       break ;
+               std::cout << "The index has to be a number between 1 and " << m_size << ".\n";
+       }
+       m_contacts[(m_last_added + 1 - wanted_ind) % m_size].long_print();
+       return ;
+}
diff --git a/ex01/phone_book.h b/ex01/phone_book.h
new file mode 100644 (file)
index 0000000..4577d9f
--- /dev/null
@@ -0,0 +1,25 @@
+#ifndef PHONE_BOOK_H
+# define PHONE_BOOK_H
+
+# include "contact.h"
+# include <cstddef>
+
+typedef std::size_t size_t;
+
+class  PhoneBook
+{
+       private:
+               size_t  m_size;
+               size_t  m_last_added;
+               Contact m_contacts[8];
+       
+       public:
+               PhoneBook(void);
+               PhoneBook(const PhoneBook &phone_book);
+               ~PhoneBook(void);
+               PhoneBook &operator=(const PhoneBook &phone_book);
+               void    add(void);
+               void    search(void);
+};
+
+#endif //PHONE_BOOK_H