handle absence of default input file, fix delete cpuId input
This commit is contained in:
parent
d00fcd2d04
commit
0e1424ea3f
@ -27,7 +27,7 @@ private:
|
||||
public:
|
||||
HashTable() {
|
||||
count = 0;
|
||||
hashSize = 3;
|
||||
hashSize = 7;
|
||||
hashAry = new HashNode<T>[hashSize];
|
||||
}
|
||||
|
||||
|
18
fio.cpp
18
fio.cpp
@ -10,8 +10,8 @@ using namespace std;
|
||||
int findHashSize(const string &filename) {
|
||||
ifstream inputFile(filename);
|
||||
|
||||
if (!inputFile) {
|
||||
cout << "Error opening the input file: \"" << filename << "\"" << endl;
|
||||
if (!inputFile.good()) {
|
||||
// cout << "Error opening the input file: \"" << filename << "\"" << endl;
|
||||
return -1;
|
||||
}
|
||||
// cout << "Reading data from \"" << filename << "\"" << endl;
|
||||
@ -32,9 +32,9 @@ void insertFile(const string &filename, BinarySearchTree<string> &bst, HashTable
|
||||
ifstream inputFile(filename);
|
||||
// cout << "Reading data from \"" << filename << "\"" << endl;
|
||||
|
||||
if (!inputFile) {
|
||||
cout << "Error opening the input file: \"" << filename << "\"" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
if (!inputFile.good()) {
|
||||
cout << "Error opening the input file: \"" << filename << "\". Skipping...\n";
|
||||
return;
|
||||
}
|
||||
|
||||
string line;
|
||||
@ -47,6 +47,13 @@ void insertFile(const string &filename, BinarySearchTree<string> &bst, HashTable
|
||||
|
||||
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();
|
||||
getline(temp, strToNum, ';');
|
||||
releaseYear = stoi(strToNum);
|
||||
@ -71,6 +78,7 @@ void insertFile(const string &filename, BinarySearchTree<string> &bst, HashTable
|
||||
}
|
||||
|
||||
inputFile.close();
|
||||
cout << "Data from file \"" << filename << "\" added.\n";
|
||||
}
|
||||
|
||||
|
||||
|
20
main.cpp
20
main.cpp
@ -49,11 +49,15 @@ int main() {
|
||||
printHelp();
|
||||
|
||||
int hashSize = findHashSize(DEFAULT_FILE);
|
||||
HashTable<CPU> cpuTable = HashTable<CPU>(hashSize);
|
||||
HashTable<CPU> cpuTable = HashTable<CPU>(hashSize == -1 ? 7 : hashSize);
|
||||
BinarySearchTree<string> cpuTree;
|
||||
|
||||
// Read initial data from output file
|
||||
// Read initial data from output file if it exists
|
||||
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);
|
||||
SearchManager<CPU> searchManager(&cpuTable);
|
||||
@ -148,19 +152,19 @@ void handleFileInput(HashTable<CPU> &hashTable, BinarySearchTree<string> &tree)
|
||||
cin >> filename;
|
||||
|
||||
insertFile(filename, tree, hashTable);
|
||||
cout << "Data from file \"" << filename << "\" added.\n";
|
||||
}
|
||||
|
||||
void deleteCPU(HashTable<CPU> &hashTable, BinarySearchTree<string> &tree, UndoManager<CPU> &undoManager) {
|
||||
string cpuId;
|
||||
cout << "Enter CPU ID to delete: ";
|
||||
cin >> cpuId;
|
||||
cin.ignore();
|
||||
getline(cin, cpuId);
|
||||
|
||||
CPU cpu(cpuId, 0, 0, "", 0.0);
|
||||
CPU cpuFound;
|
||||
while (hashTable.search(cpuFound, cpu, key_to_index) == -1) {
|
||||
cout << "CPU ID not found. Enter a valid CPU ID: ";
|
||||
cin >> cpuId;
|
||||
cpu = CPU(cpuId, 0, 0, "", 0.0);
|
||||
if (hashTable.search(cpuFound, cpu, key_to_index) == -1) {
|
||||
cout << "CPU ID \"" << cpuId << "\" not found.\n";
|
||||
return;
|
||||
}
|
||||
hashTable.remove(cpuFound, cpu, key_to_index);
|
||||
undoManager.addToUndoStack(cpuFound);
|
||||
|
Loading…
x
Reference in New Issue
Block a user