diff options
Added files from project 2 (mirror)
2 files changed, 193 insertions, 0 deletions
| -rw-r--r-- | Year 1/Object-oriented Programming/Project 2/src/UserInterface/UserInterface.cpp | 148 | ||||
| -rw-r--r-- | Year 1/Object-oriented Programming/Project 2/src/UserInterface/UserInterface.h | 45 |
diff --git a/Year 1/Object-oriented Programming/Project 2/src/UserInterface/UserInterface.cpp b/Year 1/Object-oriented Programming/Project 2/src/UserInterface/UserInterface.cpp new file mode 100644 index 0000000..0fc9e96 --- /dev/null +++ b/Year 1/Object-oriented Programming/Project 2/src/UserInterface/UserInterface.cpp @@ -0,0 +1,148 @@ +#include "UserInterface.h" +#include "../Generic/List/List.hpp" +#include "../Generic/String/String.h" +#include "../Generic/ConsoleInterface/Toolbox.hpp" +#include <stdexcept> + +/* Private (static) */ + +Table* UserInterface::table = nullptr; +String UserInterface::fileName = String(); +bool UserInterface::saved = false; + + +void UserInterface::com_open(const char* params) { + if (table != nullptr) { + printLine("A file has already been opened!"); + return; + } + + try { + table = new Table(params); + fileName = params; + saved = true; + printLine("Successfully opened file!"); + } + catch (std::logic_error err) { + printLine(err.what()); + } +} +void UserInterface::com_close(const char* params) { + if (table == nullptr) { + printLine("No file has been opened!"); + return; + } + + delete table; +} +void UserInterface::com_save(const char* params) { + com_saveas(fileName.get_cstr()); +} +void UserInterface::com_saveas(const char* params) { + if (table == nullptr) { + printLine("No file has been opened!"); + return; + } + + try { + table->saveToFile(params); + saved = true; + printLine("Successfully saved to file!"); + } + catch (std::logic_error err) { + printLine(err.what()); + } +} + +void UserInterface::com_help(const char* params) { + std::cout << "The following commands are supported:" << std::endl + << "print prints the whole table" << std::endl + << "edit RxCy <data> Replaces the value on row x and column y with the given data" << std::endl + << "open <file> opens <file>" << std::endl + << "close closes currently opened file" << std::endl + << "save saves the currently open file" << std::endl + << "saveas <file> saves the currently open file in <file>" << std::endl + << "help prints this information" << std::endl + << "exit exists the program" << std::endl; +} + +void UserInterface::com_exit(const char* params) { + if (!saved && table != nullptr) { + printLine("You have an open file with unsaved changes, please select close or save first."); + return; + } + + throw std::logic_error("Exiting program..."); // stupid way of making the program stop +} + +void UserInterface::com_print(const char* params) { + if (table == nullptr) { + printLine("You need to open a file!"); + return; + } + + List<String> cells = table->getAllCells(); + printTable(cells, table->get_cols()); +} + +#define isDigit(a) (a >= '0' && a <= '9') + +void UserInterface::com_edit(const char* params) { + if (*params != 'R') { + printLine("Error: Invalid cell position format! Expected R<number>C<number>"); + return; + } + + unsigned row = atoi(++params) - 1; + while (*params != 'C') { + if (*params == '\0') { + printLine("Error: End of line reached. Expected row number."); + return; + } + if (!isDigit(*params)) { + printLine("Error: Found a non-digit character. Exptected row number."); + return; + } + + params++; + } + unsigned col = atoi(++params) - 1; + while (*params != ' ') { + if (*params == '\0') { + printLine("Error: End of line reached. Expected columns number."); + return; + } + if (!isDigit(*params)) { + printLine("Error: Found a non-digit character. Exptected column number."); + return; + } + + params++; + } + + try { + table->putCell(row, col, params); + saved = false; + printLine("Successfully edited cell!"); + } + catch (std::logic_error err) { + printLine(err.what()); + } +} + +/* Public */ + +UserInterface::UserInterface() : menu() { + menu.addCommand(Command("open", com_open)); + menu.addCommand(Command("close", com_close)); + menu.addCommand(Command("save", com_save)); + menu.addCommand(Command("saveas", com_saveas)); + menu.addCommand(Command("help", com_help)); + menu.addCommand(Command("exit", com_exit)); + menu.addCommand(Command("print", com_print)); + menu.addCommand(Command("edit", com_edit)); +} + +void UserInterface::run() { + menu.navigate(); +} diff --git a/Year 1/Object-oriented Programming/Project 2/src/UserInterface/UserInterface.h b/Year 1/Object-oriented Programming/Project 2/src/UserInterface/UserInterface.h new file mode 100644 index 0000000..3557da8 --- /dev/null +++ b/Year 1/Object-oriented Programming/Project 2/src/UserInterface/UserInterface.h @@ -0,0 +1,45 @@ +#ifndef HEADER_USERINTERFACE +#define HEADER_USERINTERFACE + +/*! \file UserInterface.h + * \brief Stores the declaration of class UserInterface + */ + +#include "../Generic/ConsoleInterface/Menu.h" +#include "../Generic/String/String.h" +#include "../Models/Table.h" + +/*! \brief The class that contains and via which the user controls the application's user interface + */ +class UserInterface { + Menu menu; + + static Table* table; + static String fileName; + static bool saved; + + //! Opens a file + static void com_open(const char* params); + //! Closes a file + static void com_close(const char* params); + //! Saves a file to the location it was opened from + static void com_save(const char* params); + //! Saves a file to a given location + static void com_saveas(const char* params); + //! Prints the help message + static void com_help(const char* params); + //! Exists the appliction + static void com_exit(const char* params); + + //! Prints the currently opened table + static void com_print(const char* params); + //! Edits a cell of the currently opened table + static void com_edit(const char* params); + +public: + UserInterface(); + //! Starts the user interface + void run(); +}; + +#endif |
