In: Computer Science
Creating an e-mail Message Class
e-mail. How we often communicate today…from Christmas lists, to pick up from the store lists, to-do lists, to one-liner messages…all examples of communication using an e-mail message.
Lab Assignment Objectives
Understand the Application
Your task this lab is to design, implement and test your own class called Message that models a basic email application.
Specifically you will write a program that will define a simple class that will model an e-mail message. You will then write a test driver program to validate your class email app.
You will define the Message class. The Message will include a sender, recipient and body. The sender is the originator of the email message. The recipient is the receiver of the message. The body contains the text of the message.
Provide the following methods for the Message class:
A constructor with parameters self, sender and recipient. Set values for sender and recipient. Set body as an empty string.
A method append with parameters self and line. line contains the line of text to add to the body of the email message. End each email message line with a newline character.
A method to_string that returns a string representation of the entire message. The entire message includes the sender, the recipient and the body of the message. Example: “From: Student One\nTo: Student Two\nLunch today?\n”
A helper method str_ok() that validates str parameters.
The Program Specification
The assignment is to first create a class called Message. Message will consist of three instance attribute strings as its basic data. It will also contain a few instance methods to support that data.
Once defined, you will use the Message class to instantiate a Message object(s) that can be used in your test driver mainprogram.
***
Class Message
Instance Members:
All legal strings should be of length <= 50 consisting of printable characters.
Instance Methods:
Constructor
Message() -- a parameterized constructor that initializes all class members.
def __init__(self, sender, recipient):-- a constructor that initializes all members according to the passed parameters. The constructor has to to be sure each parameter string satisfies the class requirement for a member string. It does this, as shown in the lab forum discussion thread, by calling the mutators and taking possible action if a return value is false. If any passed parameter does not pass the test, a default string should be stored in that member.
Mutators
set() methods for the sender and recipient instance members. using the convention as follows:
set_sender( ..), set_recipient( ... ).
append() method for the body instance member.
Mutators make use of the helper method str_ok() to validate parameter data.
When the set methods detect an invalid string, the mutator returns False and a new default String is stored in that member.
When the append method detects an invalid string, no action should be taken.
Helper Methods
string to_string() - a method that returns a string which contains all the information (three strings) of the Message object. This string can be in any format as long as it is understandable and clearly formatted.
def str_ok(self, the_str): -- a helper method that the mutators can use to determine whether a string is legal. This method returns True if the string's length is <= MAX_LEN (inclusive) and the string consists of printable characters. It returns False, otherwise.
Testing Specification
Input Error Checking: For simplicity a python test file is provided to begin testing your class. The filename is demo_message.py provided below.
Test Run Requirements: Extend the provided demo_message.py as your test run validator to both display your email message as well as to include demonstration that the set methods correctly reject invalid parameter data.
Here are some other tips and requirements:
1. Name the class Message.
2. Use a named constant to set the default string to the empty string.
3. Update the sender name to your name (i.e. name should be changed to your first name). Update (i.e. customize) the wish list items to items of your choice. If you wish, update the recipient.
4. Import your Message class module into your test demo file.
5. Include the commented out copy of your test run in your demo file.
Here is a sample run:
…
From: Bob
To: Ellen
For Christmas, I would like:
Video games
World peace
Short Summary:
**************Please upvote the answer and appreciate our time.************
Source Code:
Message.py File:
class Message:
# Named Constants
MAX_LENGTH = 50
EMPTY_STRING = ""
# constructor that initializes all members according to the passed
parameters.
def __init__(self, sender, recipient):
# set default values
self.__sender = self.EMPTY_STRING
self.__recipient = self.EMPTY_STRING
self.__body = []
# make use of mutators method
self.set_sender(sender)
self.set_recipient(recipient)
def set_sender(self, sender):
if self.str_ok(sender):
self.__sender = sender
else:
self.__sender = self.EMPTY_STRING
def set_recipient(self, recipient):
if self.str_ok(recipient):
self.__recipient = recipient
else:
self.__recipient = self.EMPTY_STRING
def append(self, line):
if self.str_ok(line):
self.__body.append(line)
# Mutators make use of the helper method str_ok() to validate
parameter data.
def str_ok(self, the_str):
return isinstance(the_str, str) and len(the_str) <=
self.MAX_LENGTH
def to_string(self):
string = "From: " + self.__sender + "\n"
string += "To: " + self.__recipient + "\n"
for line in self.__body:
string += line + "\n"
return string
demo_message.py File:
from Message import Message
#Create an object
message = Message("Sara", "Ellen")
message.append("For Christmas, I would like:")
message.append("Video games")
message.append("World peace")
print(message.to_string())
Refer the following screenshots for code indentation:
Sample Run:
**************************************************************************************
Feel free to rate the answer and comment your questions, if you have any.
Please upvote the answer and appreciate our time.
Happy Studying!!!
**************************************************************************************