D'd'd'd'd'd'd'd'd'd'd'd'd'd'd'ddorrigo Mobile Phone Class

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

"""

* Dorrigo Mobile Phone Class.


*
* INFO1110 Assignment, Semester 2, 2020.
*
* dorrigo_mobile
* In this assignment you will be creating an Dorrigo Mobile Phone as part of a simulation.
* The Mobile phone includes several attributes unique to the phone and has simple functionality.
* You are to complete 2 classes. dorrigo_mobile and dorrigo_contact
*
* The phone has data
* Information about the phone state.
* If it is On/Off
* Battery level
* If it is connected to network.
* Signal strength when connected to network
* Information about the current owner saved as contact information.
* First name
* Last name
* Phone number
* A list of 10 possible contacts.
* Each contact stores first name, last name, phone number and chat history up to 20 messages
*
* The phone has functionality
* Turning on the phone
* Charging the phone. Increase battery level
* Change battery (set battery level)
* Use phone for k units of battery (decreases battery level by k)
* Search/add/remove contacts
*
* Attribute features
* if the phone is off. It is not connected.
* if the phone is not connected there is no signal strength
* the attribute for battery life has valid range [0,100]. 0 is flat, 100 is full.
* the attribute for signal strength has a valid range [0, 5]. 0 is no signal, 5 is best signal.
*
* Please implement the methods provided, as some of the marking is
* making sure that these methods work as specified.
*
* @author An INFO1110 tutor.
* @date September, 2020
*
"""
from dorrigo_contact import dorrigo_contact

class dorrigo_mobile:

def __init__(self, max_contacts):


"""
Every phone manufactured has the following attributes

the phone is off


the phone has battery life 25
the phone is not connected
the phone has signal strength 0
Each of the contacts stored in the array contacts has a None value

the owner first name "Dorrigo"


the owner last name is "Incorporated"
the owner phone number is "180076237867"
the owner chat message should have only one message
"Thank you for choosing Dorrigo products"
"""
#contacts =
self.phone_on = False
self.battery_life = 25
self.connected = False
self.signal_strength = 0
self.contacts =[None] * max_contacts
self.max_contacts = max_contacts
self.occupied = 0
self.owner = dorrigo_contact("Dorrigo", "Incorporated", "180076237867")
self.owner.add_chat_message("Dorrigo", "Thank you for choosing Dorrigo products")

def get_copy_of_owner_contact(self):
""" returns a copy of the owner contact details
return None if the phone is off
"""
if self.phone_on == False:
return None
else:
return self.owner.create_copy()

def add_contact(self, contact):


""" only works if phone is on
will add the contact in the array only if there is space and does not exist
The method will find an element that is None and set it to be the contact.
returns True if successful
"""
if self.phone_on == False:
return False
if self.occupied == self.max_contacts:
return False
if contact == None:
return False
exist = False
i=0
while i < self.max_contacts:
if self.contacts[i]==contact:
exist = True
break
return False
i += 1

if exist == False:
a=0
while a < self.max_contacts:
if self.contacts[a] == None:
self.contacts[a] = contact
break
a += 1
self.occupied += 1
return True
else:
return False

def remove_contact(self, contact):


""" only works if phone is on
find the dorrigo_contact object and set the array element to None
return True on successful remove
"""
if self.phone_on == False:
return False
else:
i=0
while i < self.max_contacts:
if self.contacts[i] == contact and self.contacts[i] != None:
self.contacts[i] = None
self.occupied -= 1
return True
break
i +=1
return False

def get_number_of_contacts(self):
""" only works if phone is on
returns the number of contacts, or -1 if phone is off
"""
if self.phone_on == False:
return -1
else:
return self.occupied

def search_contact(self, name):


""" only works if phone is on
returns a list of all contacts that match first name OR last name
if phone is off, or no results, None is returned
"""
if self.phone_on == False:
return None
else:
contact_list = []
i=0
while i < self.max_contacts:
if self.contacts[i] != None:
if self.contacts[i].fname == name or self.contacts[i].lname == name:
contact_list.append(self.contacts[i])
i += 1
if contact_list == []:
return None
else:
return contact_list
def is_phone_on(self):
""" returns True if phone is on
"""
if self.phone_on == True:
return True
else:
return False

def set_phone_on(self, on):


"""
set the on status based on the boolean input
when phone turns on, it costs 5 battery for startup. network is initially disconnected
when phone turns off it costs 0 battery, network is disconnected
always return True if turning off
return False if do not have enough battery level to turn on
return True otherwise
"""
if on == True:
if self.battery_life < 6:
return False
else:
self.battery_life -= 5
self.connected = False
self.phone_on = True
return True
else:
self.connected = False
self.phone_on = False
return True

def get_battery_life(self):
""" Return the battery life level as an integer. if the phone is off, zero is returned.
"""
if self.phone_on == False:
return 0
else:
return self.battery_life

def change_battery(self, new_battery_level):


""" Change battery of phone.
On success. The phone is off and new battery level adjusted and returns True
If new_battery_level is outside manufacturer specification of [0,100], then
no changes occur and returns False.
"""
if new_battery_level < 0 or new_battery_level > 100:
return False
else:
self.battery_life = new_battery_level
self.phone_on = False
return True

def is_connected_network(self):
""" only works if phone is on.
returns True if the phone is connected to the network
"""
if self.phone_on == False:
return False
else:
return self.connected

def disconnect_network(self):
""" only works if phone is on.
when disconnecting, the signal strength becomes zero
always returns True
"""
if self.phone_on == False:
return False
if self.connected == True:
self.connected = False
self.signal_strength = 0
return True
if self.connected == False:
self.signal_strength = 0
return False

def connect_network(self):
""" only works if phone is on.
Connect to network
if already connected do nothing and return True
if connecting:
1) signal strength is set to 1 if it was 0
2) signal strength will be the previous value if it is not zero
3) it will cost 2 battery life to do so
returns the network connected status
"""
if self.phone_on == False:
return False
if self.connected == True:
return True

self.connected = True
if self.signal_strength == 0:
self.signal_strength = 1
if self.signal_strength != 0:
self.signal_strength = self.signal_strength

if self.use_phone(2) == True:
return self. connected
else:
return False

def get_signal_strength(self):
""" only works if phone is on.
returns a value in range [1,5] if connected to network
otherwise returns 0
"""
if self.phone_on == False:
return 0
else:
if self.connected == True:
if self.signal_strength >= 1 and self.signal_strength <=5:
return self.signal_strength
else:
return 0
else:
return 0

def set_signal_strength(self, new_strength):


""" only works if phone is on.
sets the signal strength and may change the network connection status to on or off
signal of 0 disconnects network
signal [1,5] can connect to network if not already connected
if the signal is set outside the range [0,5], nothing will occur and will return False
returns True on success
"""
if self.phone_on == False:
return False
if new_strength < 0 or new_strength > 5:
return False
if self.connected == False:
if new_strength != 0:
self.connected = True
self.signal_strength = new_strength
return True

if self.connected == True:
self.signal_strength = new_strength
if self.signal_strength == 0:
self.connected = False
return True

def charge_phone(self):
""" each charge increases battery level by 10
the phone has overcharge protection and cannot exceed 100
returns True if the phone was charged by 10, otherwise False
"""
if self.battery_life > 90:
self.battery_life = 100
return False
else:
self.battery_life += 10
return True

def use_phone(self, k):


""" Use the phone which costs k units of battery life.
if the activity exceeds the battery life, the battery automatically
becomes zero and the phone turns off.
returns True on successful use (not partial)
"""
self.battery_life = self.battery_life - k
if self.battery_life > 0:
return True
else:
self.battery_life = 0
self.phone_on = False
return False

You might also like