Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
286 lines (232 sloc) 8.08 KB
//
// IdeasBank.h
// Ideas_Bank
//
// Created by Mirna Wahab on 24/04/2017.
// Copyright © 2017 Mirna Wahab. All rights reserved.
//
#ifndef IdeasBank_h
#define IdeasBank_h
class IdeasBank {
private:
Index index;
vector<Idea> ideas;
AvlTree<Index, string> invIndx_tree;
public:
// ==================== TESTING ====================
static void printIndex(Index index) {
cout << "key: " << index.key << endl;
cout << "idList: ";
for (int i = 0; i < index.idList.size(); i++) {
cout << index.idList[i];
if (i+1 != index.idList.size()){
cout << ", ";
}
}
cout << endl << endl;
}// end of printIndex
void printTree(){
if (invIndx_tree.AVL_Empty()){
cout << "Nothing in tree to display" << endl;
}
else{
cout << "\n AVL TREE CONTENT\n" << endl;
invIndx_tree.AVL_Print();
cout << endl;
invIndx_tree.AVL_Traverse(printIndex);
}
}// end printTree
void inputIdeaTest(){
Idea newIdea;
newIdea.setTxtID(109);
newIdea.setProposer("mirna");
newIdea.setKwords("wirelessly");
newIdea.setContent("wireless computer idea");
ideas.push_back(newIdea);
}// end of inputIdeaTest
// ==================== END TESTING ====================
void indexing() {
vector<string> kwords_vec;
vector<int> relevantIDs;
int foundIdIn;
string kword;
for (int loop = 0; loop < ideas.size(); loop++){
kwords_vec = ideas[loop].getKwords();
for (int i = 0; i < kwords_vec.size(); i++){
// goes through all ideas
for (int j = 0; j < ideas.size(); j++){
if (ideas[j].lookForWord(kwords_vec[i])){
kword = kwords_vec[i];
foundIdIn = ideas[j].getID();
relevantIDs.push_back(foundIdIn);
}// end of lookfor
}
invIndx_tree.AVL_UpdateAndInsert(kword, relevantIDs);
relevantIDs.clear();
}// end of kwords.size loop
}
}// end of indexing
void searchQuery(string word){
Index outIndex;
if (invIndx_tree.AVL_Retrieve(word, outIndex)){
cout << "\nRelevant idea ID's for "
<< word << ":" << endl;
for (int i = 0; i < outIndex.idList.size(); i++){
cout << outIndex.idList[i] << endl;
}
}
else{
cout << "\nNo relevant ideas found for "
<< word << endl;
}
}// end of searchQuery
void inputIdea(){
bool exit = false;
string proposer, keyword, content;
cout << "\n Enter new Idea\n" << endl;
Idea newIdea; // create new idea obj
// SET ID
newIdea.setID();
// SET PROPOSER
cout << "Input Proposer Name:" << endl;
getline(cin, proposer);
newIdea.setProposer(proposer);
// SET KEYWORDS
cout << "Input keywords seperated by newline, "
<< "enter 'q' when done" << endl;
while (exit == false){
cin >> keyword;
if(keyword == "q" || keyword == "Q"){
exit = true;
}
else{
newIdea.setKwords(keyword);
}
}
cin.ignore(); // ignores above cin
// SET CONTENT
cout << "Input content:" << endl;
getline(cin, content);
newIdea.setContent(content);
ideas.push_back(newIdea);
cout << endl;
}// end of inputdeas
void inputIdeasTxt(){
string proposer, content, kWord, line, input;
vector<string> kwords_vec;
int id_ = 0, count = 0;
size_t found;
ifstream file;
file.open("ideas.txt");
if (file.fail()){
cout << "Failed to open file: Check file path\n";
exit(1);
}
while (!file.eof()){
getline (file, line);
found = line.find(":");
if (found != string::npos){
input = line.substr(found+2, line.size()-found);
count++;
}
istringstream readWord(input);
switch(count) {
case 1 : // set ID
id_ = stoi(input);
break;
case 2 : // set Proposers
proposer = input;
break;
case 3 : // set Keywords
while(readWord >> kWord) { // reads line word by word
if (kWord.back() == ','){
kWord.pop_back();
}
kwords_vec.push_back(kWord);
}
break;
case 4 : // set Content
content = input;
break;
default :
break;
}// end of switch
if (count == 4){ // 4 lines per idea
// insert idea into vector
Idea newIdea;
newIdea.setTxtID(id_);
newIdea.setProposer(proposer);
for (int i=0; i < kwords_vec.size(); i++){
newIdea.setKwords(kwords_vec[i]);
}
newIdea.setContent(content);
ideas.push_back(newIdea);
kwords_vec.clear();
count = 0;
}
}// end of EOF loop
file.close();
}// end of inputIdeasTxt
void deleteIdea(int ID){
bool found = false;
vector<string> kWordsToDelete;
vector<string> kWords_vec;
string key;
for(int i = 0; i < ideas.size(); i++ ){
// deletes idea obj
if (ID == ideas[i].getID()){
kWordsToDelete = ideas[i].getKwords();
ideas.erase(ideas.begin()+i);
found = true;
}
// deletes all instances of id in tree
for (int j = 0; j < ideas[i].getKwords().size(); j++) {
key = ideas[i].getKwords()[j];
Index modIndex;
if (invIndx_tree.AVL_Retrieve(key, modIndex)){
for (int i = 0; i < modIndex.idList.size(); i++) {
if (ID == modIndex.idList[i]){
modIndex.idList.erase(modIndex.idList.begin()+i);
invIndx_tree.AVL_Update(key, modIndex);
}
}
}
}
} // end of ideas.size loop
// deletes key/kword from tree
for (int i = 0; i < kWordsToDelete.size(); i++) {
invIndx_tree.AVL_Delete(kWordsToDelete[i]);
}
if(found == false){
cout << "Idea with ID: " << ID
<< " does not exist to delete" << endl;
}
}// end of deleteIdea
void displayIdea(int ID){
bool found = false;
for (int i = 0; i < ideas.size(); i++ ){
if (ID == ideas[i].getID()){
cout << "\nDisplaying idea with ID: "
<< ID << endl;
ideas[i].printIdea();
found = true;
break;
}
}
if(found == false){
cout << "Idea with ID: "
<< ID << " does not exist to display" << endl;
}
}// end of displayIdea
void displayAllIdeas(){
if (ideas.size() == 0){
cout << "No ideas to display" << endl;
}
else{
for(int i = 0; i < ideas.size(); i++ ){
ideas[i].printIdea();
}
}
}// end of displayAllIdeas
}; // end of class
#endif /* IdeasBank_h */
You can’t perform that action at this time.