Create a class MedicalRecord
that holds private attributes for a patient’s medical history. Include methods to add and retrieve medical entries, making sure that the data is validated.
Example 1:
Input: Add: ("2022-02-02", "Flu"), Retrieve: "2022-02-02" Output: ("2022-02-02", "Flu")
Example 2:
Input: Add: ("2023-02-30", "Cold") Output: "Invalid date"
Use a dictionary to maintain the medical history and validate the dates when adding new entries.
<pre>
import datetime
class MedicalRecord:
def __init__(self):
self._record = {}
def add_entry(self, date, diagnosis):
try:
datetime.datetime.strptime(date, ‘%Y-%m-%d’)
self._record[date] = diagnosis
except ValueError:
return “Invalid date”
def retrieve_entry(self, date):
return self._record.get(date, “Entry not found”)
# Test the class
record = MedicalRecord()
record.add_entry(“2022-02-02”, “Flu”)
print(record.retrieve_entry(“2022-02-02”)) # Output: Flu
print(record.add_entry(“2023-02-30”, “Cold”)) # Output: Invalid date
</pre>
Unlock AI & Data Science treasures. Log in!