In: Computer Science
Challenge: Documents
Description: Create a class in Python 3 named Document that has specified attributes and methods for holding the information for a document and write a program to test the class.
Purpose: The purpose of this challenge is to provide experience creating a class and working with OO concepts in Python 3.
Requirements:
Write a class in Python 3 named Document that has the following attributes and methods and is saved in the file Document.py.
Attributes
__title is a hidden attribute used to hold the document's title (which is a string).
__body is a hidden attribute used to hold the text of the document (which is a string).
__author is a hidden attribute used to indicate the document's author (which is a string).
Methods
__init__ is to define the three attributes above and assign their default values.
get_title should return the value of the __title field.
get_body should return the value of the __body field.
get_author should return the value of the __author field.
Document Generator Program
Once you have created the Document class, create another Python file called documentGenerator.py. This program is to use Document.py as a module.
In documentGenerator.py, prompt the user to enter the title, body, and author for a document. Create a new Documentobject instance to store this data. Then, ask the user if they would like to repeat the process. They should be able to create as many Document object instances as they would like.
After the user is done creating documents, the program is to use the object’s accessor methods (get_title, get_body, and get_author) to retrieve the title, body, and author of each document. This information is to be displayed in a nicely formatted way.
THANK YOU! (:
The program is given below. The comments are provided for the better understanding of the logic.
Document.py file
#Document.py class
class Document:
#The Method to initialise the hidden variables. This method takes 3 arguments as shown below.
def __init__(self, title, body, author):
#assign the arguments to the hidden variables.
#The variables starting with __ are hidden and can be accessed only within the class. It cannot be accessed outside the class.
self.__title = title
self.__body = body
self.__author = author
#The below are 3 getter methods to return the 3 hidden variables to the calling function.
#Without these methods, it is not possible to access the 3 hidden variables from outside this class.
def get_title(self):
return self.__title
def get_body(self):
return self.__body
def get_author(self):
return self.__author
The program is given below. The comments are provided for the better understanding of the logic.
documentGenerator.py file
#documentGenerator.py class
#The below import statement will import the details in the file "Document.py"
#Please make sure to put both the python in the same directory
import Document
#Declare a list to hold all the Document instances.
#The user can create any number of instances of the Document and it need to be stored.
documentsList = []
#This variable will handle continuing the loop, if the user wants to enter details of another document.
continueEnteringBookDetails = True
#The loop will continue as long as the variable continueEnteringBookDetails is True
#Inside the loop this variable is set to True or False depending on the user input.
while(continueEnteringBookDetails):
#Read Title, Body and Author from the user.
bookTitle = input("Enter Document Title: ")
bookBody = input("Enter Document Body: ")
bookAuthor = input("Enter Document Author: ")
#Create a Document instance.
#All the 3 variables, we got from the user are passed to the Document class.
#In the Document class, this will invoke the __init__ method.
doc = Document.Document(bookTitle, bookBody, bookAuthor)
#After the Document instance is created, add that to the list documentsList.
documentsList.append(doc)
#Check with the user if he//she wants to continue entering details of another document.
#Set the variable continueEnteringBookDetails to True or False depending on user's input.
#That will either exit the loop or continue with the loop.
userInput = input("\nDo you want to enter details of another document (y/n)? ")
userInput = userInput.strip().lower()
if(userInput == "y"):
continueEnteringBookDetails = True
else:
continueEnteringBookDetails = False
#Print the Document Details.
print("\n***The Document details follows***")
#Print the Header with fixed width 5, 30, 30 characters respectively. The less than symbol will align it to the left.
print("{0:<5}".format("S#"), end="")
print("{0:<30}".format("Title"), end="")
print("{0:<30}".format("Author"))
i = 1
#Loop through the documentsList and print details one by one.
for doc in documentsList:
#Use the same width and print the contents.
print("{0:<5}".format(i), end="")
print("{0:<30}".format(doc.get_title()), end="")
print("{0:<30}".format(doc.get_author()))
#The document body is printed in a separate line as it can span multiple lines.
print("Document Body:")
print(doc.get_body())
#print a line after each document details.
print("")
i = i + 1
Please store both the files in the same directory and then run the python documentGenerator.py. Please indent it correctly as per the screenshot
The screenshots of the code and output are provided below.