// MOTORS_02 Application /* Electric Motor Inventory example, using a text file by Kip Irvine. Required background: Two lectures on classes and objects, including file streams, constructors. See the MotorsApp PowerPoint slide show for a list of the following scenarios: Scenario 1: Manager appends a new motor to the motor inventory file Scenario 2: The manager views a list of all motors in the motor inventory file Scenario 3: The manager searches for a motor in the motor inventory file using the motor's ID number Scenario 4: The manager adds a new motor to the inventory file if and only if the motor is not already in the file Scenario 5: The manager views a list of all motors having a specific voltage. */ #include #include #include #include //#include //#include using namespace std; #include "motor.h" const string MOTOR_FILENAME = "motors.txt"; void wait() { cout << "Press any key..."; getchar(); cout << endl; } //************* PROPOSED UTILITY FUNCTIONS ************** double InputReal() // Purpose: Input a real number, with error checking // for corrupted input // Returns: A valid double value { return 0; } bool OpenInputInventoryFile( ifstream & infile ) // Purpose: Open the Inventory file for input. // Receives: infile - the file stream to be opened // Returns: true if the file was successfully opened, // false otherwise { return true; } bool OpenOutputInventoryFile( ofstream & outfile ) // Purpose: Open the Inventory file for output. // Receives: outfile - the file stream to be opened // Returns: true if the file was successfully opened, // false otherwise { return true; } bool AppendMotor( CMotor & aMotor ) // Purpose: Append a motor to the Inventory file // Returns: true if the operation was successfull, or // false if it was not. { return true; } bool FindMotor( CMotor & aMotor ) // Purpose: Searches for a motor in the inventory file by looking // for a matching ID number. If one is found, a complete // motor object is returned in aMotor. // Receives: aMotor - a motor containing only an ID number // Returns: true if findID matches one of the motor // ID numbers in the file; false otherwise { // to be done later return false; } //********* END OF UTILITY FUNCTIONS ************* void Scenario1() // Manager appends a new motor to the motor inventory file { cout << "Appending a new motor to the inventory file.\n\n"; cout << "Enter all motor attributes:\n"; CMotor M1; M1.Input(); ofstream outfile(MOTOR_FILENAME.c_str(), ios::app); if( !outfile.is_open()) { cout << "Cannot open " << MOTOR_FILENAME << " in output mode.\n"; return; } M1.WriteToFile(outfile); } void Scenario2() // The manager views a list of all motors in the // motor inventory file { ifstream infile(MOTOR_FILENAME.c_str()); if( !infile.is_open()) { cout << "Cannot open " << MOTOR_FILENAME << endl; return; } system("cls"); cout << "**** Complete Motor Inventory ****\n\n"; int lineCount = 0; CMotor aMotor; while( !infile.eof() ) { aMotor.ReadFromFile(infile); // check for blank ID number at end of file: if( aMotor.get_ID() != "" ) aMotor.Display(); lineCount++; if( lineCount % 22 == 0 ) // pause every 22 lines wait(); } } void Scenario3() // The manager searches for a motor in the motor // inventory file using the motor's ID number { ifstream infile(MOTOR_FILENAME.c_str()); if( !infile.is_open()) { cout << "Cannot open " << MOTOR_FILENAME << endl; return; } system("cls"); cout << "**** Searching for a Motor ****\n\n"; string findID; cout << "Enter the ID number to be found: "; cin >> findID; CMotor aMotor; bool foundMatchingID = false; while( !(infile.eof() || foundMatchingID) ) { aMotor.ReadFromFile(infile); if( aMotor.get_ID() == findID ) { cout << "A matching motor was found:\n"; aMotor.Display(); foundMatchingID = true; } } // while if( !foundMatchingID ) cout << "Unable to find a matching motor.\n\n"; } void Scenario4() // The manager adds a new motor to the inventory file if // and only if the motor is not already in the file { ifstream infile(MOTOR_FILENAME.c_str()); if( !infile.is_open()) { cout << "Cannot open " << MOTOR_FILENAME << endl; return; } system("cls"); cout << "**** Adding a New Motor to the Inventory " "File ****\n\n"; CMotor newMotor; cout << "Enter all motor attributes:\n"; newMotor.Input(); while( !infile.eof() ) { CMotor aMotor; aMotor.ReadFromFile(infile); if( aMotor.get_ID() == newMotor.get_ID() ) { cout << "The motor ID is already in the file, and " "cannot be added.\n"; return; } } // while infile.close(); // If we arrive here, the motor was not found. We can // append the new motor. ofstream outfile(MOTOR_FILENAME.c_str(), ios::app); if( !outfile.is_open()) { cout << "Cannot open " << MOTOR_FILENAME << " in output mode.\n"; return; } newMotor.WriteToFile(outfile); cout << "The new motor was successfully appended " "to the inventory file.\n"; } void Scenario5() // The manager views a list of all motors having a // specific voltage. { ifstream infile(MOTOR_FILENAME.c_str()); if( !infile.is_open()) { cout << "Cannot open " << MOTOR_FILENAME << endl; return; } system("cls"); cout << "**** Motor Inventory (selective) ****\n\n"; cout << "Enter two values separated by a space: \n" "(1) A motor voltage that will be used to \n" " select motors for display from the Inventory file,\n" "(2) A tolerance value (in volts) for accuracy of comparison\n\n" "> "; double voltage, tolerance; while(true) { cin >> voltage >> tolerance; if( cin.fail()) { // cin.clear(); cin.ignore(255,'\n'); cout << "Bad input--try again: "; } else break; } // Begin searching for motors that match the target // voltage. int lineCount = 0; CMotor aMotor; while( !infile.eof() ) { aMotor.ReadFromFile(infile); // Find the absolute value of the difference between // the current motor's voltage and the target voltage. // If the difference is within the user-specified limit, // assume that we have a matching motor. if( fabs(aMotor.get_Voltage() - voltage) <= tolerance ) aMotor.Display(); lineCount++; if( lineCount % 22 == 0 ) // pause every 22 lines wait(); } } int menu() { system("cls"); cout << "***** Motor Inventory: Main Menu *****\n\n" << "Select a Scenario:\n\n" "1: Manager appends a new motor to the motor inventory file\n" "2: The manager views a list of all motors in the\n" " motor inventory file\n" "3: The manager searches for a motor in the motor\n" " inventory file using the motor's ID number\n" "4: The manager adds a new motor to the inventory file if\n" " and only if the motor is not already in the file\n" "5: The manager views a list of all motors having a\n" " specific voltage.\n" "9: Quit program\n\n" "> "; int choice; while(true) { cin >> choice; if( cin.fail()) { // cin.clear(); cin.ignore(255,'\n'); cout << "Bad input--try again: "; } else break; } return choice; } int main(void) { while(true) { int choice = menu(); switch( choice ) { case 1: Scenario1(); break; case 2: Scenario2(); break; case 3: Scenario3(); break; case 4: Scenario4(); break; case 5: Scenario5(); break; case 9: return 0; } wait(); } }