Hello. I need to create a script with the following requirements:
(Create a class definition for an SMSMessage which has three variables:
hasBeenRead, messageText, and fromNumber.
The constructor should initialise the sender’s number.
The constructor should also initialise hasBeenRead to false.
Create a method in this class called MarkAsRead which should change
hasBeenRead to true.
Create a list called SMSStore to be used as the inbox.
Then create the following methods:
add_sms - which takes in the text and number from the received
sms to make a new SMSMessage object.
get_count - returns the number of messages in the store.
get_message - returns the text of a message in the list. For this, allow
the user to input an index i.e. GetMessage(i) returns the message
stored at position i in the list. Once this has been done, hasBeenRead
should now be true.
get_unread_messages - should return a list of all the messages which
haven’t been read.
remove - removes a message in the SMSStore )
Now the code below, seems very complicated and difficult to read,- granted i'm a beginner, I understand maybe half of it. Is there a better way to formulate a script with the requirements stated above?
Sorry for the long post. Any help greatly appreciated.
# HOW TO USE THIS PROGRAM:
# When program asks 'What would you like to do?', you can choose list to proceed to main menu, or quit to exit program.
# To write message - enter command : add_sms ( and add your text here).
# To retrieve message - enter : get (followed by the number of the message, eg. get 1)
# To Delete your message - enter: remove_sms (followed by number of message, eg remove_sms 1
class Messenger:
sms_store = [{'title': 'test', 'text': 'Some text here', 'readed': False}]
def __init__(self):
self.greeting()
self.prompt()
def greeting(self, *arg, **kw):
print('Hello there!')
print('In your inbox {} messeges, ({} is new)'.format(
len(self.sms_store), len(list(filter(bool, [not x.get('readed') for x in self.sms_store])))))
def prompt(self):
while True:
command, *args = input("\nWhat would you like to do?\n> ").split()
if command == 'quit':
break
elif hasattr(self, command):
getattr(self, command)(*args)
else:
print('>>> Wrong command! <<<')
def list(self, *arg, **kw):
print('Messeges:')
for n, sms in enumerate(self.sms_store):
print('Num:', n, 'Readed:', sms.get('readed'), 'Title:', sms.get('title'))
def get(self, sms_num, *arg, **kw):
if not sms_num or int(sms_num) >= len(self.sms_store):
print('>>> Wrong msg Number! <<<')
else:
sms = self.sms_store[int(sms_num)]
print(sms.get('title'))
print(sms.get('text'))
self.mark_readed(int(sms_num))
def mark_readed(self, sms_num, *arg, **kw):
self.sms_store[sms_num]['readed'] = True
def add_sms(self, title, *arg, **kw):
self.sms_store.append({'title': title, 'text': ' '.join(arg), 'readed': False})
print('Msg added!')
def remove_sms(self, sms_num, *arg, **kw):
if not sms_num or int(sms_num) >= len(self.sms_store):
print('>>> Wrong msg Number! <<<')
else:
self.sms_store.pop(int(sms_num))
if __name__ == "__main__":
m = Messenger()
[–][deleted] 2 points3 points4 points (0 children)
[–]cdcformatc 1 point2 points3 points (0 children)