1 / 70

Time Tracking for Patient Waiting Times

A program that allows a doctor's office to track the amount of time patients spend waiting for appointments, using a Time ADT. The program handles user input in the format of HH:MM:AM/PM and calculates total waiting time and average wait time.

kerrygreen
Télécharger la présentation

Time Tracking for Patient Waiting Times

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4 Abstract Data Types, Classes and Objects

  2. Representing Time • Example: a doctor’s office wants to keep track of the amount of time a patient spends waiting for an appointment. • A program could be written to do this, but there are a few problems that must be overcome in its design.

  3. Example: Time The easiest way for a program to handle time (int) is hard for the user int the_time; cout << “Enter the patient arrival time as the number of minutes since midnight:” ; cin >> arrival;

  4. Time as an int • If time is represented as an int however, many operations become easy to perform. • Arrival = 1234 minutes • SeenByDoctor = 1264 minutes • Wait = SeenByDoctor-Arrival = 30 minutes

  5. Patient Waiting Times coded as int (minutes) • #include <iostream.h> • // Somehow, the type "Time" gets defined up here • int main() • { • int numberOfVisits(0), totalWaitingTime(0); • char answer; • do { • Time arrival, seenByDoctor; • cout << "Enter arrival time:"; • cin >> arrival; • cout << "Enter time seen by doctor:";

  6. Code Example: Patient Waiting Times • cin >> seenByDoctor; • numberOfVisits++; • // assume that subtracting one Time from another yields the • // difference in minutes as an int • totalWaitingTime += seenByDoctor - arrival; • cout << "Done? Enter 'y' to quit, anything else to continue: "; • cin >> answer; • } while (answer != 'y'); • cout << "Number of visits: " << numberOfVisits << "\n"; • cout << "Total waiting time: " << totalWaitingTime << " minutes.\n"; • cout << "Average wait is " << totalWaitingTime/numberOfVisits • << " minutes.\n"; • return 0; • }

  7. Other forms of Time • Although the last program is easy to understand, it would require the user to transform the times into minutes before entering them. • This is a BAD idea. • User’s should enter data in the form they are most comfortable with. • For time it is usually HH:MM:AM/PM

  8. Sample Input and Output from Patient Waiting Times Program • Enter arrival time: 12:17PMEnter time seen by doctor: 12:25PMDone? Enter 'y' to quit, anything else to continue: .Enter arrival time: 1:10PMEnter time seen by doctor: 2:02PMDone? Enter 'y' to quit, anything else to continue: yNumber of visits: 2Total waiting time: 60 minutes.Average wait is 30 minutes.

  9. Time and the C++ Program Designer • So, the program designer’s goal is to meet both the needs of the user and of the programmer. • We want our program to work in a manner similar to the program code was have seen, and • We want user input to come in the most natural way possible.

  10. Designing a Time ADT • The best way to handle Time would be to provide a Time ADT • It’s class definition would include an integer for hour, and integer for minutes, and an enumerated type for AM/PM • The class should also include functions for reading times and subtracting them to get the difference in minutes.

  11. Using the Time ADT • The users would not have to know anything about the actual class definition. • They just enter Time values in the form they are used to. • The actual implementation of the Time ADT is hidden from them. • This is the idea of abstraction

  12. More on the Time ADT • Our design goal is to come up with a Time ADT class that is • a good abstraction • the concept is familiar to the user • safe • automatically checks for bad input • modifiable • changes made to the ADT won’t hurt client code • reusable • many apps requiring Time can use it

  13. ADTs consist of Characteristics (data state) Operations

  14. Time ADT Characteristics of ‘time’ Operations associated with ‘time’

  15. Time ADT • Characteristics: • A Time consists of some number of hours and minutes, and is either before noon (AM) or after noon (PM). • Twelve Noon is 12:00 PM and Twelve Midnight is 12:00 AM. • All Times are assumed to fall on the same day.

  16. Time ADT • Operations: • Time readTime(bool & errorFlag) • Precondition: Standard Input has characters available. • Postconditions: Leading whitespace characters are ignored; readTime attempts to read, from standard input, a time in the format <HH>:<MM>:<A>, where <HH> is an integer between 1 and 12, <MM> is an integer between 0 and 59, and <A> is either “AM” or “PM”. If a properly formatted time can be read, errorFlag is set to false; otherwise, errorFlag is true. • Returns: If errorFlag is false, the Time read from Standard Input; otherwise, arbitrary time is returned.

  17. Time ADT • int subtractTimes(Time t1, Time t2) • Precondition: t1, t2 are well-defined. • Postcondition: None. • Returns: The difference, in minutes, between Time t1 and Time t2. If t1 occurs before t2, this difference is negative.

  18. Patient waiting time program, revised for our implementation of Time • #include "dslib.h" • #include <iostream.h> • // Somehow, the type "Time" gets defined up here • int main() • { • int numberOfVisits(0), totalWaitingTime(0); • char answer; • do { • bool errorFlag; • Time arrival, seenByDoctor; • cout << "Enter arrival time:"; • arrival = readTime(errorFlag); // Instead of cin >> arrival

  19. Patient waiting time program, revised for our implementation of Time • while (errorFlag) { • cout << "Arrival time was incorrectly formatted; try again: "; • arrival = readTime(errorFlag); • } • cout << "Enter time seen by doctor:"; • seenByDoctor = readTime(errorFlag); // not cin • while (errorFlag) { • cout << "Seen by doctor time was incorrectly formatted; try again: "; • seenByDoctor = readTime(errorFlag); • }

  20. Patient waiting time program, revised for our implementation of Time • numberOfVisits++; • // assume that subtracting one Time from another yields the • // difference in minutes as an int • totalWaitingTime += subtractTimes(seenByDoctor, arrival); • cout << "Done? Enter 'y' to quit, anything else to continue: "; • cin >> answer; • } while (answer != 'y'); • cout << "Number of visits: " << numberOfVisits << "\n"; • cout << "Total waiting time: "<< totalWaitingTime << " minutes.\n"; • cout << "Average wait is " << totalWaitingTime/numberOfVisits • << " minutes.\n"; • return 0; • }

  21. Time ADT, data state # of hours # of minutes AM/PM Assumes all time on the same day

  22. Time ADT operations readTime subtractTimes

  23. ADT operations Each has a signature (prototype) Signatures describe The type of each argument The type of the return value

  24. ADTs and code reuse A good implementation of an ADT like Time should be reusable. Any program that includes your header file and has access to the implementation file can use the Time ADT. Don’t reinvent the wheel Reduces programmer efficiency Drives up development costs Drives up maintenance costs

  25. Implementing a Time ADT See Code example 4-3, page 75 Notice int hour, minute Returntime in minutes Good test questions on page 77

  26. Implementation of Time ADT • #include "dslib.h" • typedef int Time; • Time readTime(bool & errorFlag) • { • // The time must be formatted as <HH>:<MM><AMorPM>, where • // <HH> is an int in the range 0 to 12, <MM> is an int in • // the range 0 to 59, and <AMorPM> is either AM or PM. • enum AM_PM {AM, PM} AM_or_PM; • int hour, minute; • const char delimiter = ':';

  27. Implementation of Time ADT • // Assume that the format is bad -- once valid data is extracted, • // reset errorFlag to false • errorFlag = true; • // formatted input -- fail if not an int • if (!(cin >> hour)) • return 0; • if (hour < 0 || hour > 12) • return 0; • char c; • cin >> c; • if (c != delimiter) • return 0; • if (!(cin >> minute)) // formatted input • return 0;

  28. Implementation of Time ADT • if (minute < 0 || minute > 59) • return 0; • cin >> c; • if (c == 'A' || c == 'a') • AM_or_PM = AM; • else if (c == 'P' || c == 'p') • AM_or_PM = PM; • else • return 0; • cin >> c; • if (c != 'M' && c != 'm') • return 0; • // if the program gets here, the data was correctly formatted -- • // so compute the time.

  29. Implementation of Time ADT • errorFlag = false; • Time returnTime; • if (hour == 12) • returnTime = minute; • else • returnTime = hour*60 + minute; • if (AM_or_PM == PM) • returnTime += 60*12; • return returnTime; • } • int subtractTimes(Time t1, Time t2) • { • return t1 - t2; • }

  30. What C++ allows with ADTs Abstraction Information hiding Encapsulation Code reuse These are the key terms used so far. You should know what each of them means and why it is important.

  31. Basic Definitions Client program a program using an ADT Information hiding the ability of a client to use an ADT without knowledge of its implementation or direct access to it’s data members

  32. Further definitions • Encapsulation • the ability of a language to create ADTs in which the client has no access to the underlying implementation • Class - the C++ vehicle used to construct encapsulated ADTs

  33. Time ADT readTime int minutes subtractTimes Client’s View Implementor’s View (implementation details visible here) (only the operations visible here) Two Views of an ADT, using Time ADT as an example

  34. Example: Time class Public Declares just those operations the client will need Member functions (methods) Private Definitions of the ADT representation Not accessible to client code Data members

  35. Class Header Files Store class definitions Often the same name as the class #include “county.h” // for county class Contains data state definitions The actual code for ADT operations is often put in another file called the ‘implementation file’

  36. Private section denotes restricted access only member functions of the class have access to what is in private (usually data) Public section denotes interface element Public member functions and data are able to be directly accessed by client code. The client programmer must know what the public classes of the ADT are. Public and Private

  37. A class declaration for the Time ADT • class Time { • public: • void readTime(bool & errorFlag); • int subtractTimes(Time t); • private: • int minutes; • };

  38. Commented class definition for Time ADT • class Time { • // • // Characteristics: • // • // A Time consists of some number of hours and minutes, • // and is either before noon (AM) or after noon (PM). • // • // Twelve Noon is 12:00 PM and Twelve Midnight is 12:00 AM. • // • // All Times are assumed to fall on the same day. • //

  39. Full definition file for Time ADT • public: • void readTime(bool & errorFlag); • // Precondition: Standard input has characters available. • // Postconditions: Leading whitespace characters are ignored; • // readTime attempts to read, from standard input, a time in • // the format <HH>:<MM><A>, where <HH> is an integer between • // 1 and 12, <MM> is an integer between 0 and 59, and <A> is • // either "AM" or "PM". If a properly formatted time can be • // read, errorFlag is set to false, and the value of the Time • // variable is set to the time read; otherwise, errorFlag is • // set to true.

  40. Full definition file for Time ADT (con’t) • int subtractTimes(Time t); • // Precondition: This Time variable contains a proper value. • // Postcondition: None. • // Returns: The difference, in minutes, between this Time and Time t. • // If this Time occurs prior to Time t, the returned difference • // is negative. • // **** the rest of the class declaration is private • private: • int minutes; • };

  41. C++ improvements on our Time ADT • Objects are instances of classes (instantiation) • County ramsey; // ramsey is a County object • Every call to a member function of the class must be associated with an object of that class • cout << ramsey.getPop();

  42. The “this” pointer • “this” is a pointer to the object that called a particular class function. • When we call a function • as in: seenByDoctor.subtractTimes(arrival); • seenByDoctor is pointed to by “this”

  43. Time ADT revised consistent with Class syntax • Characteristics: • A Time consists of some number of hours and minutes, and is either before noon (AM) or after noon (PM). • Twelve Noon is 12:00 PM and Twelve Midnight is 12:00 AM. • All Times are assumed to fall on the same day.

  44. Time ADT revised consistent with Class syntax • Operations • void readTime(bool & errorFlag) • Precondition: Standard Input has characters available. • Postconditions: Leading whitespace characters are ignored; readTime attempts to read, from standard input, a time in the format <HH>:<MM><A>, where <HH> is an integer between 1 and 12, <MM> is an integer between 0 and 59, and <A> is either “AM” or “PM”. If a properly formatted time can be read, errorFlag is set to false; otherwise, errorFlag is set to true. • Returns: If errorFlag is false, the value of this Time is the time read from Standard Input; otherwise, the value of this Time remains unchanged.

  45. Time ADT revised consistent with Class syntax • int subtractTimes(Time t1) • Precondition: t1 is well-defined. • Postcondition: None. • Returns: The difference, in minutes, between this Time and Time t1; that is, this Time - t1. If this Time occurs before Time t1, this difference is negative.

  46. Example 4-6

  47. Patient Waiting Times program, revised for C++ Class notation • // cx4-6.cpp • // Patient Waiting Times program, revised for C++ Class notation • #include <iostream.h> • #include "cx4-5.h" • int main() • { • int numberOfVisits(0), totalWaitingTime(0); • char answer; • do { • bool errorFlag; // arrival, seenByDoctor are instances of Time class • Time arrival, seenByDoctor; • cout << "Enter arrival time:"; • arrival.readTime(errorFlag);

  48. Patient Waiting Times program, revised for C++ Class notation • while (errorFlag) { • cout << "Arrival time was incorrectly formatted; try again: "; • arrival.readTime(errorFlag); • } • cout << "Enter time seen by doctor:"; • seenByDoctor.readTime(errorFlag); • while (errorFlag) { • cout << "Seen by doctor time was incorrectly formatted; try again: "; • seenByDoctor.readTime(errorFlag); • } • numberOfVisits++;

  49. Patient Waiting Times program, revised for C++ Class notation • // assume that subtracting one Time from another yields the • // difference in minutes as an int • totalWaitingTime += seenByDoctor.subtractTimes(arrival); • cout << "Done? Enter 'y' to quit, anything else to continue: "; • cin >> answer; • } while (answer != 'y'); • cout << "Number of visits: " << numberOfVisits << "\n"; • cout << "Total waiting time: "<< totalWaitingTime << " minutes.\n"; • cout << "Average wait is " << totalWaitingTime/numberOfVisits • << " minutes.\n"; • return 0; • }

  50. The scope operator :: is used to tell the compiler that a function is a member of a particular class. Most notably, this is part of the function definitions in the implementation file that accompanies your class header file and contains non-inline functions. Implementing C++ classes

More Related