CS 250 Computer Programming and Problem Solving - FALL 1998 

[ Home | Syllabus | Notes | Glossary | CS250 WEB Version Home Page ]


Design Notes: Or why I change the examples in the book



FixedString Class

Why did I change the FString example in the book?


Doctor Appointment Schedule

 


Scenarios

Let's examine a few scenarios for the Doctor Appointment Problem:

Scenario 1:

  1. Patient calls Scheduler for an Appointment
  2. Scheduler gives patient list of doctors and asks which one the patient wants to see.
  3. Patient chooses doctor
  4. Scheduler shows available appointment times
  5. Patient chooses a time
  6. Scheduler makes an appointment at that time for that patient with that doctor.

Scenario 2:

  1. Patient calls Scheduler for an Appointment
  2. Scheduler gives patient list of doctors and asks which one the patient wants to see.
  3. Patient chooses doctor and time
  4. Scheduler says doctor is busy at that time
  5. Patient picks another time
  6. Scheduler makes an appointment at that time for that patient with that doctor.

Scenario 3:

  1. Patient calls Scheduler for an Appointment
  2. Scheduler gives patient list of doctors and asks which one the patient wants to see.
  3. Patient chooses doctor and time
  4. Scheduler says doctor is busy at that time
  5. Patient picks another doctor
  6. Scheduler makes an appointment at that time for that patient with that doctor.

Scenario 4:

  1. Scheduler saves Appointments in a file to be printed for each doctor.

Scenario 5:

  1. Patient calls Scheduler to change an Appointment, gives name
  2. Scheduler looks up Appointment
  3. Scheduler removes Appointment
  4. Scheduler notifies Doctor

Of course there are many other scenarios: we want the most critical ones first

Going through each scenario will bring up issues to be resolved and problems to be solved.

For instance in scenario 1:

In solving these problems, various design decisions will be made and responsibilites assigned to the objects we identified earlier.


Where to go Next?

Pick any scenario (say 1 which is probably the most common) and begin to make decisions which assign functions and properties to the objects mentioned in this scenario.

So we have a bunch of HAS-A relationships between these objects.

But how exactly should these relationships be expressed? for example:

Another issue: should this information be crossed referenced? For exmaple,

 


Now What?

Once we have a reasonable understanding of the objects and their relationship, we can begin to design them. There are a number of basic approaches

I'll try a top down, incremental design to cover a simplified scenario 1.
Simplified because only one doctor.

 


//######################
// Scheduler is responsible for making appointments with patients
// and recording them in the doctor's daily schedule
// and Printing out these schedules
// keeps a reference to all doctors for this purpose
//######################

class Scheduler {
public:
  Scheduler( Doctor * docs, unsigned NumDoctors);
  // Scheduler keeps track of appointments for all doctors
  // A list of doctors is passed and a reference to this list is kept
  // in this object

  void PrintAllAppointments( const char * fileName );
  // PRE: fileName is a legtimate file name
  // POST: the daily schedule of all doctors is printed to "fileName"

  bool ScheduleOneAppointment();
  // Makes an appointment for one patient
  //POST: If there is another patient, schedules an appoitment for that patient
  // and returns true
  // else returns false (meaning, that the day is over - go home)

  void ScheduleAllAppointments(); 
  // runs all day scheduling appointments
  // returns when all patients have be scheduled for this day

private:
  Doctor * doctors;  // points to first element in array of doctors hptrd_left.gif (955 bytes)
  // notice that there is only one copy of the doctor object, but we can
  // refer to it using the pointer and so can update the doctor/s schedule
  // we could have made a copy of the doctor objects - 
  // but then they could not be accessed or changed outside (consistently)
 unsigned NumDoctors; // keeps size of array of Doctors
};

//................ Scheduler .................

// Initialize 
// array of doctors from a file.

Scheduler::Scheduler( Doctor * docs, unsigned num )
{
  doctors = docs; hptrd_left.gif (955 bytes)
  NumDoctors = num;
  ifstream dnameFile( "doctors.txt" );
  if( !dnameFile ) hptrd_left.gif (955 bytes)
  {
    cout << "Cannot open input file! Aborting program.\n";
    abort;
  }
  FixedString temp;
  for(unsigned i = 0; i < NumDoctors; i++)
  {
	dnameFile >> temp; hptrd_left.gif (955 bytes)
    doctors[i]hptrd_left.gif (955 bytes).SetName( temp ); 
  }
}

// Write each doctor's list of appointments,
// showing time and patient name for each.

void Scheduler::PrintAllAppointments(const char * fileName)
{
  ofstream ofile( fileName );
  if( ofile )
    for(unsigned i = 0; i < NumDoctors; i++)
      doctors[i].ShowAppointments( ofile ); hptrd_left.gif (955 bytes)
}


void Scheduler::ScheduleAllAppointments()
{
  while( ScheduleOneAppointment() ) hptrd_left.gif (955 bytes)
    continue;
}

// Choose a doctor, input patient name, and
// ask for a particular time slot. Make the
// appointment, and display the results.

int Scheduler::ScheduleOneAppointment()
{
  // Get patient name, let patient request a doctor.

  Patient aPatient;
  aPatient.InputName(); hptrd_left.gif (955 bytes)
  // Show patient list of doctor's names and let patient choose one.
  //unsigned doctorNum = aPatient.ChooseDoctor();
  doctorNum = 0; //?????? for testing always choose the first doctor hptrd_left.gif (955 bytes)

  Doctor & theDoc = doctors[doctorNum]; hptrd_left.gif (955 bytes)

  while( !aPatient.IsScheduled() ) hptrd_left.gif (955 bytes)
  {
    // Patient chooses a time slot. Construct an appointment
    // from the time slot, doctor number, and patient name.

    TimeSlot aTime = aPatient.ChooseTimeSlot( theDoc ); hptrd_left.gif (955 bytes)
    Appointment app( aTime, doctorNum, aPatient  );

    // Try to schedule the patient for a particular time
    // slot with the chosen doctor. If successful, add the
    // appointment time to the patient's record; if not,
    // display an error message.

    if( theDoc.AddToSchedule( app )) hptrd_left.gif (955 bytes)
      aPatient.SetAppointment( app ); hptrd_left.gif (955 bytes)
    else
      cout << "Sorry, Dr. " << theDoc.GetLastName()
           << " is not available at that time. " << endl;
  }
  cout << aPatient;
  return getYN("Continue?"); hptrd_left.gif (955 bytes)
}

Copyright chris wild 1998.
For problems or questions regarding this website contact [Chris Wild (e-mail:wild@cs.odu.edu].
Last updated: September 24, 1998.