#include "AccountListList-new.h" #include "AccountListLink.h" int PickRand(int max) { int random = rand() % max; return random; } int main() { // part for assignment - testing new operators char wait; // create empty lists and show them cout << "create empty lists and show them" << endl; AccountList myList; cout << "myList has " << myList.size() << " elements " << endl; myList.ShowList(); AccountList myOtherList; cout << "myOtherList has " << myOtherList.size() << " elements " << endl; myOtherList.ShowList(); cout << "Waiting for you - type char and return> " << flush; cin >> wait; int cnt; // add five accounts to both lists, depositing a random amount between 0 and 1999 in each for (cnt = 0; cnt < 5; cnt++) { Account newOne; int amount = PickRand(2000); newOne.Deposit(amount); myList.push_back(newOne); Account newTwo; amount = PickRand(2000); newTwo.Deposit(amount); myOtherList.push_back(newTwo); } // show the lists then wait cout << "show two lists with random acct#s and random deposits" << endl; cout << "myList has " << myList.size() << " elements " << endl; myList.ShowList();; cout << "myOtherList has " << myOtherList.size() << " elements " << endl; myOtherList.ShowList(); cout << "Waiting for you - type char and return> " << flush; cin >> wait; // test += operator myList += myOtherList; // show that myList has changed and myOtherList has not cout << "test += operator - show that myList has changed and myOtherList has not" << endl; cout << "myList has " << myList.size() << " elements " << endl; myList.ShowList();; cout << "myOtherList has " << myOtherList.size() << " elements " << endl; myOtherList.ShowList(); cout << "Waiting for you - type char and return> " << flush; cin >> wait; // test getNth functions cout << "test getNth functions" << endl; for (cnt = 0; cnt < myOtherList.size(); cnt++ ) { // get it Account nth = myOtherList.GetNthAccount(cnt); // display it cout << "Using GetNthAccount - myOtherList Account: " << cnt << " " << nth << endl; } for (cnt = 0; cnt < myOtherList.size(); cnt++ ) { // get it AccountLink * nthPtr = myOtherList.GetNthAccountLinkPtr(cnt); // display it cout << "Using GetNthAccountLinkPtr - myOtherList Account: " << cnt << " " << *nthPtr << endl; } // wait cout << "Waiting for you - type char and return> " << flush; cin >> wait; // remove 7th element of myList - simultaneously demonstrating that myOtherList is not affected - because // *copy* of it was appended to myList cout << "test remove function - show that myList has changed (element 7 gone) and myOtherList has not" << endl; Account toRemove = myList.GetNthAccount(7); bool ok = myList.remove(toRemove); cout << "remove ok: " << ok << endl; // show that myList has changed and myOtherList has not cout << "myList has " << myList.size() << " elements " << endl; myList.ShowList();; cout << "myOtherList has " << myOtherList.size() << " elements " << endl; myOtherList.ShowList(); cout << "Waiting for you - type char and return> " << flush; cin >> wait; // remove first and last elements of myOtherList - simultaneously demonstrating that myList is not affected - because // *copy* of myOtherList was appended to myList cout << "test remove function special cases - show that myOtherList has changed (first and last elements gone) and myList has not" << endl; toRemove = myOtherList.GetNthAccount(0); myOtherList.remove(toRemove); int newSize = myOtherList.size(); AccountLink * toRemovePtr = myOtherList.GetNthAccountLinkPtr(newSize-1); myOtherList.remove(toRemovePtr->GetValue()); // show that myList has changed and myOtherList has not cout << "myList has " << myList.size() << " elements " << endl; myList.ShowList();; cout << "myOtherList has " << myOtherList.size() << " elements " << endl; myOtherList.ShowList(); cout << "Waiting for you - type char and return> " << flush; cin >> wait; // check sort myList.sort(); // show that myList has changed and myOtherList has not cout << "test sort - show that myList has changed and myOtherList has not" << endl; cout << "myList has " << myList.size() << " elements " << endl; myList.ShowList();; cout << "myOtherList has " << myOtherList.size() << " elements " << endl; myOtherList.ShowList(); cout << "Waiting for you - type char and return> " << flush; cin >> wait; return 0; /* // below part posted to www already char wait; AccountList myList; cout << "myList has " << myList.size() << " elements " << endl; myList.ShowList(); int cnt; for (cnt = 0; cnt < 3; cnt++) { Account newOne; myList.push_back(newOne); cout << "myList has " << myList.size() << " elements " << endl; myList.ShowList(); } cout << "Waiting for you - type char and return> " << flush; cin >> wait; // try depositing random amount between 0 and 2000 // dont have iterators - must deal with pointers to nodes (called AccountLink s) AccountLink * acctPtr; for (acctPtr = myList.begin(); acctPtr != 0; acctPtr = acctPtr->Next() ) { int amount = PickRand(2000); acctPtr->GetValue().Deposit(amount); } cout << "myList has " << myList.size() << " elements " << endl; myList.ShowList(); cout << "Waiting for you - type char and return> " << flush; cin >> wait; // save a copy of the list of accounts for safe keeping AccountList listCopy(myList); // now add some to the front for (cnt = 0; cnt < 3; cnt++) { Account newOne; myList.push_front(newOne); cout << "myList has " << myList.size() << " elements " << endl; myList.ShowList(); } cout << "Waiting for you - type char and return> " << flush; cin >> wait; cout << "Removing " << myList.back() << " from list " << endl; myList.pop_back(); cout << "myList has " << myList.size() << " elements " << endl; myList.ShowList(); cout << "Waiting for you - type char and return> " << flush; cin >> wait; cout << "listCopy has " << listCopy.size() << " elements " << endl; listCopy.ShowList(); return 0; */ }