You are required to design and implement two related programs:
Starter files for both of these programs are provided along with this assignment brief, to help you get started and to facilitate an appropriate program structure. Please use the starter files.
As emphasised by the case study of Module 5, it is important to take the time to properly design a solution before starting to write code. Hence, this assignment requires you to write and submit pseudocode of your program design for admin.py, but not jokebot.py (pseudocode is not very well suited to illustrating the design of an event-driven GUI program). Furthermore, while your tutors are happy to provide help and feedback on your assignment work throughout the semester, they will expect you to be able to show your pseudocode and explain the design of your code.
You will gain a lot more benefit from pseudocode if you actually attempt it before trying to code your program even if you just start with a rough draft to establish the overall program structure, and then revise and refine it as you work on the code. This back and forth cycle of designing and coding is completely normal and expected, particularly when you are new to programming. The requirements detailed on the following pages should give you a good idea of the structure of the program, allowing you to make a start on designing your solution in pseudocode.
See Reading 3.3 for further information and tips regarding writing good pseudocode.
Write a separate section of pseudocode for each function you define in your program so that the pseudocode for the main part of your program is not cluttered with function definitions. Ensure that the pseudocode for each of your functions clearly describes the parameters that the function receives and what the function returns back to the program.
It may help to think of the pseudocode of your program as the content of a book, and the pseudocode of functions as its appendices: It should be possible to read and understand a book without necessarily reading the appendices, however they are there for further reference if needed.
Three functions are required in this assignment (detailed later in the assignment brief). The following pages describe the requirements of both programs in detail.
admin.py is a program with a Command-Line Interface (CLI) like that of the programs we have created throughout the first half of the unit. The entirety of this program can be implemented in under 175 lines of code If your program exceeds this, ask your tutor for advice. Everything you need to know in order to develop this program is covered in the first 7 modules of the unit. This program should be developed before jokebot.py.
This program allows the user to manage the details of a collection of jokes which are stored in a text file named data.txt. Use the json module to write data to the text file in JSON format and to read the JSON data from the file back into Python. See Reading 7.1 for details regarding this. This is an example of the files content in JSON format (which is almost identical to Python):
"setup": "What do you call a group of 8 hobbits?",
"punchline": "A hobbyte.",
"setup": "I once drank some food colouring...",
"punchline": "It made me feel like I had dyed a little inside.",
This example file contains two items in a list. Each of those items is a dictionary consisting of 4 items which have keys of setup, punchline, numOfRatings and sumOfRatings. If this file was to be read into a Python variable named data, then data would refer to the dictionary containing the first joke (about hobbits), and data['numOfRatings'] would refer to 0 (integer).
In the following information, numbered points describe a requirement of the program, and bullet points (in italics) are additional details, notes and hints regarding the requirement. Ask your tutor if you do not understand the requirements or would like further information. The requirements are:
1. The first thing the program should do is try to open data.txt in read mode, then load the data from the file into a variable named data and then close the file.
2. The program should then print a welcome message and enter an endless loop which starts by printing a list of options: Choose [a]dd, [l]ist, [s]earch, [v]iew, [d]elete or [q]uit. and then prompts the user to enter their choice.
Once a choice has been entered, use an if/elif to respond appropriately (detailed below).
3. If the user enters a (add), prompt them to enter the setup and punchline of a joke, and use them to create a new dictionary with the structure shown on the previous page. Append the dictionary to the data list, then write the list to the text file in JSON format to save the joke.
4. If the user enters l (list), display a list of the jokes (just the setup) preceded by their index number in the data list, or a No jokes saved error message if there is nothing in the list.
5. If the user enters s (search), prompt for a search term and then list the jokes whose setup or punchline contains the search term. Remember to include the index number next to each result.
6. If the user enters v (view), prompt them for an index number and then print the corresponding jokes setup and punchline, including details of the jokes rating.
7. If the user enters d (delete), prompt them for an index number and then remove the corresponding item from the data list, then display a Joke deleted message.
8. If the user enters q (quit), print Goodbye! and break out of the loop to end the program.
9. If the user enters anything else, print an Invalid choice message (the user will then be re- prompted for a choice on the next iteration of the loop).
This concludes the core requirements of admin.py. The following pages detail the functions mentioned above, the additional requirements for CSP5110 students, optional additions and enhancements, and an annotated screenshot of the program.
Since jokes are rated in the GUI program (jokebot.py) the ratings aspect of the assignment will make more sense once you have read the rest of the assignment brief.
Remember that you are required to submit pseudocode for your design of admin.py.
The requirements above mentioned three functions - inputInt(), inputSomething(), and saveChanges(). You must define and use these functions as part of admin.py.
1. The inputInt() function takes one parameter named prompt. The function should repeatedly re-prompt the user (using the prompt parameter) for input until they enter an integer. It should then return the value as an integer.
2. The inputSomething() function takes one parameter named prompt. The function repeatedly re-prompt the user (using the prompt parameter) for input until they enter a value which consists of at least one character once all whitespace has been stripped from the start and end of it. It should then return the value as a string.
3. The saveChanges() function takes one parameter named dataList (this will be the data list). The function should open data.txt in write mode, then write the dataList parameter to the file in JSON format and close the file. This function does not return anything.
You may write/use additional functions if you feel they improve your program.
jokebot.py is a program with a Graphical User Interface (GUI), as covered in Module 9. Everything you need to know in order to develop this program is covered in the first 9 modules of the unit. This program should be developed after admin.py. The entirety of this program can be implemented in under 150 lines of code If your program exceeds this, ask your tutor for advice.
This program uses the data from the data.txt file to show one joke at a time, allowing the user to rate the joke before showing the next one. Once all of the jokes have been shown, the program ends. To ensure compatibility and consistency, you are required to use the tkinter module for the GUI of jokebot.py. You will also need to use the tkinter.messagebox module and the json module.
To help illustrate the jokebox.py program, here are some annotated screenshots of it being used: (Your program may look slightly different, depending on which version of which operating system you use) see image.
See the content of Module 9 for examples of creating a class for your programs GUI. A starter file has been provided to assist you. For conciseness, I have referred to attribute names without the self. prefix in the following details.
Implement all of the following requirements, and ask your tutor if you do not understand any of the requirements or would like further information. The requirements of the program are:
The constructor (the __init__ method) of your GUI class must implement the following:
1. Create the main window of the program and give it a title of Joke Bot.
2. Try to open the data.txt file in read mode and load the JSON data from the file into an attribute named self.data, and then close the file. Also create an attribute named self.currentJoke and set it to 0.
3. The constructor should then use Label, Entry, Button and Frame widgets from the tkinter module to implement the GUI depicted on the previous page.
4. Finally, the constructor should end by calling the showJoke method to place the first joke into the GUI, and then call tkinter.mainloop() to start the main loop.
That is all that the constructor requires. The following pages detail the methods mentioned above, the additional requirements for CSP5110 students and optional additions and enhancements.
You are not required to submit pseudocode for your design of jokebot.py, as pseudocode is not particularly well suited to illustrating the design of an event-driven GUI program.
Your GUI class requires two methods to implement the functionality of the program - showJoke and rateJoke. Neither of them require any parameters other than self, which is passed to all methods in a class. As part of the GUI class of jokebot.py, you must write these methods.
1. The showJoke method is responsible for displaying a joke in the GUI. The method must first select a joke from self.data using the self.currentJoke attribute, and then configure the labels for the setup, punchline and rating information to display the setup, punchline and rating information of the selected joke.
2. The rateJoke method is responsible for validating and recording the rating that a user gives a joke. The method must first ensure that an integer between 1 and 5 has been entered into the rating Entry widget If not, show an error messagebox and return to end the method.
If a valid rating has been entered, add one to the jokes numOfRatings value, and add the rating to the jokes sumOfRatings value in the self.data attribute. Then, open the data.txt file in write mode, write self.data to it in JSON format and close the file.
Finally, check the current joke is the last one (using self.currentJoke and self.data) and show the appropriate messagebox as per the screenshots above. If it is the final joke, end the program. Otherwise, add 1 to the self.currentJoke attribute, delete the content of the rating entry widget, and then call the showJoke method to display the next joke.
These two methods are all that are required to implement the functionality of the program, but you may write/use additional functions if you feel they improve your program.