GA Coding Question for Python Classes ( 1 hr )
Program Specifications Write a basic Telephone class to support basic
operations such as making/receiving phone calls, receiving messages, and
finding the cost of using the phone after making calls. [Link] is
provided with function stubs. Follow each step to gradually complete all
instance methods.
Step 0: Complete the constructor to:
- Initialize the telephone’s phone number and cost per phone-call with the
values of the parameters.
- Initialize the telephone’s received voicemail messages to an empty list
and its data spend so far to 0.
- By default, the phone number is initialized to "1-555-555-5555", and the
cost per phone call is 2.25. Note the provided constant variable
indicates the data plan as having a maximum cost of $32.00.
Step 1 (1 pts). Complete the instance methods to retrieve the phone number,
get the voicemail messages, get the cost per call, and get the phone’s data-
spend so far.
Step 2 (2 pt). Complete the make_a_call(self, recipient) instance method to
output the following statement with the current phone number and the
recipient parameter (recipient would be passed as another telephone object):
1-555-555-5555 is dialing <recipient’s phone number>.
Waiting…
Step 3 (2 pt). Complete the receive_call(self, caller) instance method to output
the following statement.
1-555-555-5555 is receiving a call from <caller’s phone
number>. Answer or decline?
Prompt for user input (can use python input() function) to get the user’s
choice.
If they answer, increase the data-spend so far for both this phone and the
calling phone by the value of each of their cost-per-call attributes.
If they decline, add to this telephone’s voicemail messages an entry of
“Received a missed call from <caller’s phone number>”
Step 4 (1 pts). Complete the read_voicemail(self) instance method. Output
the telephone’s list of voicemail messages, i.e.
Message 1: <message>
...
Step 5 (2 pts). Update make_a_call() to check if either the caller or recipient
telephones have gone over the given DATA_LIMIT value. If either one of them
has, the call should not be made and the recipient phone should have a
voicemail message added of “Failed to receive call, your data spend is over
the limit.”
Step 6 (1 pts). Add a boolean attribute to indicate if the telephone is set to ‘do
not disturb’ or not.
1) Complete the toggle_do_not_disturb(self) instance method to set the
boolean attribute to True if it is False, and set it to False if it is True.
2) Update the constructor to start with do-not-disturb being off .
3) Update receive_call(self, caller) to skip prompting the user and just update
the voicemail messages if do-not-disturb is on.
Step 7 (1 pt) Add a dictionary attribute that represents the phone’s contacts,
where phone numbers are paired to names (i.e “1-234-567-8901” : “John
Smith”). Initialize the contacts dictionary to be empty in the constructor.
Complete the add_contact(self, entry) method to add the contact entry to the
phone. Update the make_a_call() and receive_a_call() to print the contact’s
name instead of their number if the number of the recipient or caller
telephones is found in this phone’s contacts.
Sketch Code [Link] to start with
DATA_PLAN = 32.0
class Telephone():
def __init__(self, phone_number="1-555-555-5555", cost_per_call=0.75):
#complete the constructor
pass
#complete the following getter methods
def get_phone_number(self):
pass
def get_voicemail_messages(self):
pass
def get_cost_per_call(self):
pass
def get_current_data_spend(self):
pass
def make_a_call(self, recipient):
# complete the method
pass
def toggle_do_not_disturb(self):
# complete the method
pass
def receive_a_call(self, caller):
# complete the method
pass
def read_voicemail(self):
# complete the method
pass
def add_contact(self, entry):
pass
if __name__ == "__main__":
phone1 = Telephone(phone_number="1-123-456-789", cost_per_call=2.25)
phone2 = Telephone()
for i in range(10):
print("Phone1 Data Spend: ", phone1.get_current_data_spend())
print("Phone2 Data Spend: ", phone2.get_current_data_spend())
phone1.make_a_call(phone2)
phone2.receive_a_call(phone1)
phone2.read_voicemail()