135k views
2 votes
The Doctor program described in Chapter 5 combines the data model of a doctor and the operations for handling user interaction. Restructure this program according to the model/view pattern so that these areas of responsibility are assigned to separate sets of classes. The program should include a class with an interface that allows one to obtain a greeting. a signoff message, and a reply to a patient's string. To implement the greeting. define a method named for the class. To implement the signoff message, define a method named and should return a string with a greeting or farewell message respectively. The reply function is defined for you, it should be added as a method for the class. The rest of the program, in a separate program module, handles the user's interactions with the object. Develop this program with a terminal-based user interface. Note: The program should output in the following format: Hello, how can I help you today? > I am sick You seem to think that you are sick? > Yes And what do you think about this? > I am not feeling good Note: The program should output in the following format: Hello, how can I help you today? > I am sick You seem to think that you are sick? > Yes And what do you think about this? > I am not feeling good Did I just hear you say that you are not feeling good? > Yes Why do you believe that Yes? > My nose is running I would like to hear more about that. > I have a headache too You seem to think that you have a headache too? > Correct Go on. > It doesn't stop Did I just hear you say that It doesn't stop? > Correct Go on. > That's it And what do you think about this? > quit Have a nice day!

User Kinbiko
by
7.5k points

1 Answer

3 votes

Final Answer:

The Doctor program described in Chapter 5 can be restructured according to the model/view pattern by creating a class for handling the data model (Doctor) and another class for user interaction (UserInterface). The Doctor class should have methods for greeting, signoff message, and a predefined reply method. The UserInterface class will handle the terminal-based user interaction. The program output should follow the specified format for a conversation with the user.

Step-by-step explanation:

To implement the model/view pattern, we create two classes: Doctor and UserInterface. The Doctor class encapsulates the data model, including methods for greeting, signoff message, and a reply method. The UserInterface class manages the interaction with the user through a terminal-based interface. This separation of responsibilities adheres to the model/view pattern, ensuring a clean and maintainable code structure.

In the Doctor class, the `greeting` method outputs the initial greeting, the `signoff_message` method returns a farewell message, and the `reply` method is predefined to handle responses from the patient. In the UserInterface module, the program takes input from the user, calls the appropriate Doctor methods, and displays the formatted output as specified.

The terminal-based interaction follows the conversation format outlined in the question, creating a seamless interaction with the user. This restructuring enhances code organization, making it more modular and easier to extend or modify in the future. The Doctor class focuses on the data model, while the UserInterface class handles the user interaction aspect, adhering to the principles of the model/view pattern for improved code design and maintainability.

User Gommb
by
8.3k points