Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming / Project 1 / src / Generic
diff options
context:
space:
mode:
Commit24bdf39e6c21ab846f6c86803761d78a094e05c0 (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-05-26 20:31:26 +0300
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-05-26 20:31:26 +0300
Files1b2077b088098cc7ae6afc946b10a4d680110294 /Year 1/Object-oriented Programming/Project 1/src/Generic
uni-projects-24bdf39e6c21ab846f6c86803761d78a094e05c0.tar
uni-projects-24bdf39e6c21ab846f6c86803761d78a094e05c0.tar.gz
uni-projects-24bdf39e6c21ab846f6c86803761d78a094e05c0.zip
Parentcbe944835cc0951a946d01f034b4d28ff9874a4e (diff)
Added files from project 1 (mirror)
diff --git a/Year 1/Object-oriented Programming/Project 1/src/Generic/ConsoleInterface/Command.cpp b/Year 1/Object-oriented Programming/Project 1/src/Generic/ConsoleInterface/Command.cpp
new file mode 100644
index 0000000..c14bcc8
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Project 1/src/Generic/ConsoleInterface/Command.cpp
@@ -0,0 +1,24 @@
+#include "Command.h"
+#include <cstring>
+
+Command::Command() : Command("", nullptr) {}
+
+/*! \param nameInMenu C-style string, class stores at most 255 characters (last character is always terminating zero)
+ * \param exec Function pointer that will be executed when run() is called. Can be nullptr.
+ */
+Command::Command(const char* nameInMenu, void (*exec)()) {
+ strncpy(this->nameInMenu, nameInMenu, 255);
+ this->nameInMenu[255] = '\0';
+ this->exec = exec;
+}
+
+/*! Executes the stored function pointer, when it's not nullptr. Otherwise does nothing.
+ */
+void Command::run() const {
+ if (exec != nullptr)
+ exec();
+}
+
+const char* Command::get_nameInMenu() const {
+ return nameInMenu;
+}
diff --git a/Year 1/Object-oriented Programming/Project 1/src/Generic/ConsoleInterface/Command.h b/Year 1/Object-oriented Programming/Project 1/src/Generic/ConsoleInterface/Command.h
new file mode 100644
index 0000000..f1eb55b
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Project 1/src/Generic/ConsoleInterface/Command.h
@@ -0,0 +1,27 @@
+#ifndef HEADER_CONSOLEINTERFACE_COMMAND
+#define HEADER_CONSOLEINTERFACE_COMMAND
+
+/*! \file Command.h
+ * \brief Stores the declaration of class Command
+ */
+
+/*! \brief Stores a 256 character name and a function pointer to be executed when calling run()
+ *
+ * Menu uses this class for a more generic implementation of it's navigate function.
+ */
+class Command {
+ char nameInMenu[256];
+ void (*exec)();
+
+public:
+ //! Leaves name empty and exec function pointer to nullptr
+ Command();
+ //! Copies contents of nameInMenu and stores exec
+ Command(const char* nameInMenu, void (*exec)());
+ //! Executes the stored function pointer
+ void run() const;
+
+ const char* get_nameInMenu() const;
+};
+
+#endif
diff --git a/Year 1/Object-oriented Programming/Project 1/src/Generic/ConsoleInterface/Menu.cpp b/Year 1/Object-oriented Programming/Project 1/src/Generic/ConsoleInterface/Menu.cpp
new file mode 100644
index 0000000..3ab857e
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Project 1/src/Generic/ConsoleInterface/Menu.cpp
@@ -0,0 +1,135 @@
+#include "Menu.h"
+#include "Toolbox.hpp"
+#include <cstring>
+
+Menu::Menu() : Menu("Menu", false, false) {}
+
+/*! \param title C-style string, class stores at most 255 characters (last character is always terminating zero)
+ * \param backExistsApp Flag whether or not to list the 0-th option as "Exit", rather than "Go Back"
+ * \param isSubMenu Flag whether or not to show the title as a subTitle or not
+ */
+Menu::Menu(const char* title, bool backExistsApp, bool isSubMenu) : menuOptions() {
+ this->backExistsApp = backExistsApp;
+ this->isSubMenu = isSubMenu;
+ strncpy(this->title, title, 255);
+ this->title[255] = '\0';
+}
+
+/*! \param title C-style string, class stores at most 255 characters (last character is always terminating zero)
+ * \param backExistsApp Flag whether or not to list the 0-th option as "Exit", rather than "Go Back"
+ * \param isSubMenu Flag whether or not to show the title as a subTitle or not
+ * \param commands Pointer to an array of Command instances
+ * \param commandCount Number of elements that "commands" points to
+ */
+Menu::Menu(const char* title, bool backExistsApp, bool isSubMenu, const Command* commands, unsigned commandCount) {
+ this->backExistsApp = backExistsApp;
+ this->isSubMenu = isSubMenu;
+ menuOptions = List<Command>(commands, commandCount);
+ strncpy(this->title, title, 255);
+ this->title[255] = '\0';
+}
+
+void Menu::addCommand(const Command& command) {
+ menuOptions.add(command);
+}
+
+enum MessageType {
+ NoPrint,
+ Success,
+ Warning,
+ Error,
+};
+const char* msg;
+MessageType msgType;
+
+/*! \param message C-style string, saves to pointer for later reuse when showing the message.
+ *
+ * Stores the message pointer in a global variable (in the cpp file) and
+ * sets it's type as an Error in another global variable (in the cpp file).
+ *
+ * The next time the whole Menu is shown, the message will appear below the title.
+ */
+void Menu::registerError(const char* message) const {
+ msg = message;
+ msgType = Error;
+}
+
+/*! \param message C-style string, saves to pointer for later reuse when showing the message.
+ *
+ * Stores the message pointer in a global variable (in the cpp file) and
+ * sets it's type as a Warning in another global variable (in the cpp file).
+ *
+ * The next time the whole Menu is shown, the message will appear below the title.
+ */
+void Menu::registerWarning(const char* message) const {
+ msg = message;
+ msgType = Warning;
+}
+
+/*! \param message C-style string, saves to pointer for later reuse when showing the message.
+ *
+ * Stores the message pointer in a global variable (in the cpp file) and
+ * sets it's type as Success in another global variable (in the cpp file).
+ *
+ * The next time the whole Menu is shown, the message will appear below the title.
+ */
+void Menu::registerSuccess(const char* message) const {
+ msg = message;
+ msgType = Success;
+}
+
+/*!
+ * Prints the title, then an Error/Warning/Sucess message (if set),
+ * prints all command names as an ordered list (starting from 1),
+ * waits for user input to select one of those commands (by list number)
+ * and finally executes the appropriate command's run() function.
+ *
+ * After the run() function exists, everything is reprinted.
+ * The 0 list index is always "Go Back"/"Exit" and it always stops the reprinting loop.
+ *
+ * If there are no commands, prints "Menu is empty!".
+ * If user input doesn't correspond to any command, registers an error message and reprints.
+ */
+void Menu::navigate() const {
+ if (menuOptions.get_count() == 0) {
+ printLine("Menu is empty!");
+ return;
+ }
+
+ unsigned buffer = 1; // We immedeately read the value of buffer, so this is fine
+
+ while (buffer != 0) {
+ clear();
+ if (isSubMenu)
+ subTitleBox(title);
+ else
+ titleBox(title);
+
+ switch(msgType) {
+ case Error: errorBox(msg); msgType = NoPrint; break;
+ case Warning: warningBox(msg); msgType = NoPrint; break;
+ case Success: successBox(msg); msgType = NoPrint; break;
+ case NoPrint: printLine(""); break;
+ }
+ printLine("");
+
+ resetOrderedList(0);
+ if (backExistsApp)
+ printOrderedListElem("Exit");
+ else
+ printOrderedListElem("Go Back");
+
+ for (unsigned i = 0; i < menuOptions.get_count(); i++)
+ printOrderedListElem(menuOptions[i].get_nameInMenu());
+
+ _printInputBoxLabel("Execute No [0-");
+ print(menuOptions.get_count());
+ print("]: ");
+ read(buffer);
+
+ if (buffer > menuOptions.get_count())
+ registerError("Invalid menu option!");
+ else if (buffer > 0)
+ menuOptions[buffer - 1].run();
+ }
+}
diff --git a/Year 1/Object-oriented Programming/Project 1/src/Generic/ConsoleInterface/Menu.h b/Year 1/Object-oriented Programming/Project 1/src/Generic/ConsoleInterface/Menu.h
new file mode 100644
index 0000000..e0ea685
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Project 1/src/Generic/ConsoleInterface/Menu.h
@@ -0,0 +1,46 @@
+#ifndef HEADER_CONSOLEINTERFACE_MENU
+#define HEADER_CONSOLEINTERFACE_MENU
+
+/*! \file Menu.h
+ * \brief Stores the declaration of class Menu
+ */
+
+#include "Command.h"
+#include "../List/List.hpp"
+
+/*! \brief Handles navigation between multiple commands
+ *
+ * Shows the title, then below it an Error/Warning/Success message if set,
+ * lists all command options as an enumerated list, takes command index
+ * and calls the chosen command's run() function.
+ */
+class Menu {
+ char title[256];
+ List<Command> menuOptions;
+
+ bool backExistsApp;
+ bool isSubMenu;
+
+public:
+ //! Sets name as "Menu", leaves all flags to false and creates an empty Command list
+ Menu();
+ //! Copies title, saves flags and creates and empty Command list
+ Menu(const char* title, bool backExistsApp, bool isSubMenu);
+ //! Copies title, saves flags and copies commands
+ Menu(const char* title, bool backExistsApp, bool isSubMenu, const Command* commands, unsigned commandCount);
+
+ //! Adds a command to the internal Command list
+ void addCommand(const Command& command);
+
+ //! Tell Menu to show an error message next time it prints
+ void registerError(const char* message) const;
+ //! Tell Menu to show a warning message next time it prints
+ void registerWarning(const char* message) const;
+ //! Tell Menu to show a success message next time it prints
+ void registerSuccess(const char* message) const;
+
+ //! Lists commands and after user input executes an appropriate command run() function
+ void navigate() const;
+};
+
+#endif
diff --git a/Year 1/Object-oriented Programming/Project 1/src/Generic/ConsoleInterface/Toolbox.cpp b/Year 1/Object-oriented Programming/Project 1/src/Generic/ConsoleInterface/Toolbox.cpp
new file mode 100644
index 0000000..ca83b3b
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Project 1/src/Generic/ConsoleInterface/Toolbox.cpp
@@ -0,0 +1,223 @@
+#include "Toolbox.hpp"
+
+/*!
+ * Erases display and moves cursor to top left corner via ANSI escape sequences: \c ESC[2J \c ESC[1;1H
+ *
+ * \warning Not all terminals support any or all ANSI escape sequences
+ *
+ * \note Source: https://stackoverflow.com/a/52895729/12036073
+ */
+void clear() {
+ std::cout << u8"\033[2J\033[1;1H";
+}
+
+/*!
+ * Sub boxes are like normal boxes, but indented with 4 spaces.
+ */
+void _printSubBoxSpacing() {
+ std::cout << " ";
+}
+
+/* Title boxes */
+
+/*! \param title C-style string, there are no size checks so it could wrap
+ *
+ * Prints the title, surrounded by "===".
+ */
+void titleBox(const char* title) {
+ std::cout << std::endl << "=== " << title << " ===" << std::endl;
+}
+
+/*! \param title C-style string, there are no size checks so it could wrap
+ *
+ * Prints the title, surrounded by "---".
+ */
+void subTitleBox(const char* title) {
+ std::cout << std::endl << "--- " << title << " ---" << std::endl;
+}
+
+/* Status boxes */
+
+/*! \param message C-style string, there are no size checks so it could wrap
+ *
+ * Success boxes start with "<S>" and print the message.
+ *
+ * \remark Success/Warning/Error boxes start with "<C>" where C is an appropriate character.
+ */
+void successBox(const char* message) {
+ std::cout << "<S> " << message << std::endl;
+}
+
+/*!
+ * \copydetails successBox()
+ * \sa _printSubBoxSpacing()
+ */
+void successSubBox(const char* message) {
+ _printSubBoxSpacing();
+ successBox(message);
+}
+
+/*! \param message C-style string, there are no size checks so it could wrap
+ *
+ * Warning boxes start with "<W>" and print the message.
+ *
+ * \remark Success/Warning/Error boxes start with "<C>" where C is an appropriate character.
+ */
+void warningBox(const char* message) {
+ std::cout << "<W> " << message << std::endl;
+}
+
+/*!
+ * \copydetails warningBox()
+ * \sa _printSubBoxSpacing()
+ */
+void warningSubBox(const char* message) {
+ _printSubBoxSpacing();
+ warningBox(message);
+}
+
+/*! \param message C-style string, there are no size checks so it could wrap
+ *
+ * Error boxes start with "<E>" and print the message.
+ *
+ * \remark Success/Warning/Error boxes start with "<C>" where C is an appropriate character.
+ */
+void errorBox(const char* message) {
+ std::cout << "<E> " << message << std::endl;
+}
+
+/*!
+ * \copydetails errorBox()
+ * \sa _printSubBoxSpacing()
+ */
+void errorSubBox(const char* message) {
+ _printSubBoxSpacing();
+ errorBox(message);
+}
+
+/* Input line boxes */
+
+/*! \param label C-style string, there are no size checks so it could wrap
+ *
+ * Prints "(+)" and then the given label.
+ * It doesn't create spacing after label, so it should exist in the label itself.
+ *
+ * \remark Semi-internal means the function is defined in the header out of necessity (for templated functions)
+ */
+void _printInputBoxLabel(const char* label) {
+ std::cout << "(+) " << label;
+}
+
+void __inputLineBoxGetInput(char* output, unsigned maxWidth, bool ignore) {
+ if (ignore)
+ std::cin.ignore();
+ std::cin.getline(output, maxWidth);
+}
+
+/*! \param label C-style string, there are no size check so it could wrap
+ * \param output Pointer to a char array
+ * \param maxWidth Maximum count of characters to read from user input. output MUST be able to hold that many characters!
+ * \param ignore Whether or not to ignore the first new-line delimiter.
+ * true by default, should be set to false only when an inputLineBox/SubBox has been issued directly prior or when it's the very first issued box command.
+ *
+ * Prints "(+)" before the label.
+ */
+void inputLineBox(const char* label, char* output, unsigned maxWidth, bool ignore) {
+ _printInputBoxLabel(label);
+ __inputLineBoxGetInput(output, maxWidth, ignore);
+}
+
+/*!
+ * \copydetails inputLineBox()
+ * \sa _printSubBoxSpacing()
+ */
+void inputLineSubBox(const char* label, char* output, unsigned maxWidth, bool ignore) {
+ _printSubBoxSpacing();
+ _printInputBoxLabel(label);
+ __inputLineBoxGetInput(output, maxWidth, ignore);
+}
+
+/*! \param ignore Whether or not to ignore the first new-line delimiter.
+ * true by default, should be set to false only when an inputLineBox/SubBox has been issued directly prior or when it's the very first issued box command.
+ *
+ * Prints a "Press enter to continue" message and waits for the user
+ */
+void pressEnterToContinue(bool ignore) {
+ char tmp;
+ inputLineBox("[Press enter to continue]", &tmp, 1, ignore);
+}
+
+/* Ordered list */
+
+int orderedListIndex = 1;
+
+/*!
+ * Prints the latests ordered list index, surrounded by square brackets, and increments the ordered list index.
+ *
+ * \remark There are no size checks, so list index could overflow.
+ *
+ * \remark Semi-internal means the function is defined in the header out of necessity (for templated functions)
+ */
+
+void _printOrderedListBeginning() {
+ std::cout << "[" << orderedListIndex++ << "] ";
+}
+
+/*! \param starter Sets the starting number by which ordered list will enumerate
+ *
+ * \remark Although starter can be a negative number, ordered list will always increment up the number.
+ */
+void resetOrderedList(int starter) {
+ orderedListIndex = starter;
+}
+
+/* Table */
+
+void __printTableHeaderNumber(unsigned number) {
+ std::cout << ":" << ((number < 100) ? " " : "") << number << ((number < 10) ? " " : "");
+}
+
+void __printTableColumnHeader(unsigned start, unsigned count) {
+ std::cout << ": ";
+ for (unsigned i = 0; i < count; i++)
+ __printTableHeaderNumber(start++);
+ std::cout << ":" << std::endl;
+
+ for (unsigned i = 0; i <= count; i++)
+ std::cout << "====";
+ std::cout << "=" << std::endl;
+}
+
+void __printTableLine(unsigned columns) {
+ std::cout << ":===";
+ for (unsigned i = 0; i < columns; i++)
+ std::cout << "+---";
+ std::cout << ":" << std::endl;
+}
+
+/*! \param startNumber The number by which column and row enumeration begins
+ * \param columns How many columns the table should have
+ * \param items C-style string, each cell is a single character from the string
+ *
+ * Prints a string as a grid/table of characters from top to bottom, left to right.
+ * The first character is on the top left, the last one is on the bottom right.
+ *
+ * Table is printed until a terminating zero is encountered.
+ * The rows are "calculated" from the columns count and the items length.
+ */
+void table(unsigned startNumber, unsigned columns, const char* items) {
+ __printTableColumnHeader(startNumber, columns);
+
+ __printTableHeaderNumber(startNumber);
+ for (unsigned i = 0, colInd = 0, rowInd = 0; items[i] != '\0'; i++, (++colInd) %= columns) {
+ if (colInd == 0 && i != 0) {
+ std::cout << ":" << std::endl;
+ __printTableLine(columns);
+ __printTableHeaderNumber((++rowInd) + startNumber);
+ }
+
+ std::cout << ((colInd == 0) ? "║ " : "| ") << items[rowInd * columns + colInd] << " ";
+ }
+ std::cout << ":" << std::endl;
+}
+
diff --git a/Year 1/Object-oriented Programming/Project 1/src/Generic/ConsoleInterface/Toolbox.hpp b/Year 1/Object-oriented Programming/Project 1/src/Generic/ConsoleInterface/Toolbox.hpp
new file mode 100644
index 0000000..91306cb
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Project 1/src/Generic/ConsoleInterface/Toolbox.hpp
@@ -0,0 +1,115 @@
+#ifndef HEADER_CONSOLEINTERFACE_TOOLBOX
+#define HEADER_CONSOLEINTERFACE_TOOLBOX
+
+#include <iostream>
+
+/*! \file Toolbox.hpp
+ * \brief Stores a wide range of functions for simpler/more automated printing.
+ *
+ * Adds a lot of functions for printing.
+ *
+ * \remark iostream is included by necessity (templated functions), you should only use the provided functions, if you can.
+ */
+
+#define MAX_LINE_WIDTH 1024
+
+//! Clears the console screen
+void clear();
+//! Prints a title box
+void titleBox(const char* title);
+//! Prints a title sub box
+void subTitleBox(const char* title);
+
+//! Prints a success box
+void successBox(const char* message);
+//! Prints a success box as a sub box
+void successSubBox(const char* message);
+//! Prints a warning box
+void warningBox(const char* message);
+//! Prints a warning box as a sub box
+void warningSubBox(const char* message);
+//! Prints an error box
+void errorBox(const char* message);
+//! Prints an error box as a sub box
+void errorSubBox(const char* message);
+
+//! Prints label, gets a whole line of input and stores it to output
+void inputLineBox(const char* label, char* output, unsigned maxWidth, bool ignore = true);
+//! Prints label as a sub box, gets a whole line of input and stores it to output
+void inputLineSubBox(const char* label, char* output, unsigned maxWidth, bool ignore = true);
+//! Waits for user to press enter
+void pressEnterToContinue(bool ignore = true);
+
+//! Resets the ordered list starting number
+void resetOrderedList(int starter = 1);
+
+//! Prints a string as a table
+void table(unsigned startNumber, unsigned columns, const char* items);
+
+//! Semi-internal function that prints the spacing for sub boxes
+void _printSubBoxSpacing();
+//! Semi-internal function that prints the label of an input box
+void _printInputBoxLabel(const char* label);
+//! Semi-internal function that prints the latest ordered list index
+void _printOrderedListBeginning();
+
+//! Reads user input and stores it
+template <typename T>
+void read(T* storage) {
+ std::cin >> storage;
+}
+
+//! Reads user input and stores it
+template <typename T>
+void read(T& storage) {
+ std::cin >> storage;
+}
+
+//! Prints given item
+template <typename T>
+void print(const T* item) {
+ std::cout << item;
+}
+
+//! Prints given item
+template <typename T>
+void print(const T& item) {
+ std::cout << item;
+}
+
+//! Prints given item and an endline character
+template <typename T>
+void printLine(const T* item) {
+ std::cout << item << std::endl;
+}
+
+//! Prints given item and an endline character
+template <typename T>
+void printLine(const T& item) {
+ std::cout << item << std::endl;
+}
+
+//! Prints the element as the latest list item
+template <typename T>
+void printOrderedListElem(const T& elem) {
+ _printOrderedListBeginning();
+ printLine(elem);
+}
+
+//! Prints a label and then reads user input and stores it
+template <typename T>
+void inputBox(const char* label, T* output) {
+ _printInputBoxLabel(label);
+ std::cin >> *output;
+}
+
+//! Prints a label with sub box spacing and then reads user input and stores it
+//! \sa _printSubBoxSpacing()
+template <typename T>
+void inputSubBox(const char* label, T* output) {
+ _printSubBoxSpacing();
+ _printInputBoxLabel(label);
+ std::cin >> *output;
+}
+
+#endif
diff --git a/Year 1/Object-oriented Programming/Project 1/src/Generic/Date/Date.cpp b/Year 1/Object-oriented Programming/Project 1/src/Generic/Date/Date.cpp
new file mode 100644
index 0000000..7c8f4d9
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Project 1/src/Generic/Date/Date.cpp
@@ -0,0 +1,174 @@
+#include "Date.h"
+#include <cstring>
+
+/* Private */
+
+bool Date::isLeapYear() {
+ // Implements https://en.wikipedia.org/wiki/Leap_year#Gregorian_calendar
+ return year % 400 == 0 || year % 100 != 0 || year % 4 == 0;
+}
+
+unsigned short Date::daysInMonth() {
+ if (month == 2)
+ return 28 + isLeapYear();
+ if (month <= 7)
+ return 30 + month % 2;
+ return 31 - month % 2;
+}
+
+/* Public */
+
+Date::Date() : Date(01, 05, 2022) {}
+
+/*! \param str C-style string
+ *
+ * Values are initialized with zeroes. Then it parses the string:
+ *
+ * Skips spaces, saves day with atoi, skips two characters,
+ * skips spaces, saves month with atoi, skips two characters,
+ * skips spaces, saves year with atoi.
+ *
+ * Values are validated with setters.
+ * You should always have spaces between day, month and year and no non-numeric characters between them.
+ *
+ * \remark If there are non-numeric characters, as per atoi() spec, the appropriate property is zero.
+ */
+Date::Date(const char* str) : Date(0, 0, 0) {
+ while (*str == ' ' && *str != '\0') str++;
+ set_day(atoi(str));
+ str += 2;
+ while (*str == ' ' && *str != '\0') str++;
+ set_month(atoi(str));
+ str += 2;
+ while (*str == ' ' && *str != '\0') str++;
+ set_year(atoi(str));
+}
+
+/*!
+ * Initializes values with zeroes and then uses setters to save the function arguments.
+ */
+Date::Date(unsigned short day, unsigned short month, unsigned short year) {
+ this->day = this->month = this->year = 0;
+ set_day(day);
+ set_month(month);
+ set_year(year);
+}
+
+unsigned short Date::get_day() const {
+ return day;
+}
+
+/*! \return Whether newDay was saved or not
+ *
+ * If newDay is equal to 0, or is more than the appropriate days in the month, the day isn't set.
+ */
+bool Date::set_day(unsigned short newDay) {
+ if (newDay == 0 || newDay > daysInMonth())
+ return false;
+ day = newDay;
+ return true;
+}
+
+unsigned short Date::get_month() const {
+ return month;
+}
+
+/*! \return Whether newMonth was saved or not
+ *
+ * If newMonth is equal to 0 or is more than 12, the month isn't set.
+ */
+bool Date::set_month(unsigned short newMonth) {
+ if (newMonth == 0 || newMonth > 12)
+ return false;
+ month = newMonth;
+ return true;
+}
+
+unsigned short Date::get_year() const {
+ return year;
+}
+
+/*! \return Whether year was saved or not
+ *
+ * Year isn't set when newYear is zero
+ */
+bool Date::set_year(unsigned short newYear) {
+ if (newYear == 0)
+ return false;
+ year = newYear;
+ return true;
+}
+
+int digitToChar(unsigned digit) {
+ if (digit <= 9)
+ return '0' + digit;
+ return ' ';
+}
+
+/*! \return String object in the format "day.month.year"
+ *
+ * \note Currently there isn't any way to change the formatting
+ */
+String Date::createString() const {
+ String out;
+ return ((((out += day) += ".") += month) += ".") += year;
+}
+
+/*! \param istr An input stream
+ *
+ * Directly reads bytes from stream (calls read() function).
+ *
+ * \remark Doesn't alter the stream in any other way.
+ * \note Best used with binary ifstream
+ */
+void Date::read(std::istream& istr) {
+ istr.read((char*)&day, sizeof(day));
+ istr.read((char*)&month, sizeof(month));
+ istr.read((char*)&year, sizeof(year));
+}
+
+/*! \param ostr An output stream
+ *
+ * Directly writes bytes to stream (calls write() function).
+ *
+ * \remark Doesn't alter the stream in any other way.
+ * \note Best used with binary ofstream
+ */
+void Date::write(std::ostream& ostr) {
+ ostr.write((char*)&day, sizeof(day));
+ ostr.write((char*)&month, sizeof(month));
+ ostr.write((char*)&year, sizeof(year));
+}
+
+/*! \returns -1 if this < other, 1 if this > other, 0 if this == other
+ */
+int Date::compare(const Date& other) const {
+ if (year < other.year || month < other.month || day < other.day)
+ return -1;
+ return year > other.year || month > other.month || day > other.day;
+}
+
+
+/* Outside of class */
+
+/*!
+ * Uses the stream's >> operator to read and parse the day, month and year.
+ *
+ * \note Best used with std::cin or text std::ifstream
+ */
+std::istream& operator>>(std::istream& istr, Date& dt) {
+ unsigned short d, M, y;
+ istr >> d >> M >> y;
+ dt = Date(d, M, y);
+ return istr;
+}
+
+/*!
+ * Uses the stream's << operator to write the date in the format "day.month.year"
+ *
+ * \note Best used with std::cout or text std::ofstream
+ */
+std::ostream& operator<<(std::ostream& ostr, const Date& dt) {
+ ostr << dt.get_day() << "." << dt.get_month() << "." << dt.get_year();
+ return ostr;
+}
diff --git a/Year 1/Object-oriented Programming/Project 1/src/Generic/Date/Date.h b/Year 1/Object-oriented Programming/Project 1/src/Generic/Date/Date.h
new file mode 100644
index 0000000..89cc89c
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Project 1/src/Generic/Date/Date.h
@@ -0,0 +1,59 @@
+#ifndef HEADER_DATE
+#define HEADER_DATE
+
+/*! \file Date.h
+ * \brief Stores the declaration of class Date
+ */
+
+#include <istream>
+#include <ostream>
+#include "../String/String.h"
+
+/*! \brief Stores a day, month and year
+ */
+class Date {
+ unsigned short day;
+ unsigned short month;
+ unsigned short year;
+
+ bool isLeapYear();
+ unsigned short daysInMonth();
+
+public:
+ //! Sets date as 01 May 2022
+ Date();
+ //! Parses string
+ Date(const char* str);
+ //! Sets day, month and year
+ Date(unsigned short day, unsigned short month, unsigned short year);
+
+ //! Returns the day
+ unsigned short get_day() const;
+ //! Sets the day
+ bool set_day(unsigned short day);
+ //! Returns the month
+ unsigned short get_month() const;
+ //! Sets the month
+ bool set_month(unsigned short month);
+ //! Returns the year
+ unsigned short get_year() const;
+ //! Sets the year
+ bool set_year(unsigned short year);
+
+ //! Returns the date as a String object
+ String createString() const;
+
+ //! Reads date from stream
+ void read(std::istream& istr);
+ //! Writes date to stream
+ void write(std::ostream& ostr);
+ //! Compares two dates
+ int compare(const Date& other) const;
+};
+
+//! Reads date from stream with >> operator
+std::istream& operator>>(std::istream& istr, Date& dt);
+//! Writes date to stream with << operator
+std::ostream& operator<<(std::ostream& ostr, const Date& dt);
+
+#endif
diff --git a/Year 1/Object-oriented Programming/Project 1/src/Generic/List/List.hpp b/Year 1/Object-oriented Programming/Project 1/src/Generic/List/List.hpp
new file mode 100644
index 0000000..0b53033
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Project 1/src/Generic/List/List.hpp
@@ -0,0 +1,345 @@
+#ifndef HEADER_LIST
+#define HEADER_LIST
+
+/*! \file List.hpp
+ * \brief Stores declaration and definition of templated class List
+ */
+
+#include <istream>
+#include <ostream>
+
+/*! \brief Templated class that stores an array of elements in dynamic memory
+ *
+ * \warning findIndex(), contain(), read(), write(), operator<<() and operator>>() require the type to have defined a couple of functions.
+ * \sa findIndex()
+ * \sa contain()
+ * \sa read()
+ * \sa write()
+ * \sa operator<<()
+ * \sa operator>>()
+ */
+template <typename T>
+class List {
+protected:
+ T* elements;
+ unsigned length;
+ unsigned count;
+
+ void resize();
+ void free();
+ void copyFrom(const List& other);
+
+public:
+ //! Copies all elements in given array
+ List(const T* elements, unsigned elementsCount);
+ //! Adds an element
+ void add(const T& element);
+ //! Inserts element at given index
+ void insertAt(const T& element, unsigned index);
+ //! Removes element at index
+ bool removeAt(unsigned index);
+ //! Finds the index of element
+ unsigned findIndex(const T& element) const;
+ bool contain(const T& element) const;
+ //! Returns reference to element at index
+ T& operator[](unsigned index);
+ //! Returns constant reference to element at index
+ const T& operator[](unsigned index) const;
+
+ //! Appends elements from other list
+ List<T>& operator+=(const List<T> other);
+
+ //! Reads from stream
+ std::istream& read(std::istream& istr);
+ //! Writes to stream
+ std::ostream& write(std::ostream& ostr) const;
+
+ //! Returns the length
+ unsigned get_length() const;
+ //! Returns the count
+ unsigned get_count() const;
+
+ List();
+ List& operator=(const List& other);
+ List(const List& other);
+ ~List();
+
+ List(List&& other);
+ List& operator=(List&& other);
+};
+
+//! Reads List from stream with >> operator
+template <typename T>
+std::istream& operator>>(std::istream& istr, List<T>& obj);
+//! Writes List to stream with << operator
+template <typename T>
+std::ostream& operator<<(std::ostream& ostr, const List<T>& obj);
+
+/* Private */
+
+template <typename T>
+void List<T>::resize() {
+ length = (length == 0) ? 8 : length << 1;
+ T* temp = new T[length];
+ for (int i = 0; i < count; i++)
+ temp[i] = elements[i];
+ delete[] elements;
+ elements = temp;
+}
+
+template <typename T>
+void List<T>::free() {
+ delete[] elements;
+}
+
+template <typename T>
+void List<T>::copyFrom(const List& other) {
+ elements = new T[other.length];
+ for (int i = 0; i < other.count; i++)
+ elements[i] = other.elements[i];
+ length = other.length;
+ count = other.count;
+}
+
+/* Public */
+
+template <typename T>
+List<T>::List(const T* elements, unsigned elementsCount) {
+ length = 8;
+ count = elementsCount;
+ this->elements = new T[length];
+ for (unsigned i = 0; i < count; i++)
+ this->elements[i] = elements[i];
+}
+
+/*!
+ * Resizes internal array if there is no space for additional elements.
+ */
+template <typename T>
+void List<T>::add(const T& element) {
+ if (length == count) resize();
+
+ elements[count++] = element;
+}
+
+/*!
+ * If index is after the last element, the element is just added.
+ * Otherwise all elements after the index are shifted right and element is put in place.
+ *
+ * Resizes internal array if there is no space for additional elements.
+ */
+template <typename T>
+void List<T>::insertAt(const T& element, unsigned index) {
+ if (index >= count) {
+ add(element);
+ return;
+ }
+ if (length == count) resize();
+
+ for (unsigned i = count; i > index; i--)
+ elements[i] = elements[i - 1];
+
+ elements[index] = element;
+ count++;
+}
+
+/*! \returns Wether element could be removed
+ *
+ * If index is after that of the last element, nothing is done and false is returned.
+ * Otherwise elements after index are shifted right and count is reduced.
+ */
+template <typename T>
+bool List<T>::removeAt(unsigned index) {
+ if (index >= count) return false;
+
+ for (int i = index; i < count; i++)
+ elements[i] = elements[i + 1];
+ count--;
+ return true;
+}
+
+/*! \returns Index of element. If element isn't found, returns the count of element.
+ *
+ * \warning The function depends on the type having a function "compare" defined, which takes two elements and returns a number <0 if elem1 < elem2, >0 if elem1 > elem2, 0 if elem1 == elem2
+ * \note Searching is done linearly
+ */
+template <typename T>
+unsigned List<T>::findIndex(const T& element) const {
+ unsigned ind = 0;
+ while (ind < count && elements[ind].compare(element) != 0)
+ ind++;
+ return ind;
+}
+
+/*! \returns Whether the element is contained in the current List
+ *
+ * \warning Function depends on findIndex(), which means the same "compare" function must be defined in the type
+ * \sa findIndex()
+ */
+template <typename T>
+bool List<T>::contain(const T& element) const {
+ return findIndex(element) < count;
+}
+
+template <typename T>
+T& List<T>::operator[](unsigned index) {
+ return elements[index];
+}
+
+template <typename T>
+const T& List<T>::operator[](unsigned index) const {
+ return elements[index];
+}
+
+template <typename T>
+List<T>& List<T>::operator+=(const List<T> other) {
+ for (unsigned i = 0; i < other.length; i++)
+ add(other[i]);
+ return *this;
+}
+
+/*! \param istr An input stream
+ *
+ * Directly reads bytes from stream (calls read() function).
+ * Any stored values are deleted and replaced with those from the stream.
+ *
+ * \warning The function depends on the type having a function "read" defined, which takes an std::istream& and writes it's data to it. Return type doesn't matter.
+ * \remark Doesn't alter the stream in any other way.
+ * \note Best used with binary ifstream
+ */
+template <typename T>
+std::istream& List<T>::read(std::istream& istr) {
+ istr.read((char*)&length, sizeof(length));
+ istr.read((char*)&count, sizeof(count));
+
+ delete[] elements;
+ elements = new T[length];
+
+ for (int i = 0; i < count; i++)
+ elements[i].read(istr);
+
+ return istr;
+}
+
+/*! \param ostr An output stream
+ *
+ * Directly writes bytes to stream (calls write() function).
+ *
+ * \warning The function depends on the type having a function "write" defined, which takes an std::ostream& and writes it's data to it. Return type doesn't matter.
+ * \remark Doesn't alter the stream in any other way.
+ * \note Best used with binary ofstream
+ */
+template <typename T>
+std::ostream& List<T>::write(std::ostream& ostr) const {
+ ostr.write((const char*)&length, sizeof(length));
+ ostr.write((const char*)&count, sizeof(count));
+
+ for (int i = 0; i < count; i++)
+ elements[i].write(ostr);
+
+ return ostr;
+}
+
+/*!
+ * Length is the size of the underlying array (allocated memory).
+ */
+template <typename T>
+unsigned List<T>::get_length() const {
+ return length;
+}
+
+/*!
+ * Count is the amount of elements that are stored.
+ */
+template <typename T>
+unsigned List<T>::get_count() const {
+ return count;
+}
+
+// Rule of 4
+
+template <typename T>
+List<T>::List() : List(nullptr, 0) {}
+
+template <typename T>
+List<T>& List<T>::operator=(const List& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+template <typename T>
+List<T>::List(const List& other) {
+ copyFrom(other);
+}
+
+template <typename T>
+List<T>::~List() {
+ free();
+}
+
+// Move semantics
+
+template <typename T>
+List<T>::List(List&& other) {
+ length = other.length;
+ count = other.count;
+ elements = other.elements;
+ other.elements = nullptr;
+}
+
+template <typename T>
+List<T>& List<T>::operator=(List&& other) {
+ if (this != &other) {
+ free();
+ length = other.length;
+ count = other.count;
+ elements = other.elements;
+ other.elements = nullptr;
+ }
+ return *this;
+}
+
+/* Outside of class */
+
+/*!
+ * Uses the stream's >> operator to read and parse the elements.
+ * The first item in the stream should be the count.
+ *
+ * \warning The function depends on the type having the operator >> defined, which takes an std::istream& and writes it's data to it. Return type doesn't matter.
+ * \note Best used with std::cin or text std::ifstream
+ */
+template <typename T>
+std::istream& operator>>(std::istream& istr, List<T>& obj) {
+ List<T> newObj;
+ unsigned count;
+ istr >> count;
+
+ T temp;
+ for (int i = 0; i < count; i++) {
+ istr >> temp;
+ obj.add(temp);
+ }
+
+ return istr;
+}
+
+/*!
+ * Uses the stream's << operator to write the count and then all objects.
+ *
+ * \warning The function depends on the type having the operator << defined, which takes an std::ostream& and writes it's data to it. Return type doesn't matter.
+ * \note Best used with std::cout or text std::ofstream
+ */
+template <typename T>
+std::ostream& operator<<(std::ostream& ostr, const List<T>& obj) {
+ ostr << obj.get_count() << std::endl;
+ for (int i = 0; i < obj.get_count(); i++)
+ ostr << obj[i];
+
+ return ostr;
+}
+
+
+#endif
diff --git a/Year 1/Object-oriented Programming/Project 1/src/Generic/List/OrderedList.hpp b/Year 1/Object-oriented Programming/Project 1/src/Generic/List/OrderedList.hpp
new file mode 100644
index 0000000..8d13a5d
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Project 1/src/Generic/List/OrderedList.hpp
@@ -0,0 +1,55 @@
+#ifndef HEADER_ORDEREDLIST
+#define HEADER_ORDEREDLIST
+
+/*! \file OrderedList.hpp
+ * \brief Stores declaration and definition of templated class OrderedList
+ */
+
+#include "List.hpp"
+
+/*! \brief Inherits List, but contains it's elements in a sorted manner
+ *
+ * \warning Outside those defined in List, insert() requires the type to have defined a "compare" of functions.
+ * \sa insert()
+ */
+template <typename T>
+class OrderedList : public List<T> {
+public:
+ OrderedList();
+ OrderedList(const T* elements, unsigned elementsCount);
+
+ /*! \brief \warning Function is deleted, use insert() instead
+ * \sa insert()
+ */
+ void add(T element) = delete;
+ /*! \brief \warning Function is deleted, use insert() instead
+ * \sa insert()
+ */
+ void insertAt(T element, unsigned index) = delete;
+
+ //! Inserts an element at a sorted position
+ void insert(T element);
+};
+
+template <typename T>
+OrderedList<T>::OrderedList() : OrderedList(nullptr, 0) {}
+
+template <typename T>
+OrderedList<T>::OrderedList(const T* elements, unsigned elementsCount) : List<T>::List(elements, elementsCount) {}
+
+/*!
+ * \warning The function depends on the type having a function "compare" defined, which takes two elements and returns a number <0 if elem1 < elem2, >0 if elem1 > elem2, 0 if elem1 == elem2
+ * \note Insertion is done linearly
+ */
+template <typename T>
+void OrderedList<T>::insert(T element) {
+ if (List<T>::length == List<T>::count) List<T>::resize();
+
+ unsigned insertionInd = 0;
+ while (insertionInd < List<T>::count && List<T>::elements[insertionInd].compare(element) < 0)
+ insertionInd++;
+
+ List<T>::insertAt(element, insertionInd);
+}
+
+#endif
diff --git a/Year 1/Object-oriented Programming/Project 1/src/Generic/String/String.cpp b/Year 1/Object-oriented Programming/Project 1/src/Generic/String/String.cpp
new file mode 100644
index 0000000..9d61999
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Project 1/src/Generic/String/String.cpp
@@ -0,0 +1,169 @@
+#include "String.h"
+#include <cstring>
+
+/* Private */
+
+void String::free() {
+ delete[] str;
+}
+
+void String::copyFrom(const String& other) {
+ str = new char[other.length + 1];
+ strncpy(str, other.str, other.length + 1);
+ length = other.length;
+}
+
+/* Public */
+
+/*!
+ * Allocated with length + 1 size, so there is a terminating zero at the end.
+ */
+String::String(unsigned length) {
+ this->length = length;
+ str = new char[length + 1];
+ str[length] = '\0';
+}
+
+/*! \params str C-style string
+ */
+String::String(const char* str) {
+ length = strlen(str);
+ this->str = new char[length + 1]; // capture terminating zero
+ strncpy(this->str, str, length + 1);
+}
+
+const char* String::get_cstr() const {
+ return str;
+}
+
+unsigned String::get_length() const {
+ return length;
+}
+
+char& String::operator[](unsigned index) {
+ return str[index];
+}
+
+/*! \params str C-style string
+ */
+String& String::operator+=(const char* str) {
+ unsigned strLen = strlen(str);
+ char* newStr = new char[length + strLen + 1];
+ strncpy(newStr, this->str, length + 1);
+ strncat(newStr, str, strLen + 1);
+
+ delete[] this->str;
+ this->str = newStr;
+ length += strLen;
+
+ return *this;
+}
+
+/*!
+ * Converts the number to a C-style string and then uses += to append it.
+ */
+String& String::operator+=(unsigned number) {
+ unsigned tempLen = 0;
+ for (unsigned tmp = number; tmp > 0 || tempLen == 0; tmp /= 10)
+ tempLen++;
+
+ char* tmp = new char[tempLen + 1];
+ tmp[tempLen] = '\0';
+ for (unsigned i = tempLen - 1; i < tempLen; i--, number /= 10)
+ tmp[i] = number % 10 + '0';
+
+ *this += tmp;
+ delete[] tmp;
+ return *this;
+}
+
+String::String() : String("") {}
+
+String::String(const String& other) {
+ copyFrom(other);
+}
+
+String& String::operator=(const String& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+String::~String() {
+ free();
+}
+
+String::String(String&& other) {
+ str = other.str;
+ other.str = nullptr;
+ length = other.length;
+}
+
+String& String::operator=(String&& other) {
+ if (this != &other) {
+ delete[] str;
+ str = other.str;
+ other.str = nullptr;
+ length = other.length;
+ }
+ return *this;
+}
+
+/*! \param istr An input stream
+ *
+ * Directly reads bytes from stream (calls read() function).
+ * First reads the string length, then the underlying C-style string (including terminating zero).
+ *
+ * \remark Doesn't alter the stream in any other way.
+ * \note Best used with binary ifstream
+ */
+void String::read(std::istream& istr) {
+ istr.read((char*)&length, sizeof(length));
+ delete[] str;
+ str = new char[length + 1];
+ istr.read(str, sizeof(char) * (length + 1));
+}
+
+/*! \param ostr An output stream
+ *
+ * Directly writes bytes to stream (calls write() function).
+ * First writes the string length, then the underlying C-style string (including terminating zero).
+ *
+ * \remark Doesn't alter the stream in any other way.
+ * \note Best used with binary ofstream
+ */
+void String::write(std::ostream& ostr) const {
+ ostr.write((char*)&length, sizeof(length));
+ ostr.write(str, sizeof(char) * (length + 1));
+}
+
+/*! \params other C-style string
+ * \returns strcmp between underlying C-style string and "other"
+ */
+int String::compare(const String& other) const {
+ return strcmp(str, other.str);
+}
+
+/*!
+ * Uses the stream's getline function to read the data.
+ *
+ * \warning It takes at most 1024 characters from the stream!
+ * \note Best used with std::cin or text std::ifstream
+ */
+std::istream& operator>>(std::istream& istr, String& event) {
+ istr.getline(event.str, 1024);
+ event.length = strlen(event.str);
+
+ return istr;
+}
+
+/*!
+ * Uses the stream's << operator to write the underlying C-style string
+ *
+ * \note Best used with std::cout or text std::ofstream
+ */
+std::ostream& operator<<(std::ostream& ostr, const String& event) {
+ return ostr << event.str;
+}
diff --git a/Year 1/Object-oriented Programming/Project 1/src/Generic/String/String.h b/Year 1/Object-oriented Programming/Project 1/src/Generic/String/String.h
new file mode 100644
index 0000000..84fbe9c
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Project 1/src/Generic/String/String.h
@@ -0,0 +1,56 @@
+#ifndef HEADER_STRING
+#define HEADER_STRING
+
+/*! \file String.h
+ * \brief Stores declaration of class String
+ */
+
+#include <istream>
+#include <ostream>
+
+class String {
+ char* str;
+ unsigned length;
+
+ void free();
+ void copyFrom(const String& other);
+
+public:
+ //! The underlying array is allocated with length (+ 1) size
+ String(unsigned length);
+ //! Copies str
+ String(const char* str);
+ //! Returns internal C-style string
+ const char* get_cstr() const;
+ //! Returns number of characters in string
+ unsigned get_length() const;
+ //! Returns a character reference (from the underlying C-style string) at the given index
+ char& operator[](unsigned index);
+
+ //! Appends a C-style string
+ String& operator+=(const char* str);
+ //! Appends a number
+ String& operator+=(unsigned number);
+
+ String();
+ String(const String& other);
+ String& operator=(const String& other);
+ ~String();
+
+ String(String&& other);
+ String& operator=(String&& other);
+
+ //! Reads String from stream
+ void read(std::istream& istr);
+ //! Writes String to stream
+ void write(std::ostream& ostr) const;
+ //! Compares two strings
+ int compare(const String& other) const;
+
+ //! Reads String from stream with >> operator
+ friend std::istream& operator>>(std::istream& istr, String& event);
+ //! Writes String to stream with << operator
+ friend std::ostream& operator<<(std::ostream& ostr, const String& event);
+};
+
+#endif