//DataBase.cpp #include "DataBase.h" #include #include DataBase::DataBase(){ NumStudents = 0; NumItems = 0; } //////////////////////////////////////////////////////////////// Load // Loads items and students // // Parameters: // // // Pre: // // Post: // // Returns: Describe what value the function returns, if any. // // Called by: // Calls: // void DataBase::Load(){ ifstream IN1; ifstream IN2; IN1.open("students.data"); IN2.open("items.data"); //add assert assert(!IN1.fail()); //add assert //int i, j; //i = j = 0; string Next; IgnoreBlank(IN1); getline(IN1, Next, '\n'); //IN1.ignore(10,'\n'); while(IN1){ //cout << endl << "next " << Next; S[NumStudents].AddStudent(IN1, Next); //IgnoreBlank(IN1); IgnoreBlank(IN1); getline(IN1, Next, '\n'); //i++; NumStudents++; //IN1.ignore(10,'\n'); } IgnoreBlank(IN2); getline(IN2, Next, '\n'); //IN1.ignore(10,'\n'); while(IN2){ I[NumItems].AddItem(IN2, Next, S, NumStudents); IgnoreBlank(IN2); getline(IN2, Next, '\n'); //j++; NumItems++; //IN1.ignore(10,'\n'); } IN1.close(); IN2.close(); } //////////////////////////////////////////////////////////////// // // // Parameters: // // // Pre: // // Post: // // Returns: Describe what value the function returns, if any. // // Called by: // Calls: // void DataBase::AddStudent(ifstream& inFile, ofstream& outFile){ string Next; bool Owner = false, Added = false; getline(inFile, Next, '\n'); for(int i = 0; i // // // Parameters: // // // Pre: // // Post: // // Returns: Describe what value the function returns, if any. // // Called by: // Calls: // void DataBase::AddItem(ifstream& inFile, ofstream& outFile){ bool Owned = false, Added = false; string Next; getline(inFile, Next, '\n'); //check for empty spaces for(int i = 0; i // // // Parameters: // // // Pre: // // Post: // // Returns: Describe what value the function returns, if any. // // Called by: // Calls: // void DataBase::DeleteStudent(ifstream& inFile, ofstream& outFile){ string Delete; bool found1 = false; //bool found2 = false; inFile >> ws; getline(inFile, Delete, '\n'); outFile << "delete " << Delete < // // // Parameters: // // // Pre: // // Post: // // Returns: Describe what value the function returns, if any. // // Called by: // Calls: // void DataBase::PrintStudents(ofstream& outFile){ //cout << "\n*** PRINTING STUDENTS ***\n"; for(int i = 0; i < NumStudents; i++){ if(S[i].ReturnDeleted() == false){ S[i].PrintInfo(outFile); } } } //////////////////////////////////////////////////////////////// // // // Parameters: // // // Pre: // // Post: // // Returns: Describe what value the function returns, if any. // // Called by: // Calls: // void DataBase::PrintItems(ofstream& outFile){ //cout << NumItems; for(int i = 0; i < NumItems; i++){ if(!I[i].ReturnDeleted() ){ I[i].PrintInfo(outFile); } } } //////////////////////////////////////////////////////////////// // // // Parameters: // // // Pre: // // Post: // // Returns: Describe what value the function returns, if any. // // Called by: // Calls: // void DataBase::PrintStudent(ifstream& inFile, ofstream& outFile, string ToPrint){ outFile << "print " << ToPrint; bool found = false; for(int i = 0; i < NumStudents; i++){ if(S[i].ReturnEmail() == ToPrint && S[i].ReturnDeleted() == false){ S[i].PrintInfo(outFile); found = true; } } outFile << endl; if(!found){ outFile << ToPrint << " not found.\n"; } //print student info else{ for(int j = 0; j < NumItems; j++){ if(I[j].ReturnOwner() == ToPrint && I[j].ReturnDeleted() == false){ //cout << I[j].ReturnOwner(); if(I[j].ReturnName() != "blank"){ outFile << "Item:\n"; outFile << " " << I[j].ReturnName() < 0.0){ outFile << "Price:\n"; outFile << " " << "$" << fixed << setprecision(2) << I[j].ReturnPrice() << endl; } if(I[j].ReturnAvailability() != "blank"){ outFile << "Availability:\n"; outFile << " " << I[j].ReturnAvailability() << endl; } outFile << endl; } } } } void DataBase::IgnoreBlank(ifstream& IN){ if(IN.peek() == '\n'){ IN.ignore(100, '\n'); } }