/* Unlist1.cpp */ #include "Unlist1.h" /* class file for UnsortedType */ /*********************************************************************** * MakeEmpty() -- make length empty * * * * Parameters * * void * * Returns * * void * * Precondition * * NONE * * Postcondition * * List is empty. * ***********************************************************************/ void UnsortedType::MakeEmpty() { length = 0; } /*********************************************************************** * UnsortedType() -- make length empty * * * * Parameters * * void * * Returns * * void * * Precondition * * NONE * * Postcondition * * List is empty. * ***********************************************************************/ UnsortedType::UnsortedType() { length = 0; } /*********************************************************************** * IsFull() -- lets us know if the list is full and returns bool * * * * Parameters * * void * * Returns * * bool -- True or False, is the list full or not * * Precondition * * List has been initialized. * * Postcondition * * Function value = (list is full) * ***********************************************************************/ bool UnsortedType::IsFull() const { return (length == MAX_ITEMS); } /*********************************************************************** * LengthIs() -- lets us know what the length of our list is * * * * Parameters * * void * * Returns * * int -- numeric length of list * * Precondition * * List has been initialized. * * Postcondition * * Function value = number of elements in list * ***********************************************************************/ int UnsortedType::LengthIs() const { return length; } /*********************************************************************** * RetrieveItem() -- index through our list of type ItemType and * * compare our employee name to that of the one in * * the list. If they match the ItemType that was * * passed by reference will equal that location in * * our list. * * * * Parameters * * ItemType item -- item that was passed to us * * bool found -- sets found to true if we find what we want * * Returns * * void -- remember found and item can be accessed when done * * Precondition * * List has been initialized and Key member of item is initialized * * Postcondition * * If there is an element someItem whose key matches item's key, * * then found = true and item is a copy of someItem; otherwise * * found = false and item is unchanged. List is unchanged. * ***********************************************************************/ void UnsortedType::RetrieveItem(ItemType &item, bool &found) { bool moreToSearch; /* to make sure we are in list */ int location = 0; /* start at the base of list */ found = false; /* not found yet */ moreToSearch = (location < length); /* set moreto search */ /* loop while we are still in list and have not found what we want */ while (moreToSearch && !found) { /* item.ComparedToName will return an enum and switch on that */ switch (item.ComparedToName(info[location])) { /* keep looping */ case LESS : case GREATER : location++; moreToSearch = (location < length); break; /* great we found it */ case EQUAL : found = true; /* make item = info[location] */ item = info[location]; break; } } } /*********************************************************************** * RetrieveItemByName() -- index through list of type ItemType and * * compare our employee name to that of the one in * * the list. If they match the ItemType that was * * passed by reference will equal that location in * * our list. (NOTE: same as above) * * * * Parameters * * ItemType item -- item that was passed to us * * bool found -- sets found to true if we find what we want * * Returns * * void -- remember found and item can be accessed when done * * Precondition * * List has been initialized and Key member of item is initialized * * Postcondition * * If there is an element someItem whose key matches item's key, * * then found = true and item is a copy of someItem; otherwise * * found = false and item is unchanged. List is unchanged. * ***********************************************************************/ void UnsortedType::RetrieveItemByName(ItemType& item, bool& found) { bool moreToSearch; /* to make sure we are in list */ int location = 0; /* start at the base of list */ found = false; /* not found yet */ moreToSearch = (location < length); /* set moreto search */ /* loop while we are still in list and have not found what we want */ while (moreToSearch && !found) { /* item.ComparedToName will return an enum and switch on that */ switch (item.ComparedToName(info[location])) { /* keep looping */ case LESS : case GREATER : location++; moreToSearch = (location < length); break; /* great we found it */ case EQUAL : found = true; /* make item = info[location] */ item = info[location]; break; } } } /*********************************************************************** * InsertItem() -- adds another item to the list * * * * Parameters * * ItemType item -- item that was passed to us * * Returns * * void * * Precondition * * List has been initialized and List is not full and item is not * * in list. * * Postcondition * * item is in the list * ***********************************************************************/ void UnsortedType::InsertItem(ItemType &item) { /* as long as we are not at max items put another one on the list */ if (LengthIs() < MAX_ITEMS) { info[length] = item; length++; } else cout << " Error -- Tried to insert too many items "; } /*********************************************************************** * DeleteItem() -- Deletes the element whose key matches item's key. * * * * Parameters * * ItemType item -- item that was passed to us * * Returns * * void * * Precondition * * List has been initialized and Key member of item is initialized * * and One and only one element in list has a key matching item's * * key. * * Postcondition * * No element in list has a key matching item's key. * ***********************************************************************/ void UnsortedType::DeleteItem(ItemType item) { int location = 0; /* once they equal the same thing remove it */ while (item.ComparedToName(info[location]) != EQUAL) location++; info[location] = info[length - 1]; length--; } /*********************************************************************** * ResetList() -- Initializes current position for an iteration through* * the list. * * * * Parameters * * void * * Returns * * void * * Precondition * * List has been initialized. * * Postcondition * * Current position is prior to list. * ***********************************************************************/ void UnsortedType::ResetList() { currentPos = -1; } /*********************************************************************** * GetNextItem() -- Gets the next element in list. * * * * Parameters * * ItemType item -- item that was passed to us * * Returns * * void * * Precondition * * List has been initialized and current position is defined and * * element at current position is not last in list. * * Postcondition * * Current position is updated to next position and item is a copy * * of element at current position. * ***********************************************************************/ void UnsortedType::GetNextItem(ItemType& item) { currentPos++; item = info[currentPos]; } /*********************************************************************** * IncreaseRank() -- Increase the rank * * * * Parameters * * StrType companyname -- StrType that was passed to us * * Returns * * void * * Precondition * * just make sure you have selected a valid employee and company * * Postcondition * * rank will move up by one and readjust the list * ***********************************************************************/ void UnsortedType::IncreaseRank(StrType companyname) { int listlength = LengthIs(); /* index through until we find what we want */ for (int i = 0; i < listlength; i++) { if (info[i].GetCompany() == companyname) info[i].IncreaseRank(); } } /*********************************************************************** * DecreaseRank() -- Decrease the rank * * * * Parameters * * StrType companyname -- StrType that was passed to us * * Returns * * void * * Precondition * * just make sure you have selected a valid employee and company * * Postcondition * * rank will move down by one and readjust the list * ***********************************************************************/ void UnsortedType::DecreaseRank(StrType companyname) { ItemType anItem; int listlength = LengthIs(); /* index through until we find what we want */ for (int i = 0; i < listlength; i++) { if (info[i].GetCompany() == companyname) info[i].DecreaseRank(); } } /*********************************************************************** * PromoteRank() -- Promote the rank * * * * Parameters * * StrType companyname -- StrType that was passed to us * * int theRank -- the rank passed to us * * bool found -- if found this will contain true else false * * Returns * * void -- remember found and item can be accessed when done * * Precondition * * just make sure you have selected a valid employee and company * * and you are promoting to a valid rank * * Postcondition * * rank will move up to theRank of this user * ***********************************************************************/ void UnsortedType::PromoteRank(StrType companyname, int theRank, bool &found) { found = false; int listlength = LengthIs(); /* index through untill we find the company and theRank */ for (int i = 0; i < listlength;i++) { if ((info[i].GetCompany() == companyname) && (info[i].GetRank() == theRank)) { info[i].IncreaseRank(); found = true; } } } /*********************************************************************** * DemoteRank() -- Demote the rank * * * * Parameters * * StrType companyname -- StrType that was passed to us * * int theRank -- the rank passed to us * * bool found -- if found this will contain true else false * * Returns * * void -- remember found and item can be accessed when done * * Precondition * * just make sure you have selected a valid employee and company * * and you are demoting to a valid rank * * Postcondition * * rank will move down to theRank of this user * ***********************************************************************/ void UnsortedType::DemoteRank(StrType companyname, int theRank, bool &found) { found = false; int listlength = LengthIs(); /* index through untill we find the company and theRank */ for (int i = 0; i < listlength;i++) { if ((info[i].GetCompany() == companyname)&& (info[i].GetRank() == theRank)) { info[i].DecreaseRank(); found = true; } } } /*********************************************************************** * ChangeEntry() -- Copy otheritem.company and otheritem.rank into me * * * * Parameters * * ItemType otheritem -- ItemType that was passed to us * * Returns * * void * * Precondition * * make sure otheritem has valid fields and that name exists in * * the list your copying to * * Postcondition * * otheritem.company and otheritem.rank will copy to this item * ***********************************************************************/ void UnsortedType::ChangeEntry(ItemType otheritem) { int i = 0; bool found = false; int listlength = LengthIs(); /* find the employee */ while (!found && i < listlength) { if (info[i].GetName() == otheritem.GetName()) found = true; else i++; } /* change the employee */ if (found) { info[i].SetCompany(otheritem.GetCompany()); info[i].SetRank(otheritem.GetRank()); } } /*********************************************************************** * Payday() -- run through the whole list and pay the employees * * * * Parameters * * void * * Returns * * void * * Precondition * * there is a list and it has employees * * Postcondition * * everyone's totalearnings go up * ***********************************************************************/ void UnsortedType::Payday() { ItemType anItem; int listlength = LengthIs(); /* hop through and pay everyone */ for (int i = 0; i < listlength;i++) { info[i].PayEmployee(); } } /*********************************************************************** * DisplayEmployees() -- run through the whole list and print the names* * of the employees. do this to the screen and * * the file. not the unemployed though. * * * * Parameters * * StrType theCompany -- the company to print for * * ofstream employeeFile -- the output file to print to * * Returns * * void * * Precondition * * there is a list and it has employees * * Postcondition * * employees are printed to screen and file * ***********************************************************************/ void UnsortedType::DisplayEmployees(StrType theCompany, ofstream &employeeFile) { ItemType anItem; int listlength = LengthIs(); /* print people with a company */ for (int i = 0; i < listlength;i++) { if (info[i].GetCompany() == theCompany) { info[i].PrintEmployee(); info[i].PrintEmployeetoFile(employeeFile); } } } /*********************************************************************** * DumpEmployees() -- run through the whole list and print the names * * of the employees do this to the screen and the * * file. the major difference between this and above* * is we print the unemployed too. * * * * Parameters * * ofstream employeeFile -- the output file to print to * * Returns * * void * * Precondition * * there is a list and it has employees * * Postcondition * * everyone is printed to screen and file * ***********************************************************************/ void UnsortedType::DumpEmployees(ofstream &employeeFile) { int i; ItemType anItem; int listlength = LengthIs(); cout << endl << "Employed Individuals\n\n"; employeeFile << endl << "Employed Individuals\n\n"; /* print people with a company */ for (i = 0; i < listlength;i++) { if (!(info[i].IsUnemployed())) { info[i].PrintEmployee(); info[i].PrintEmployeetoFile(employeeFile); } } cout << endl << "Unemployed Individuals\n\n"; employeeFile << endl << "Unemployed Individuals\n\n"; /* print people without a company */ for (i = 0; i < listlength;i++) { if (info[i].IsUnemployed()) { info[i].PrintEmployee(); info[i].PrintEmployeetoFile(employeeFile); } } } /*********************************************************************** * SummarizeResults() -- run through the whole list and print the names* * of the employees and how much they were paid. * * do this to the screen and the file. * * * * Parameters * * ofstream employeeFile -- the output file to print to * * Returns * * void * * Precondition * * there is a list and it has employees * * Postcondition * * everyone is printed to screen and file * ***********************************************************************/ void UnsortedType::SummarizeResults(ofstream &employeeFile) { int listlength = LengthIs(); cout << "Summary:\n"; employeeFile << endl << "Summary:\n"; /* print the employees and their pay */ for (int i = 0; i < listlength;i++) { info[i].GetName().PrintToScreen(false); info[i].GetName().PrintToFile(false,employeeFile); cout << " " << info[i].GetEarnings() << '\n'; employeeFile << " " << info[i].GetEarnings() << '\n'; } } /*********************************************************************** * DecreaseRankforHigherEmployees() -- decrease rank for higher * * employees * * * * Parameters * * StrType companyname -- the company name * * int theRank -- the rank of the employee * * Returns * * void * * Precondition * * make sure the list is populated with the correct employee, and * * company * * Postcondition * * Higher ranking people are decreased * ***********************************************************************/ void UnsortedType::DecreaseRankforHigherEmployees(StrType companyname, int theRank) { int listlength = LengthIs(); /* find higher emplyees and decrease their rank */ for (int i = 0; i < listlength;i++) { if ((info[i].GetCompany() == companyname) && (info[i].GetRank() > theRank)) { info[i].DecreaseRank(); } } }