diff options
Moved templated stuff into hpp files and added a blank main.cpp file
17 files changed, 111 insertions, 118 deletions
| -rw-r--r-- | src/ConsoleInterface/Command.cpp | 5 | ||||
| -rw-r--r-- | src/ConsoleInterface/Command.h | 1 | ||||
| -rw-r--r-- | src/ConsoleInterface/Menu.cpp | 2 | ||||
| -rw-r--r-- | src/ConsoleInterface/Menu.h | 2 | ||||
| -rw-r--r-- | src/ConsoleInterface/Toolbox.h | 25 | ||||
| -rw-r--r-- | src/ConsoleInterface/Toolbox.hpp (renamed from src/ConsoleInterface/Toolbox.cpp) | 8 | ||||
| -rw-r--r-- | src/EventSystem/Event.cpp | 2 | ||||
| -rw-r--r-- | src/EventSystem/Event.h | 2 | ||||
| -rw-r--r-- | src/EventSystem/EventSystem.h | 2 | ||||
| -rw-r--r-- | src/EventSystem/Reservation.cpp | 8 | ||||
| -rw-r--r-- | src/EventSystem/Reservation.h | 2 | ||||
| -rw-r--r-- | src/List/List.h | 42 | ||||
| -rw-r--r-- | src/List/List.hpp (renamed from src/List/List.cpp) | 52 | ||||
| -rw-r--r-- | src/List/OrderedList.cpp | 18 | ||||
| -rw-r--r-- | src/List/OrderedList.h | 19 | ||||
| -rw-r--r-- | src/List/OrderedList.hpp | 36 | ||||
| -rw-r--r-- | src/main.cpp | 3 |
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 <cstring> +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<Command>(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<Command> menuOptions; 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 <class T> -void read(T* storage); - -template <class T> -void read(T& storage); - -template <class T> -void print(const T* item); - -template <class T> -void printLine(const T* item); - -template <class T> -void print(const T& item); - -template <class T> -void printLine(const T& item); - -void printOrderedListElem(const char* message); -void resetOrderedList(int starter = 1); - -#endif diff --git a/src/ConsoleInterface/Toolbox.cpp b/src/ConsoleInterface/Toolbox.hpp index c42e0b2..1af42e2 100644 --- a/src/ConsoleInterface/Toolbox.cpp +++ b/src/ConsoleInterface/Toolbox.hpp @@ -1,4 +1,6 @@ -#include "Toolbox.h" +#ifndef HEADER_CONSOLEINTERFACE_TOOLBOX +#define HEADER_CONSOLEINTERFACE_TOOLBOX + #include <iostream> template <class T> @@ -38,6 +40,8 @@ void printOrderedListElem(const char* message) { printLine(message); } -void resetOrderedList(int starter) { +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.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 T> -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.cpp b/src/List/List.hpp index e5b360c..1e75c89 100644 --- a/src/List/List.cpp +++ b/src/List/List.hpp @@ -1,4 +1,44 @@ -#include "List.h" +#ifndef HEADER_LIST +#define HEADER_LIST + +#include <istream> +#include <ostream> + +template <class T> +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 */ @@ -43,8 +83,8 @@ void List<T>::add(const T& element) { } template <class T> -bool List<T>::insertAt(const T& element, unsigned index) { - if (index >= count) return add(element); +void List<T>::insertAt(const T& element, unsigned index) { + if (index >= count) add(element); if (length == count) resize(); for (unsigned i = count; i > index; i--) @@ -58,10 +98,10 @@ template <class T> bool List<T>::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; + count--; + return true; } template <class T> @@ -167,3 +207,5 @@ List<T>& List<T>::operator=(List&& other) { } 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 <class T> -OrderedList<T>::OrderedList() : OrderedList(nullptr, 0) {} - -template <class T> -OrderedList<T>::OrderedList(const T* elements, unsigned elementsCount) : List<T>::List(elements, elementsCount) {} - -template <class T> -bool OrderedList<T>::insert(const T& element) { - if (List<T>::length == List<T>::count) List<T>::resize(); - - unsigned insertionInd = 0; - while (List<T>::elements[insertionInd].compare(element) < 0 && insertionInd < List<T>::count) - insertionInd++; - - return List<T>::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 T> -class OrderedList : public List<T> { -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 T> +class OrderedList : public List<T> { +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 <class T> +OrderedList<T>::OrderedList() : OrderedList(nullptr, 0) {} + +template <class T> +OrderedList<T>::OrderedList(const T* elements, unsigned elementsCount) : List<T>::List(elements, elementsCount) {} + +template <class T> +void OrderedList<T>::insert(const T& element) { + if (List<T>::length == List<T>::count) List<T>::resize(); + + unsigned insertionInd = 0; + while (List<T>::elements[insertionInd].compare(element) < 0 && insertionInd < List<T>::count) + insertionInd++; + + List<T>::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() { + +} |
