Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <vector>
- #include <iostream>
- #include <string>
- #include <ctype.h>
- #include "C_Table.h"
- using namespace std;
- template<typename T>
- class UI
- {
- private:
- vector<CTable<T>*> v_CTables;
- void VoidMethod(CTable<T> ctable)
- {}
- bool AddCTables()
- {
- cout << "How many CTables do you want to create? :";
- int n_count = GetPositiveNumberInput();
- if (n_count == 0) return false;
- for (int i = 0; i < n_count; i++) {
- CTable<T>* temp;
- v_CTables.push_back(temp);
- }
- return true;
- }
- bool ChangeTableSizeByIndex()
- {
- if (CheckIfCTableVectorEmpty())
- {
- cout << endl << "The vector is empty" << endl;
- return false;
- }
- CTable<T>* ptr_CTableToAffect = (v_CTables[0]);
- if (SelectCTableByIndex(ptr_CTableToAffect) == false) return false;
- cout << endl << "Specify new table size for chosen CTable: ";
- int n_newSize = GetPositiveNumberInput();
- (*ptr_CTableToAffect).ChangeTableSize(n_newSize);
- return true;
- }
- bool CheckIfCTableVectorEmpty()
- {
- if (v_CTables.empty()) return true;
- return false;
- }
- bool SelectCTableByIndex(CTable<T>*& result)
- {
- cout << endl << "Which CTable (index) do you want to affect?: ";
- int n_CTableIndex = GetPositiveNumberInput();
- int vectorSize = v_CTables.size();
- if ((n_CTableIndex >= vectorSize))
- {
- cout << endl << "Index out of range" << endl;
- return false;
- }
- result = v_CTables[n_CTableIndex];
- return true;
- }
- bool DeleteCTableByIndex()
- {
- if (CheckIfCTableVectorEmpty())
- {
- cout << endl << "The vector is empty" << endl;
- return false;
- }
- cout << endl << "Which CTable (index) do you want to affect?: ";
- int n_CTableIndex = GetPositiveNumberInput();
- int vectorSize = v_CTables.size();
- if ((n_CTableIndex >= vectorSize))
- {
- cout << endl << "Index out of range" << endl;
- return false;
- }
- v_CTables.erase(v_CTables.begin() + n_CTableIndex);
- return true;
- }
- void DeleteAllCTables()
- {
- for each (CTable<T>* ptr_CTable in v_CTables)
- {
- delete ptr_CTable;
- }
- v_CTables.clear();
- }
- bool ChangeCTableNameByIndex()
- {
- if (CheckIfCTableVectorEmpty())
- {
- cout << endl << "The vector is empty" << endl;
- return false;
- }
- CTable<T>* ptr_CTableToAffect = (v_CTables[0]);
- if (SelectCTableByIndex(ptr_CTableToAffect) == false)
- {
- cout << endl << "Index out of range" << endl;
- return false;
- }
- cout << endl << "New CTable name: ";
- string s_newName;
- cin >> s_newName;
- cout << endl;
- ptr_CTableToAffect->SetName(s_newName);
- return true;
- }
- int GetPositiveNumberInput() // -1 if input incorrect
- {
- string s_userInput;
- do
- {
- s_userInput = "";
- cin >> s_userInput;
- bool isNumber = true;
- int n_inputLength = s_userInput.length();
- for (int i = 0; i < n_inputLength; i++)
- {
- if (!isdigit((int)s_userInput[i])) isNumber = false;
- }
- if (!isNumber)
- {
- cout << endl << "Wrong input, try again" << endl;
- continue;
- }
- } while (atoi(s_userInput.c_str()) < 0);
- return atoi(s_userInput.c_str());
- }
- bool CreateCTableDuplicate()
- {
- if (CheckIfCTableVectorEmpty()) return false;
- CTable<T>* ptr_CTableToAffect = v_CTables.front();
- if (SelectCTableByIndex(ptr_CTableToAffect) == false) return false;
- CTable<T>* ptr_newCTable = ptr_CTableToAffect;
- v_CTables.push_back(ptr_newCTable);
- return true;
- }
- bool PrintCTableContent()
- {
- if (CheckIfCTableVectorEmpty()) return false;
- CTable<T>* ptr_CTableToPrint = v_CTables[0];
- if (SelectCTableByIndex(ptr_CTableToPrint) == false) return false;
- string s_result = ptr_CTableToPrint->ToString();
- cout << endl << s_result << endl;
- return true;
- }
- bool AssignValueInCTable()
- {
- if (CheckIfCTableVectorEmpty())
- {
- cout << endl << "The vector is empty" << endl;
- return false;
- }
- CTable<T>* ptr_CTableToAffect = v_CTables[0];
- if (SelectCTableByIndex(ptr_CTableToAffect) == false)
- return false;
- cout << "Select index in CTable to affect: ";
- int n_index = GetPositiveNumberInput();
- if (n_index < 0 || n_index >= ptr_CTableToAffect->GetTableSize()) return false;
- cout << endl << "New value: ";
- string s_input;
- cin >> s_input;
- bool isNumber = true;
- for (int i = 0; i < s_input.length(); i++)
- {
- if (isdigit(s_input[i] == false))
- {
- cout << endl << "Not a valid number" << endl;
- return false;
- }
- }
- ptr_CTableToAffect->AssignValueByIndex(atoi(s_input.c_str()), n_index);
- return true;
- }
- void ShowNames()
- {
- for (int i = 0; i < v_CTables.size(); i++)
- {
- cout << endl << (*v_CTables[i]).ToString() << endl;
- }
- }
- void ShowMenu()
- {
- cout << "1. Add new CTables" << endl;
- cout << "2. Change table size for chosen CTable" << endl;
- cout << "3. Delete chosen CTable" << endl;
- cout << "4. Delete all CTables" << endl;
- cout << "5. Change name of chosen CTable" << endl;
- cout << "6. Clone chosen CTable" << endl;
- cout << "7. Print contents of chosen CTable" << endl;
- cout << "8. Assign value at specified index in chosen CTable" << endl;
- cout << "9. Show all CTables" << endl;
- cout << "10. Exit program" << endl;
- cout << "11. Void method" << endl;
- int n_userChoice = 0;
- cout << "Choice: ";
- n_userChoice = GetPositiveNumberInput();
- switch (n_userChoice) {
- case 1:
- {
- AddCTables();
- }
- break;
- case 2:
- {
- ChangeTableSizeByIndex();
- }
- break;
- case 3:
- {
- DeleteCTableByIndex();
- }
- break;
- case 4:
- {
- DeleteAllCTables();
- }
- break;
- case 5:
- {
- ChangeCTableNameByIndex();
- }
- break;
- case 6:
- {
- CreateCTableDuplicate();
- }
- break;
- case 7:
- {
- PrintCTableContent();
- }
- break;
- case 8:
- {
- AssignValueInCTable();
- }
- break;
- case 9:
- {
- ShowNames();
- }
- break;
- case 10:
- {
- }
- case 11:
- {
- //cout << (*v_CTables[0] + *v_CTables[1]).ToString();
- }
- break;
- default:
- {
- cout << endl << "Wrong input, try again" << endl << endl;
- }
- }
- }
- public:
- UI<T>()
- {
- while (true)
- {
- ShowMenu();
- }
- }
- ~UI()
- {
- for each (CTable<T>* ptr_CTable in v_CTables)
- {
- delete ptr_CTable;
- }
- v_CTables.resize(0);
- }
- };
Add Comment
Please, Sign In to add comment