4.4.  Exercise: Relationships

[ fromfile: lists.xml id: relationships-exercises ]

  1. In these exercises, we will implement some relationships loosely based on Figure 4.2. The diagram is only a starting point - you will need to add some members to the classes in order to complete the assignment.

    1. Implement an Employer::findJobs() function to return a list of all open Positions.

    2. Implement a Person::apply(Position*) function to call Employer::hire(), and return the same result as hire, if successful.

    3. Have the Employer::hire() function randomly return false half of the time, to make things interesting.

    4. For the Person methods which return information about employment, be sure to handle the case where the Person is not employed yet, and return something meaningful.

    5. Create some more test Employer objects (Galactic Empire and Rebel Forces), Person objects (Darth Vader, C3PO, Data), and Position objects (Tie Fighter Pilot, Protocol Android, Captain) in your client code.

    6. Define a static QList<Person*> sm_unemploymentLine in class Person, and ensure that all persons without a job are on it.

    7. Make up some funny job application scenarios, and run your program to determine whether they are successful.

  2. Figure 4.4.  Contacts

    Contacts

    1. Figure 4.4 describes a data model for a Contact system. ContactList can derive from or reuse any Qt container that you wish, as long as it supports the operations listed.

      • getPhoneList(int category) accepts a value to be compared with a Contact's category member for selection purposes. It returns a QStringList containing, for each selected Contact, the name and phoneNumber, separated by the tab symbol: “\t”.

      • getMailingList() has a similar selection mechanism and returns a QStringList containing address label data.

    2. Write a ContactFactory class that generates random Contact objects. Example 4.2 contains a substantial hint.

      Example 4.2. src/containers/contact/testdriver.cpp

      [ . . . . ]
      
      void createRandomContacts(ContactList& cl, int n=10) {
          static ContactFactory cf;
          for (int i=0; i<n; ++i) {
              cf >> cl; 1
          }
      }
      

      1

      adds a Contact into the ContactList

      There are many ways to generate random names/addresses. One way is to have the ContactFactory create lists of typical first names, last names, street names, city names, and so forth. When it is time to generate a Contact, it can pick a random element from each list, add randomly generated address numbers, zip codes, etc. We demonstrate the use of the random function in Section 1.12.3

    3. Write client code to test your classes. In particular, the client code should generate some random contacts. After that, it should test the two query methods (getPhoneList() and getMailingList()) to ensure that they return the proper sub-lists. Print the original list, as well as the query results, to the standard output. Summarize the results by listing the number of elements in the original ContactList compared to the query results.