From b0f046461b44a0c4002ea667a726403ff4a1a701 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Wed, 4 May 2022 10:58:48 +0300 Subject: Moved templated stuff into hpp files and added a blank main.cpp file --- src/ConsoleInterface/Command.cpp | 5 +- src/ConsoleInterface/Command.h | 1 + src/ConsoleInterface/Menu.cpp | 2 +- src/ConsoleInterface/Menu.h | 2 +- src/ConsoleInterface/Toolbox.cpp | 43 -------- src/ConsoleInterface/Toolbox.h | 25 ----- src/ConsoleInterface/Toolbox.hpp | 47 +++++++++ src/EventSystem/Event.cpp | 2 +- src/EventSystem/Event.h | 2 +- src/EventSystem/EventSystem.h | 2 +- src/EventSystem/Reservation.cpp | 8 ++ src/EventSystem/Reservation.h | 2 +- src/List/List.cpp | 169 ------------------------------- src/List/List.h | 42 -------- src/List/List.hpp | 211 +++++++++++++++++++++++++++++++++++++++ src/List/OrderedList.cpp | 18 ---- src/List/OrderedList.h | 19 ---- src/List/OrderedList.hpp | 36 +++++++ src/main.cpp | 3 + 19 files changed, 316 insertions(+), 323 deletions(-) delete mode 100644 src/ConsoleInterface/Toolbox.cpp delete mode 100644 src/ConsoleInterface/Toolbox.h create mode 100644 src/ConsoleInterface/Toolbox.hpp delete mode 100644 src/List/List.cpp delete mode 100644 src/List/List.h create mode 100644 src/List/List.hpp delete mode 100644 src/List/OrderedList.cpp delete mode 100644 src/List/OrderedList.h create mode 100644 src/List/OrderedList.hpp create mode 100644 src/main.cpp (limited to 'src') diff --git a/src/ConsoleInterface/Command.cpp b/src/ConsoleInterface/Command.cpp index da8915f..d3d058c 100644 --- a/src/ConsoleInterface/Command.cpp +++ b/src/ConsoleInterface/Command.cpp @@ -1,13 +1,16 @@ #include "Command.h" #include +Command::Command() : Command("", nullptr) {} + Command::Command(const char* nameInMenu, void (*exec)()) { strcpy(this->nameInMenu, nameInMenu); this->exec = exec; } void Command::run() const { - exec(); + if (exec != nullptr) + exec(); } const char* Command::get_nameInMenu() const { diff --git a/src/ConsoleInterface/Command.h b/src/ConsoleInterface/Command.h index fba011b..d00b277 100644 --- a/src/ConsoleInterface/Command.h +++ b/src/ConsoleInterface/Command.h @@ -6,6 +6,7 @@ class Command { void (*exec)(); public: + Command(); Command(const char* nameInMenu, void (*exec)()); void run() const; diff --git a/src/ConsoleInterface/Menu.cpp b/src/ConsoleInterface/Menu.cpp index 7bd7c5e..333c47a 100644 --- a/src/ConsoleInterface/Menu.cpp +++ b/src/ConsoleInterface/Menu.cpp @@ -1,5 +1,5 @@ #include "Menu.h" -#include "Toolbox.h" +#include "Toolbox.hpp" Menu::Menu(const Command* commands, unsigned commandCount) { menuOptions = List(commands, commandCount); diff --git a/src/ConsoleInterface/Menu.h b/src/ConsoleInterface/Menu.h index 0c0c915..f80636d 100644 --- a/src/ConsoleInterface/Menu.h +++ b/src/ConsoleInterface/Menu.h @@ -2,7 +2,7 @@ #define HEADER_CONSOLEINTERFACE_MENU #include "Command.h" -#include "../List/List.h" +#include "../List/List.hpp" class Menu { List menuOptions; diff --git a/src/ConsoleInterface/Toolbox.cpp b/src/ConsoleInterface/Toolbox.cpp deleted file mode 100644 index c42e0b2..0000000 --- a/src/ConsoleInterface/Toolbox.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "Toolbox.h" -#include - -template -void read(T* storage) { - std::cin >> storage; -} - -template -void read(T& storage) { - std::cin >> storage; -} - -template -void print(const T* item) { - std::cout << item; -} - -template -void printLine(const T* item) { - std::cout << item << std::endl; -} - -template -void print(const T& item) { - std::cout << item; -} - -template -void printLine(const T& item) { - std::cout << item << std::endl; -} - -int orderedListIndex = 1; - -void printOrderedListElem(const char* message) { - std::cout << orderedListIndex << ". "; - printLine(message); -} - -void resetOrderedList(int starter) { - orderedListIndex = starter; -} diff --git a/src/ConsoleInterface/Toolbox.h b/src/ConsoleInterface/Toolbox.h deleted file mode 100644 index 8d13537..0000000 --- a/src/ConsoleInterface/Toolbox.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef HEADER_CONSOLEINTERFACE_TOOLBOX -#define HEADER_CONSOLEINTERFACE_TOOLBOX - -template -void read(T* storage); - -template -void read(T& storage); - -template -void print(const T* item); - -template -void printLine(const T* item); - -template -void print(const T& item); - -template -void printLine(const T& item); - -void printOrderedListElem(const char* message); -void resetOrderedList(int starter = 1); - -#endif diff --git a/src/ConsoleInterface/Toolbox.hpp b/src/ConsoleInterface/Toolbox.hpp new file mode 100644 index 0000000..1af42e2 --- /dev/null +++ b/src/ConsoleInterface/Toolbox.hpp @@ -0,0 +1,47 @@ +#ifndef HEADER_CONSOLEINTERFACE_TOOLBOX +#define HEADER_CONSOLEINTERFACE_TOOLBOX + +#include + +template +void read(T* storage) { + std::cin >> storage; +} + +template +void read(T& storage) { + std::cin >> storage; +} + +template +void print(const T* item) { + std::cout << item; +} + +template +void printLine(const T* item) { + std::cout << item << std::endl; +} + +template +void print(const T& item) { + std::cout << item; +} + +template +void printLine(const T& item) { + std::cout << item << std::endl; +} + +int orderedListIndex = 1; + +void printOrderedListElem(const char* message) { + std::cout << orderedListIndex << ". "; + printLine(message); +} + +void resetOrderedList(int starter = 1) { + orderedListIndex = starter; +} + +#endif diff --git a/src/EventSystem/Event.cpp b/src/EventSystem/Event.cpp index 1452057..450eca9 100644 --- a/src/EventSystem/Event.cpp +++ b/src/EventSystem/Event.cpp @@ -39,7 +39,7 @@ StatusCode Event::buyTicketFromReservation(const Reservation& reservation, const unsigned resInd = reservations.findIndex(reservation); if (resInd == reservations.get_count()) return E_ReservationDoesNotExist; - if (!reservation.checkPassword(password)) return E_WrongReservationPassword; + if (!reservation.validPassword(password)) return E_WrongReservationPassword; reservations.removeAt(resInd); tickets.insert(reservation.get_ticket()); diff --git a/src/EventSystem/Event.h b/src/EventSystem/Event.h index ad3ae89..5b26458 100644 --- a/src/EventSystem/Event.h +++ b/src/EventSystem/Event.h @@ -1,7 +1,7 @@ #ifndef HEADER_EVENTSYSTEM_EVENT #define HEADER_EVENTSYSTEM_EVENT -#include "../List/OrderedList.h" +#include "../List/OrderedList.hpp" #include "../DateTime/DateTime.h" #include "../String/String.h" #include "../Venue/Hall.h" diff --git a/src/EventSystem/EventSystem.h b/src/EventSystem/EventSystem.h index 5c83fd8..79fac10 100644 --- a/src/EventSystem/EventSystem.h +++ b/src/EventSystem/EventSystem.h @@ -1,7 +1,7 @@ #ifndef HEADER_EVENTSYSTEM #define HEADER_EVENTSYSTEM -#include "../List/OrderedList.h" +#include "../List/OrderedList.hpp" #include "../DateTime/DateTime.h" #include "Event.h" diff --git a/src/EventSystem/Reservation.cpp b/src/EventSystem/Reservation.cpp index 4cc716e..26278b2 100644 --- a/src/EventSystem/Reservation.cpp +++ b/src/EventSystem/Reservation.cpp @@ -12,6 +12,14 @@ Reservation::Reservation(const Ticket& ticket, const char* password, const char* strcpy(this->note, note); } +bool Reservation::validPassword(const char* password) const { + return !strcmp(this->password, password); +} + +const Ticket& Reservation::get_ticket() const { + return ticket; +} + void Reservation::read(std::istream& istr) { ticket.read(istr); istr.read(password, sizeof(password)); diff --git a/src/EventSystem/Reservation.h b/src/EventSystem/Reservation.h index a73e719..ff56db6 100644 --- a/src/EventSystem/Reservation.h +++ b/src/EventSystem/Reservation.h @@ -15,7 +15,7 @@ public: Reservation(const Ticket& ticket); Reservation(const Ticket& ticket, const char* password, const char* note); - bool checkPassword(const char* password) const; + bool validPassword(const char* password) const; const Ticket& get_ticket() const; void read(std::istream& istr); diff --git a/src/List/List.cpp b/src/List/List.cpp deleted file mode 100644 index e5b360c..0000000 --- a/src/List/List.cpp +++ /dev/null @@ -1,169 +0,0 @@ -#include "List.h" - -/* Private */ - -template -void List::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 -void List::free() { - delete[] elements; -} - -template -void List::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 -List::List(const T* elements, unsigned elementsCount) { - count = length = 0; - for (unsigned i = 0; i < elementsCount; i++) - add(elements[i]); -} - -template -void List::add(const T& element) { - if (length == count) resize(); - - elements[count++] = element; -} - -template -bool List::insertAt(const T& element, unsigned index) { - if (index >= count) return add(element); - if (length == count) resize(); - - for (unsigned i = count; i > index; i--) - elements[i] = elements[i - 1]; - - elements[index] = element; - count++; -} - -template -bool List::removeAt(unsigned index) { - if (index >= count) return false; - - delete elements[index]; - for (int i = index; i < count; i++) - elements[i] = elements[i + 1]; - elements[count--] = nullptr; -} - -template -unsigned List::findIndex(const T& element) const { - unsigned ind = 0; - while (ind < count && elements[ind].compare(element) != 0) - ind++; - return ind; -} - -template -bool List::contain(const T& element) const { - return findIndex(element) < count; -} - -template -T& List::operator[](unsigned index) { - return elements[index]; -} - -template -const T& List::operator[](unsigned index) const { - return elements[index]; -} - -template -std::istream& List::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); // disgusting - - return istr; -} - -template -std::ostream& List::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); // disgusting - - return ostr; -} - -template -unsigned List::get_length() const { - return length; -} - -template -unsigned List::get_count() const { - return count; -} - -// Rule of 4 - -template -List::List() : List(nullptr, 0) {} - -template -List& List::operator=(const List& other) { - if (this != &other) { - free(); - copyFrom(other); - } - return *this; -} - -template -List::List(const List& other) { - copyFrom(other); -} - -template -List::~List() { - free(); -} - -// Move semantics - -template -List::List(List&& other) { - length = other.length; - count = other.count; - elements = other.elements; - other.elements = nullptr; -} - -template -List& List::operator=(List&& other) { - if (this != &other) { - free(); - length = other.length; - count = other.count; - elements = other.elements; - other.elements = nullptr; - } - return *this; -} diff --git a/src/List/List.h b/src/List/List.h deleted file mode 100644 index 23e3ee5..0000000 --- a/src/List/List.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef HEADER_LIST -#define HEADER_LIST - -#include "istream" -#include "ostream" - -template -class List { - T* elements; - unsigned length; - unsigned count; - - void resize(); - void free(); - void copyFrom(const List& other); - -public: - List(const T* elements, unsigned elementsCount); - void add(const T& element); - bool insertAt(const T& element, unsigned index); - bool removeAt(unsigned index); - unsigned findIndex(const T& element) const; - bool contain(const T& element) const; - T& operator[](unsigned index); - const T& operator[](unsigned index) const; - - std::istream& read(std::istream& istr); - std::ostream& write(std::ostream& ostr) const; - - unsigned get_length() const; - unsigned get_count() const; - - List(); - List& operator=(const List& other); - List(const List& other); - ~List(); - - List(List&& other); - List& operator=(List&& other); -}; - -#endif diff --git a/src/List/List.hpp b/src/List/List.hpp new file mode 100644 index 0000000..1e75c89 --- /dev/null +++ b/src/List/List.hpp @@ -0,0 +1,211 @@ +#ifndef HEADER_LIST +#define HEADER_LIST + +#include +#include + +template +class List { +protected: + T* elements; + unsigned length; + unsigned count; + + void resize(); + void free(); + void copyFrom(const List& other); + +public: + List(const T* elements, unsigned elementsCount); + void add(const T& element); + void insertAt(const T& element, unsigned index); + bool removeAt(unsigned index); + unsigned findIndex(const T& element) const; + bool contain(const T& element) const; + T& operator[](unsigned index); + const T& operator[](unsigned index) const; + + std::istream& read(std::istream& istr); + std::ostream& write(std::ostream& ostr) const; + + unsigned get_length() const; + unsigned get_count() const; + + List(); + List& operator=(const List& other); + List(const List& other); + ~List(); + + List(List&& other); + List& operator=(List&& other); +}; + +/* Private */ + +template +void List::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 +void List::free() { + delete[] elements; +} + +template +void List::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 +List::List(const T* elements, unsigned elementsCount) { + count = length = 0; + for (unsigned i = 0; i < elementsCount; i++) + add(elements[i]); +} + +template +void List::add(const T& element) { + if (length == count) resize(); + + elements[count++] = element; +} + +template +void List::insertAt(const T& element, unsigned index) { + if (index >= count) add(element); + if (length == count) resize(); + + for (unsigned i = count; i > index; i--) + elements[i] = elements[i - 1]; + + elements[index] = element; + count++; +} + +template +bool List::removeAt(unsigned index) { + if (index >= count) return false; + + for (int i = index; i < count; i++) + elements[i] = elements[i + 1]; + count--; + return true; +} + +template +unsigned List::findIndex(const T& element) const { + unsigned ind = 0; + while (ind < count && elements[ind].compare(element) != 0) + ind++; + return ind; +} + +template +bool List::contain(const T& element) const { + return findIndex(element) < count; +} + +template +T& List::operator[](unsigned index) { + return elements[index]; +} + +template +const T& List::operator[](unsigned index) const { + return elements[index]; +} + +template +std::istream& List::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); // disgusting + + return istr; +} + +template +std::ostream& List::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); // disgusting + + return ostr; +} + +template +unsigned List::get_length() const { + return length; +} + +template +unsigned List::get_count() const { + return count; +} + +// Rule of 4 + +template +List::List() : List(nullptr, 0) {} + +template +List& List::operator=(const List& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +template +List::List(const List& other) { + copyFrom(other); +} + +template +List::~List() { + free(); +} + +// Move semantics + +template +List::List(List&& other) { + length = other.length; + count = other.count; + elements = other.elements; + other.elements = nullptr; +} + +template +List& List::operator=(List&& other) { + if (this != &other) { + free(); + length = other.length; + count = other.count; + elements = other.elements; + other.elements = nullptr; + } + return *this; +} + +#endif diff --git a/src/List/OrderedList.cpp b/src/List/OrderedList.cpp deleted file mode 100644 index 7476574..0000000 --- a/src/List/OrderedList.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "OrderedList.h" - -template -OrderedList::OrderedList() : OrderedList(nullptr, 0) {} - -template -OrderedList::OrderedList(const T* elements, unsigned elementsCount) : List::List(elements, elementsCount) {} - -template -bool OrderedList::insert(const T& element) { - if (List::length == List::count) List::resize(); - - unsigned insertionInd = 0; - while (List::elements[insertionInd].compare(element) < 0 && insertionInd < List::count) - insertionInd++; - - return List::insertAt(element, insertionInd); -} diff --git a/src/List/OrderedList.h b/src/List/OrderedList.h deleted file mode 100644 index e0b104f..0000000 --- a/src/List/OrderedList.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef HEADER_ORDEREDLIST -#define HEADER_ORDEREDLIST - -#include "List.h" - -template -class OrderedList : public List { -public: - OrderedList(); - OrderedList(const T* elements, unsigned elementsCount); - - void add(const T& element) = delete; - bool insertAt(const T& element, unsigned index) = delete; - - bool insert(const T& element); - // unsigned findIndex(const T& element) const; TODO: binary search -}; - -#endif diff --git a/src/List/OrderedList.hpp b/src/List/OrderedList.hpp new file mode 100644 index 0000000..4396d33 --- /dev/null +++ b/src/List/OrderedList.hpp @@ -0,0 +1,36 @@ +#ifndef HEADER_ORDEREDLIST +#define HEADER_ORDEREDLIST + +#include "List.hpp" + +template +class OrderedList : public List { +public: + OrderedList(); + OrderedList(const T* elements, unsigned elementsCount); + + void add(const T& element) = delete; + void insertAt(const T& element, unsigned index) = delete; + + void insert(const T& element); + // unsigned findIndex(const T& element) const; TODO: binary search +}; + +template +OrderedList::OrderedList() : OrderedList(nullptr, 0) {} + +template +OrderedList::OrderedList(const T* elements, unsigned elementsCount) : List::List(elements, elementsCount) {} + +template +void OrderedList::insert(const T& element) { + if (List::length == List::count) List::resize(); + + unsigned insertionInd = 0; + while (List::elements[insertionInd].compare(element) < 0 && insertionInd < List::count) + insertionInd++; + + List::insertAt(element, insertionInd); +} + +#endif diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..af14ed5 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,3 @@ +int main() { + +} -- cgit v1.3.1-26-gb76a