|
| 1 | +\chapter{Exercise 00} |
| 2 | +\extitle{The Book} |
| 3 | +\turnindir{ex00} |
| 4 | +\exnumber{00} |
| 5 | +\exfiles{book.py, recipe.py, test.py} |
| 6 | +\exforbidden{None} |
| 7 | +\makeheaderfilesforbidden |
| 8 | + |
| 9 | +% ================================== % |
| 10 | +\section*{Objective} |
| 11 | +% ---------------------------------- % |
| 12 | +The goal of this exercise is to get you familiar with the notions of |
| 13 | +classes and the manipulation of the objects related to these classes. |
| 14 | + |
| 15 | +% ================================== % |
| 16 | +\section*{Instructions} |
| 17 | +% ---------------------------------- % |
| 18 | +You will have to make a class \texttt{Book} and a class \texttt{Recipe}. |
| 19 | +The classes \texttt{Book} and \texttt{Recipe} will be written in |
| 20 | +\texttt{book.py} and \texttt{recipe.py} respectively.\\ |
| 21 | +\newline |
| 22 | +Let's describe the \texttt{Recipe} class. It has some attributes: |
| 23 | +\begin{itemize} |
| 24 | + \item \texttt{name} (str): name of the recipe, |
| 25 | + \item \texttt{cooking\_lvl} (int): range from $1$ to $5$, |
| 26 | + \item \texttt{cooking\_time} (int): in minutes (no negative numbers), |
| 27 | + \item \texttt{ingredients} (list): list of all ingredients each represented by a string, |
| 28 | + \item \texttt{description} (str): description of the recipe, |
| 29 | + \item \texttt{recipe\_type} (str): can be "starter", "lunch" or "dessert". |
| 30 | +\end{itemize} |
| 31 | +You have to \textbf{initialize} the object \texttt{Recipe} and check all of its values. Only the description can be empty. |
| 32 | +In case of input errors, you should print what they are and exit properly.\\ |
| 33 | +\newline |
| 34 | +You will have to implement the built-in method \texttt{\_\_str\_\_}. |
| 35 | +It's the method called when the following code is executed:\\ |
| 36 | +\newline |
| 37 | +\begin{minted}[bgcolor=darcula-back,formatcom=\color{lightgrey},fontsize=\scriptsize]{python} |
| 38 | +tourte = Recipe(...) |
| 39 | +to_print = str(tourte) |
| 40 | +print(to_print) |
| 41 | +\end{minted} |
| 42 | +\newline |
| 43 | +\newline |
| 44 | +It is implemented this way:\\ |
| 45 | +\newline |
| 46 | +\begin{minted}[bgcolor=darcula-back,formatcom=\color{lightgrey},fontsize=\scriptsize]{python} |
| 47 | +def __str__(self): |
| 48 | + """Returns the string to print with the recipe's info""" |
| 49 | + txt = "" |
| 50 | + """Your code here""" |
| 51 | + return txt |
| 52 | +\end{minted} |
| 53 | +\newline |
| 54 | +The \texttt{Book} class also has some attributes: |
| 55 | +\begin{itemize} |
| 56 | + \item \texttt{name} (str): name of the book, |
| 57 | + \item \texttt{last\_update} \href{https://docs.python.org/3/library/datetime.html}{(datetime)}: the date of the last update, |
| 58 | + \item \texttt{creation\_date} \href{https://docs.python.org/3/library/datetime.html}{(datetime)}: the creation date of the book, |
| 59 | + \item \texttt{recipes\_list} (dict): a dictionnary with 3 keys: "starter", "lunch", "dessert". |
| 60 | +\end{itemize} |
| 61 | +You will have to implement some methods in the \texttt{Book} class:\\ |
| 62 | +\newline |
| 63 | +\begin{minted}[bgcolor=darcula-back ,formatcom=\color{lightgrey},fontsize=\scriptsize]{python} |
| 64 | +def get_recipe_by_name(self, name): |
| 65 | + """Prints a recipe with the name \texttt{name} and returns the instance""" |
| 66 | + #... Your code here ... |
| 67 | + |
| 68 | +def get_recipes_by_types(self, recipe_type): |
| 69 | + """Gets all recipes names for a given recipe_type """ |
| 70 | + #... Your code here ... |
| 71 | + |
| 72 | +def add_recipe(self, recipe): |
| 73 | + """Adds a recipe to the book and updates last_update""" |
| 74 | + #... Your code here ... |
| 75 | +\end{minted} |
| 76 | +\newline |
| 77 | +You have to handle the error if the argument passed in \texttt{add\_recipe} is not a \texttt{Recipe}.\\ |
| 78 | +\newline |
| 79 | +Finally, you will provide a \texttt{test.py} file to test your classes and prove that they are working properly. |
| 80 | +You can import all the classes into your \texttt{test.py} file by adding these lines at the top of the \texttt{test.py} file:\\ |
| 81 | +\newline |
| 82 | +\begin{minted}[bgcolor=darcula-back ,formatcom=\color{lightgrey},fontsize=\scriptsize]{python} |
| 83 | +from book import Book |
| 84 | +from recipe import Recipe |
| 85 | + |
| 86 | +# ... Your tests ... |
| 87 | +\end{minted} |
0 commit comments