4.4.  Exercise: Relationships

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

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

    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. To make things interesting, have the Employer::hire() function randomly return false half of the time.

    4. For the Person methods that 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*> s_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 model[38] for a Contact system. ContactList can derive from or reuse any Qt container that you like, 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 phone number, 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.4 contains a substantial hint.

      Example 4.4. 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.[39] 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.13.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 sublists. Print the original list and the query results to standard output. Summarize the results by listing the number of elements in the original ContactList compared to the query results.



[38] We discuss models and views in Chapter 13. For now we refer to a data structure that manages information for an application (but not the acquisition, display, or transmission of that information) as a model.

[39] Spam is a great source of names - just browse through your spam folder and grab names from the mail headers or from the message bodies. We have provided a starter list of spam names in the Dist directory.