|
|
What kind of representation should we use for Appts? Since even a busy executive probably has fewer than a dozen appointments in a single day, a linear structure would be quite appropriate, so we will use a Block of Appointments:
#include <Block.h> class Appts { Block<Appointment> appts; int nappts; public: Appts() : nappts(0) {} void add(const Appointment& a) { appts.reserve(nappts); appts[nappts++] = a; } };Notice that we also maintain the number of appointments; this is because Blocks (like arrays) do not keep track of the number of cells occupied by meaningful data.
Keeping the Appointments for a given Date sorted would simplify matters, because that's the way we have been asked to display them. To do this, we can use an algorithm from Array_alg(C++). The algorithm insert() requires that its element type have a total order relation defined by operator< (its manpage tells us this), so we must add one to Appointment:
int operator<(const Appointment& a, const Appointment& b) { return a.time < b.time; }Now we can use insert() to add an Appointment to the Block:
#include <Array_alg.h> class Appts { public: void add(const Appointment& a) { appts.reserve(nappts); insert(&a[0], &appts[0], &appts[nappts]); ++nappts; } // ... };Finally, we will also have need of the following functions:
class Appts { int num() const { return nappts; } const Appointment& operator[](int i) const { return appts[i]; } // ... };The first returns the number of Appointments in the collection, and the section returns a reference to the i'th one. As with the extra functions in Calendar, these functions were added only when their utility became obvious.