handle absence of default input file, fix delete cpuId input

This commit is contained in:
Iurii Tatishchev 2024-06-20 12:45:56 -07:00
parent d00fcd2d04
commit 0e1424ea3f
Signed by: CaZzzer
GPG Key ID: E0EBF441EA424369
3 changed files with 27 additions and 15 deletions

View File

@ -27,7 +27,7 @@ private:
public: public:
HashTable() { HashTable() {
count = 0; count = 0;
hashSize = 3; hashSize = 7;
hashAry = new HashNode<T>[hashSize]; hashAry = new HashNode<T>[hashSize];
} }

18
fio.cpp
View File

@ -10,8 +10,8 @@ using namespace std;
int findHashSize(const string &filename) { int findHashSize(const string &filename) {
ifstream inputFile(filename); ifstream inputFile(filename);
if (!inputFile) { if (!inputFile.good()) {
cout << "Error opening the input file: \"" << filename << "\"" << endl; // cout << "Error opening the input file: \"" << filename << "\"" << endl;
return -1; return -1;
} }
// cout << "Reading data from \"" << filename << "\"" << endl; // cout << "Reading data from \"" << filename << "\"" << endl;
@ -32,9 +32,9 @@ void insertFile(const string &filename, BinarySearchTree<string> &bst, HashTable
ifstream inputFile(filename); ifstream inputFile(filename);
// cout << "Reading data from \"" << filename << "\"" << endl; // cout << "Reading data from \"" << filename << "\"" << endl;
if (!inputFile) { if (!inputFile.good()) {
cout << "Error opening the input file: \"" << filename << "\"" << endl; cout << "Error opening the input file: \"" << filename << "\". Skipping...\n";
exit(EXIT_FAILURE); return;
} }
string line; string line;
@ -47,6 +47,13 @@ void insertFile(const string &filename, BinarySearchTree<string> &bst, HashTable
getline(temp, name, ';'); getline(temp, name, ';');
// check if the CPU is already in the hash table
CPU key(name, -1, -1, "", -1);
if (hash.search(key, key, key_to_index) != -1) {
cout << "Duplicate CPU \"" << name << "\" found in file. Skipping...\n";
continue;
}
temp.ignore(); temp.ignore();
getline(temp, strToNum, ';'); getline(temp, strToNum, ';');
releaseYear = stoi(strToNum); releaseYear = stoi(strToNum);
@ -71,6 +78,7 @@ void insertFile(const string &filename, BinarySearchTree<string> &bst, HashTable
} }
inputFile.close(); inputFile.close();
cout << "Data from file \"" << filename << "\" added.\n";
} }

View File

@ -49,11 +49,15 @@ int main() {
printHelp(); printHelp();
int hashSize = findHashSize(DEFAULT_FILE); int hashSize = findHashSize(DEFAULT_FILE);
HashTable<CPU> cpuTable = HashTable<CPU>(hashSize); HashTable<CPU> cpuTable = HashTable<CPU>(hashSize == -1 ? 7 : hashSize);
BinarySearchTree<string> cpuTree; BinarySearchTree<string> cpuTree;
// Read initial data from output file // Read initial data from output file if it exists
insertFile(DEFAULT_FILE, cpuTree, cpuTable); if (hashSize != -1) {
insertFile(DEFAULT_FILE, cpuTree, cpuTable);
} else {
cout << "Could not open default file \"" << DEFAULT_FILE << "\". Starting with an empty database.\n";
}
DisplayManager<CPU> displayManager(&cpuTable, &cpuTree); DisplayManager<CPU> displayManager(&cpuTable, &cpuTree);
SearchManager<CPU> searchManager(&cpuTable); SearchManager<CPU> searchManager(&cpuTable);
@ -148,19 +152,19 @@ void handleFileInput(HashTable<CPU> &hashTable, BinarySearchTree<string> &tree)
cin >> filename; cin >> filename;
insertFile(filename, tree, hashTable); insertFile(filename, tree, hashTable);
cout << "Data from file \"" << filename << "\" added.\n";
} }
void deleteCPU(HashTable<CPU> &hashTable, BinarySearchTree<string> &tree, UndoManager<CPU> &undoManager) { void deleteCPU(HashTable<CPU> &hashTable, BinarySearchTree<string> &tree, UndoManager<CPU> &undoManager) {
string cpuId; string cpuId;
cout << "Enter CPU ID to delete: "; cout << "Enter CPU ID to delete: ";
cin >> cpuId; cin.ignore();
getline(cin, cpuId);
CPU cpu(cpuId, 0, 0, "", 0.0); CPU cpu(cpuId, 0, 0, "", 0.0);
CPU cpuFound; CPU cpuFound;
while (hashTable.search(cpuFound, cpu, key_to_index) == -1) { if (hashTable.search(cpuFound, cpu, key_to_index) == -1) {
cout << "CPU ID not found. Enter a valid CPU ID: "; cout << "CPU ID \"" << cpuId << "\" not found.\n";
cin >> cpuId; return;
cpu = CPU(cpuId, 0, 0, "", 0.0);
} }
hashTable.remove(cpuFound, cpu, key_to_index); hashTable.remove(cpuFound, cpu, key_to_index);
undoManager.addToUndoStack(cpuFound); undoManager.addToUndoStack(cpuFound);