I have to write a program which reads from keyboard some info then creates a HTML file and a JSON file through Factory method design pattern. I have a uml diagram for help click here for image Ignore the class TextFile
This is how I started
from abc import ABCMeta, abstractmethod
class File(metaclass=ABCMeta):
def __init__(self, title, author, paragraphs):
self.title = title
self.author = author
self.paragraphs = paragraphs
@abstractmethod
def read_file_from_stdin(self):
pass
class HTMLFile(File):
def read_file_from_stdin(self):
self.title = input("Enter the title: ")
self.author = input("Enter the author")
number_paragraphs = input("Enter the no of paragraphs:")
self.paragraphs = input("Enter the paragraphs:")
def print_html(self):
print("<html>")
print("<title>" + self.title + "</title")
print("</html>")
class JSONFile(File):
def read_file_from_stdin(self):
self.title = input("Enter the title: ")
self.author = input("Enter the author")
number_paragraphs = input("Enter the no of paragraphs:")
self.paragraphs = input("Enter the paragraphs:")
def print_json(self):
pass
class FileFactory:
def factory(file_type):
if file_type == "HTMLFile":
return HTMLFile("Some title", "Some author", "Some paragraphs")
if file_type == "JSONFile":
return JSONFile("Some title", "Some author", "Some paragraphs")
print("Invalid type")
return -1
if __name__ == '__main__':
choice = input("Enter the type of file:")
file = FileFactory.factory(choice)
file.read_file_from_stdin()
file.print_html()
My question is about printer methods from each class. I guess I should print the file after its creation right? But how do I create a html file with my output?
Also, in the uml diagram, the method factory from FileFactory returns a File but the File class is abstract and I get an error. How to return just HTMLFile/JSONFile without parameters? If I write just return HTMLFile() for example, I get error because it needs the arguments title, author and paragraphs.
Aucun commentaire:
Enregistrer un commentaire