Create a list of employee and save it on external file your program have the following menu

Create a list of employee and save it on external file your program have the following menu:
(1)Add employee
(2)Edit employee
(3)Search Employee
(4)Quit
Each menu item must preform its designated function.


Solution: 


 #include < iostream >
#include < vector >
#include < stdio.h >
#include < stdlib.h >

using namespace std;

vector < string > employeeNames;
bool debug = false;
bool running = true;

void clearTerminalScreen( ) {
system( "clear" ); 
}

void removeAtIndex( vector < string > *list, unsigned int index ) {
vector < string > originalList = *list;
vector < string > updatedList;

for ( unsigned int i = 0; i < originalList.size(); ++i ) {
if ( i != index ) {
updatedList.push_back( originalList[ i ] );
}
}

*list = updatedList;
}

unsigned int getWordSize( char *word ) {
unsigned int size = 0;
char current = word[0];
unsigned int index = 0;

while ( current != '\0' ) {
index = index + 1;
size = size + 1;
current = word[ index ];
}

return size;
}

bool wordExistsInString( string base, char *term ) {
bool result = false;
unsigned int charSize = getWordSize( term );
const char *base_c = base.c_str();

for ( unsigned int i = 0; i < base.size(); ++i ) {
if ( base_c[ i ] == term[ 0 ] ) {
unsigned int matchScore = 0;
unsigned int checkPoint = i;
if ( ( checkPoint + ( charSize - 1 ) ) < base.size() ) {
if ( debug ) printf( "Size check result passed at %u\n", i );
for ( unsigned int j = 0; j < charSize; ++j ) {
if ( base_c[ checkPoint ] == term[ j ] ) matchScore++;
checkPoint++;
}
if ( debug ) printf( "Matching Result: %u / %u\n", matchScore, ( charSize ) );
if ( matchScore == charSize ) result = true;
}
}
}

return result;
}

void insertEmployee( ) {
clearTerminalScreen();
char *first_name, *last_name;

first_name = new char();
last_name = new char();

printf( "Insert Employee:\n\n" );
printf( "First Name $ " );
scanf( "%s", first_name );
printf( "Surname $ " );
scanf( "%s", last_name );

char *employee_name = new char();
sprintf( employee_name, "%s %s", first_name, last_name );
employeeNames.push_back( employee_name );

printf( "\nInsert successful!\n" );
}

unsigned int getSelectionFromEmployeeList( ) {
for ( unsigned int i = 0; i < employeeNames.size(); ++i ) {
printf( "%u %s\n", i, employeeNames[ i ].c_str() );
}

bool ID_isValid = false;
int selectedID = -1;

printf( "\n(if you want to cancel this operation type -1)\n" );

while ( !ID_isValid ) {
printf( "ID $ " );
scanf( "%i", &selectedID );

if ( selectedID > = 0 && selectedID < employeeNames.size() ) {
unsigned int selection = -1;
bool selectionIsValid = false;

printf( "You have selected %s, would you like to proceed? < 1 - yes / 0 - no > \nContinue $ ", employeeNames[ selectedID ].c_str() );

while ( !selectionIsValid ) {
scanf( "%u", &selection );
if ( selection == 0 || selection == 1 ) selectionIsValid = true;
else printf( "Invalid response!\n(yes = 1 / no = 0)\nContinue $ " );

}

if ( selection == 1 ) ID_isValid = true;
} else if ( selectedID == -1 ) {
ID_isValid = true;
}
}
return selectedID;
}

void editEmployee( ) {
clearTerminalScreen();
printf( "Edit Employee: \n\n" );

unsigned int selectedID = getSelectionFromEmployeeList();

if ( selectedID != -1 ) {
char *first_name, *last_name;

first_name = new char();
last_name = new char();

printf( "Selected Employee: %s\n\n", employeeNames[ selectedID ].c_str() );
printf( "First Name $ " );
scanf( "%s", first_name );
printf( "Surname $ " );
scanf( "%s", last_name );

char *employee_name = new char();
sprintf( employee_name, "%s %s", first_name, last_name );
employeeNames[ selectedID ] = employee_name;

printf( "Edit Successful!\n" );
} else {
printf( "Operation cancelled!\n" );
}
}

void removeEmployee( ) {
printf( "Remove Employee: \n\n" );
unsigned int selectedID = getSelectionFromEmployeeList( );

if ( selectedID != -1 ) {
removeAtIndex( &employeeNames, selectedID );
printf( "Delete successful!\n" );
} else {
printf( "Operation cancelled!\n" );
}

}

void listEmployees( ) {
clearTerminalScreen();
printf( "Display Employees: \n\n" );
for ( unsigned int i = 0; i < employeeNames.size(); ++i ) {
printf( "%u : %s\n", i, employeeNames[ i ].c_str() );
}
}

void showHelp( ) {
clearTerminalScreen();
printf( "Employee Name Management v1.0.0\nCreated by S. Emily\n\nCommands:\n\n1) Insert Employee\n2) Edit Employee\n3) Remove employee\n4) Search\n5) Show this again\n6) List Employees\n7) Exit\n ");
}

void searchEmployee( ) {
clearTerminalScreen();
printf( "Search Employees: \n\n" );
printf( "Name $ " );

char *searchTerm = new char();
unsigned int nResults = 0;

scanf( "%s", searchTerm );
printf( "Showing results for '%s': \n\n", searchTerm );

for ( unsigned int i = 0; i < employeeNames.size(); ++i ) {
if ( wordExistsInString( employeeNames[ i ], searchTerm ) ) {
printf( "%s\n", employeeNames[ i ].c_str() );
nResults ++;
}
}

printf( "\n%u results found!\n\n", nResults );
}

int main( ) {
showHelp( );

while ( running ) {
unsigned int choice;

printf( "\nAction $ " );
scanf( "%u", &choice );

switch ( choice ) {
case 1: insertEmployee(); break;
case 2: editEmployee(); break;
case 3: removeEmployee(); break;
case 4: searchEmployee(); break;
case 5: showHelp(); break;
case 6: listEmployees(); break;
case 7: running = false; break;
default: 
printf( "Invalid selection, type 5 to list the commands\n" );
break;
}

}

return 0;
}

No comments