Implement the functionality of the program

Assignment Help Programming Languages
Reference no: EM132314754

Assignment: Individual programming project (Fruit Test)

Background Information

This assignment tests your understanding of and ability to apply the programming concepts we have covered throughout the unit. The concepts covered in the second half of the unit build upon the fundamentals covered in the first half of the unit.

Assignment Overview

You are required to design and implement two related programs:

• "admin.py", a CLI program that allows the user to add, list, search, view and delete nutritional information about fruit, and stores the data in a text file. Develop this program before "fruit_test.py".
• "fruit_test.py", a GUI program that uses the data in the text file to quiz the user about the nutritional information of fruit. Develop this program after "admin.py".

The following pages describe the requirements of both programs in detail.

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.

Pseudocode

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 "fruit_test.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.

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.

The functions required in the "admin.py" program are detailed later in the assignment brief.

The following pages describe the requirements of both programs in detail.

Overview of "admin.py"
"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 215 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 "fruit_test.py".

This program allows the user to manage data regarding the nutritional information of fruit. The user can add a new fruit to the data by specifying its name and the following nutritional information per 100 grams: Calories, Dietary Fibre (in grams), Sugar (in grams) and Vitamin C (in milligrams). The program also allows the user to list, search, view and delete fruit in the data.

The data is 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. To illustrate the structure of the data, below is an example of the file's content in JSON format (which is almost identical to Python):

This example file contains the details of fruit as items in a list. Each of those items is a dictionary consisting of 5 items which have keys of "name", "calories", "fibre", "sugar" and "vitamin_c" - a string and four floats.

If this file was to be read into a Python variable named data, then "data[0]" would refer to the dictionary containing the first fruit (Apple), and "data[0]['fibre']" would refer to the float of 2.4.

Understanding the structure of this data and how you can use it is very important in many aspects of this assignment - in particular, you will need to understand how to loop through the items of a list and how to refer to items in a dictionary. Revise Module 3 and Module 7 if you are unsure about how to interact with lists and dictionaries, and see the Blackboard discussion board for further help.

Requirements of "admin.py"

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 a file named "data.txt" in read mode, then load the data from the file into a variable named data and then close the file.
» The data in the file should be in JSON format, so you will need to use the "load()" function from the
"json" module to read the data into your program. See the previous page for details of the data.
» If any exceptions occur (e.g. due to the file not existing, or it not containing valid JSON data), then simply set the data variable to be an empty list. This will occur the first time the program is run, since the data file will not exist yet.
» This is the first and only time that the program should need to read anything from the file. After this point, the program uses the data variable, which is written to the file whenever a change is made.

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 in the following requirements).
» This requirement has been completed for you in the starter file.

3. If the user enters "a" (add), prompt them to enter all 5 details of a fruit, beginning with the name. Place the details into a new dictionary with the structure shown on the previous page, and append the dictionary to the data list, then write the list to the text file in JSON format.
» The program should mention that the nutritional information should be for 100 grams of the fruit, and specify the expected unit of measurement as per the details in the overview of this program.
» Use your "inputSomething()" function (detailed below) when prompting for the name of the fruit, to ensure that the user is re-prompted until they enter something other than whitespace.
» Use your "inputFloat()" function (detailed below) when prompting for the nutritional details of the fruit, to ensure that the user is re-prompted until they enter an appropriate value.
» Once the dictionary of fruit information has been appended to the data list, call your "saveData()" function (detailed below) to write the data to the text file in JSON format.

4. If the user enters "l" (list), display the names of all fruit in the data list preceded by their index number plus 1 (i.e. the first fruit listed should be number 1 instead of 0), or a "No fruit saved" error message if there is nothing in the list.
» Use a "for" loop to iterate through the items in the data list. Remember: each item is a dictionary.
» You can use the "enumerate()" function to make sure you have a variable containing the index number of each fruit as you loop through them (see Lecture 3).

5. If the user enters "s" (search), prompt for a search term and then list the fruit whose name contains the search term. Remember to include the index number plus 1 next to each result.
» If the data list is empty, show a "No Fruit saved" message instead of prompting for a search term.
» This is a good opportunity to reuse and tweak the code used to list all fruit - the loop body needs an "if" to only print fruit that contain the search term (use the "in" operator - see Lecture 3).
» Convert the search term and fruit name to lowercase to find matches regardless of case.
» Use your "inputSomething()" function (detailed below) to prompt the user for a search term, to ensure that the user is re-prompted until they enter something.

6. If the user enters "v" (view), prompt them for an index number and then print the corresponding fruit's name and all of its nutritional information.
» If the data list is empty, show a "No Fruit saved" message instead of prompting for an index number.
» The index numbers entered by users will be the ones shown to them when listing and searching, so remember to subtract 1 from the number to refer to the actual index number in the data list.
» Use your "inputInt()" function (detailed below) to prompt the user for the index number of the
fruit, to ensure that the user is re-prompted until they enter an integer.
» Display an "Invalid index number" message if the user enters an index number that doesn't exist in the data list.
» In your output, be sure to mention that the values are for 100 grams of the fruit, and include the unit
of measurement next to the nutritional information as per the details in the overview of this program.

7. If the user enters "d" (delete), prompt them for an index number and then remove the corresponding fruit from the data list, then display a "Fruit deleted" message.
» If the data list is empty, show a "No Fruit saved" message instead of prompting for an index number.
» The index numbers entered by users will be the ones shown to them when listing and searching, so remember to subtract 1 from the number to refer to the actual index number in the data list.
» Use your "inputInt()" function (detailed below) to prompt the user for the index number of the
fruit, to ensure that the user is re-prompted until they enter an integer.
» Display an "Invalid index number" message if the user enters an index number that doesn't exist in the data list.
» Once the fruit has been deleted from the data list, call your "saveData()" function (detailed
below) to write the data to the text file in JSON format..

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 and optional additions and enhancements that can be added to the program.
Remember that you are required to submit pseudocode for your design of "admin.py".

Functions in "admin.py"
The requirements above mentioned four functions - "inputInt()", "inputFloat()", "inputSomething()", and "saveData()". As part of "admin.py", you must define and use these functions.

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 of 1 or more (i.e. minimum of 1). It should then return the value as an integer.
» See Workshop 4 for a task involving the creation of this function.

2. The "inputFloat()" function takes one parameter named prompt. The function should repeatedly re-prompt the user (using the prompt parameter) for input until they enter a float of 0 or more (i.e. minimum of 0). It should then return the value as a float.
• Remember that all integers can be converted to floats, e.g. if the user enters 52, it will become 52.0.
• See Workshop 4 for a task involving the creation of this function.

3. The "inputSomething()" function takes one parameter named prompt. The function should repeatedly re-prompt the user (using the prompt parameter) for input until they enter a value which consists of at least one non-whitespace character (i.e. the input cannot be nothing or consist entirely of spaces, tabs, etc.). It should then return the value as a string.
» Use the "strip()" method on a string to remove whitespace from the start and end. If a string consists entirely of whitespace, it will have nothing left once you strip the whitespace away.
» Note that exception handling is not needed in this function.

4. The "saveData()" 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.
» This is the only part of the program that should be writing to the file, and it always simply overwrites the entire content of the file with the entirety of the current data.
» See Reading 7.1 for an example of using the "json" module. You can specify an additional indent parameter in the "dump()" function to format the JSON data nicely in the text file.

The definitions of these functions should be at the start of the program (as they are in the starter file provided), and it should be called where needed in the program. Revise Module 4 if you are uncertain about defining and using functions, and be sure to implement them so that they receive and return values exactly as described above. It is important to adhere to function specifications.

In particular, remember that the "prompt" parameter of the first three functions is for the text that you want to show as a prompt. Here is an example of the function being called and its output (attached):

You are welcome to write additional functions if you feel they improve your program.

Optional Additions and Enhancements for "admin.py"
Below are some suggestions for minor additions and enhancements that you can make to the program to further test and demonstrate your programming ability. They are not required and you can earn full marks in the assignment without implementing them.

» When adding fruit, check if the specified name is the same as any of the fruit already in the
data list. If so, show a "Fruit already exists" message and return them to the main menu.

» When searching for a fruit, show a "No results found" message if the search term is not found in the name of any of the fruit in the list.

» When viewing details of a fruit, show the amount of kilojoules (rounded up to the nearest whole number) in parentheses next to the number of calories. 1 calorie is 4.184 kilojoules.

» Add a new menu option of "[o]verview" which calculates and shows:
o The number of fruit in the data list.
o The name and calories of the fruit with the least calories in the data list.
o The average amount of sugar of the fruit in the data list.
o The name and Vitamin C of the fruit with the most Vitamin C in the data list.

» Allow users to use the search, view and delete options more efficiently by allowing input of "s <search term>", "v <index number>" and "d <index number>". For example, instead of needing to type "s" and then "apple", the user could type "s apple", and instead of needing to type "v" then "2", the user could type "v 2".
o This feature takes a reasonable amount of extra thought and effort to implement efficiently.

Requirements of "fruit_test.py"

"fruit_test.py" is a program with a Graphical User Interface (GUI), as covered in Module 9. It should be coded in an Object Oriented style, as covered in Module 8. 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 125 lines of code - If your program exceeds this, ask your tutor for advice. To ensure compatibility and consistency, you must use the "tkinter" module to create the GUI. You will also need to use the "tkinter.messagebox", "random" and "json" modules.

This program uses the data from the "data.txt" file. Similar to the admin program, this program should load the data from the file once only when the program begins. The program implements a simple quiz by randomly selecting two different fruit and one nutritional component (calories, fibre, sugar or Vitamin C) and using them to present the user with a "True/False" question to answer.

The names of the two selected fruit and the nutritional component are put into the form of a True/False question.

The user can click the True or False button to answer.

When the user clicks one of the buttons, the program should determine whether their answer is correct and show an appropriate messagebox:

A new question is then generated and displayed, and the program continues like this until the user closes it - selecting two random fruit and a random nutritional component each time.

The following pages detail how to implement the program.

Constructor of the GUI Class of "fruit_test.py"

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 "Fruit Test".
» You may also wish to set other settings such as the minimum size of the window.

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.
• If any exceptions occur (due to the file not existing, or it not containing valid JSON data), show an
error messagebox with a "Missing/Invalid file" message and use the "destroy()" method on the main window to end the program. Include a "return" statement in the exception handler after destroying the main window to halt the constructor so that the program ends cleanly.

3. Check if self.data contains fewer than two items, and if so show an error messagebox with a "Not enough fruit" message and destroy the main window.
» See the previous point for tips regarding how to cleanly end the program.

4. Create an attribute named "self.components" and set it to a list containing strings of "calories", "fibre", "sugar" and "vitamin_c".
» This list is used in the "self.showQuestion" method to randomly select a nutritional component.
» You must spell the words exactly the same as the keys of the dictionaries in the self.data list.

5. The constructor should then use Label, Button and Frame widgets from the "tkinter" module to implement the GUI depicted on the previous page.
» The GUI of this program is very simple, only requiring a few widgets. It is likely to take some trial and error before you get it looking exactly as desired. You are welcome change the layout of the GUI.
» You do not need to set any text for the label that will contain the True/False question at this point. Just create and pack an empty label: the text will be set by the "self.showQuestion" method.
» See Reading 9.1 for information regarding various settings that can be applied to widgets to make
them appear with the desired padding, colour, size, etc.

» Both of the buttons should call the "self.checkAnswer" method when clicked, passing it a parameter value of either True or False depending on which button is clicked. Sending a parameter to a method called by clicking a button is a little bit tricky, so here is some example code:

6. Lastly, the constructor should end by calling the "self.showQuestion" method to place the first question 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, and some optional additions and enhancements. You are not required to submit pseudocode for your design of "fruit_test.py", as pseudocode is not particularly well suited to illustrating the design of an event-driven GUI program.

Methods in the GUI class of "fruit_test.py"
Your GUI class requires two methods to implement the functionality of the program - "self.showQuestion" and "self.checkAnswer". As part of the GUI class of "fruit_test.py", you must define these methods. I have described the required functionality of the methods rather than giving step-by-step instructions: It is up to you to design and implement the necessary code.

1. The "self.showQuestion" method randomly selects two fruit from self.data and a nutritional component from self.components and uses them to show a "True/False" question in the GUI. The question should be in the following format (or similar):
100 grams of <name of fruit 1> contains more <name of component>
than 100 grams of <name of fruit 2>.

» Use "random.sample()" to select two different random fruit from self.data, and "random.choice()" to select a random nutritional component from self.components.
» Include some code to make sure that the component name appears correctly if Vitamin C is selected.
i.e. The program should display "Vitamin C" instead of "vitamin_c".
» This method will involve some concatenation of variables and text, and setting the text of a Label.
» This method is called at the end of the constructor (to select/show the first question) and by the "self.checkAnswer" method (to select/show the next question).
» Store the selected fruit and the selected nutritional component as attributes (i.e. include "self." at
the start of the variable name) so that you can access them in the "self.checkAnswer" method.

2. The "self.checkAnswer" method determines whether the user clicked the correct button and shows a Correct/Incorrect messagebox. The method should receive a parameter named "answer", which will contain True or False (boolean) depending on which button was clicked. Your code must determine if the user's answer is correct, and show an appropriate messagebox, and then call the "self.showQuestion" method to select and show a new question.
» Use the attributes containing the currently selected fruits and currently selected component and a ">" comparison to determine whether the question is True or False, and then compare that to the "answer" parameter.

These two methods are all that are required to implement the functionality of the program, but you may write/use additional methods if you feel they improve your program.

Optional Additions and Enhancements for "fruit_test.py"

Below are some suggestions for minor additions and enhancements that you can make to the program to further test and demonstrate your programming ability. They are not required and you can earn full marks in the assignment without implementing them.

» Use attributes to keep track of the number of questions that have been asked and how many questions have been answered correctly, and use it to show a score in the GUI.

» Improve the presentation of your GUI by using padding, making sure that there is space between the buttons, using colour or different font sizes to make information stand out, etc.

» Make the Correct/Incorrect messageboxes more informative by including comparative information about the selected nutritional component of the two fruit, e.g.:

» Implement a countdown timer of 6 seconds that is shown on the GUI - if the user does not answer within 6 seconds, show a messagebox saying that they ran out of time and call "self.showQuestion" to create another question. Timers are covered in Reading 9.2.

Attachment:- Programming Principles.rar

Reference no: EM132314754

Questions Cloud

Identify a measurable patient-centered practice problem : Identify a measurable patient-centered practice problem related to quality or safety and relevant to your practice setting that you will also focus on in your.
What has changed since drucker time are the ways : Nurses are very much knowledge workers. What has changed since Drucker's time are the ways that knowledge can be acquired. The volume of data that can now be.
What are the presenting symptoms : What are the presenting symptoms? How is it distinguished from other similar disorders (assessment findings, common in particular age/sex or ethnic group, etc.)
Find a random sample and explain how you got it : Math 171 - Descriptive/Sampling Project - Use statistical methods to gather and analyze data. Find a random sample and explain how you got it
Implement the functionality of the program : CSI6208 - Programming Principles - Edith Cowan University - apply the programming concepts we have covered throughout the unit. The concepts covered
Examining a problem you have seen in nursing : Nursing research is used to study a dilemma or a problem in nursing. Examine a problem you have seen in nursing. Provide an overview of the problem.
Describe the nursing practice problem in brief : The first step of the evidence-based practice process is to evaluate a nursing practice environment to identify a nursing problem in the clinical area.
Describe how you developed a picot question : Describe how you developed a PICO(T) question focused on your chosen clinical issue of interest. Identify the four research databases that you used to conduct.
How you see yourself fitting into the iom future of nursing : In a reflection of 450-550 words, explain how you see yourself fitting into the following IOM Future of Nursing recommendations: Recommendation 4.

Reviews

Write a Review

Programming Languages Questions & Answers

  Demonstrates a basic knowledge of the programming language

Write a 2-3 page report over your programming languages topic. This may be either a paper or 2 pages of code that demonstrates a basic knowledge of the programming language.

  Write and test a class for a queue of objects

Your assignment is to write and test a class for a queue of objects. You may use any object class of your choice as the data for the queue.

  Write organized sequential code with no errors

Real application Assignment - Write organized, sequential code with no errors for dataset comparisons. The results to be generated on main screen

  Find a way to have only a few fibonacci numbers in memory

Implement an iterative solution that runs in O(n) time and returns a pointer to a HugeInteger struct that contains F(n). Be sure to prevent memory leaks before returning from this function.

  Prepare the sequence diagram

You have to prepare the sequence diagram

  Writing a class

Build a class for a type called Fraction

  Create a design for simulating the behavior of an elevator

You are to create an object-oriented design for simulating the behavior of an elevator. Your design should consist of an appropriate use case diagram.

  Write program to calculate cost of flowers sold at stand

Write down the program in class FlowerCounter which calculates the cost of flowers sold at flower stand. Five types of flowers-petunia, pansy, rose, violet, and camation- are stocked and cost.

  The related set of networked computers

In networking, the related set of networked computers (i.e. sites ending in .edu, .com, .gov, etc.)

  Write a program that determine which number produce sequence

Write a program that determines which numbers produce a happy sequence, within a series of test numbers. Main will call a user-defined function to read the first and last numbers for the range to test from the user.

  Use two-dimensional array in javascript to solve problem

Using javascript. Use a two-dimensional array to solve the following problem: A company has four salespeople (1 to 4) who sell five different products (1 to 5).

  Write a function to generate lsquorock lsquopaper or

write a function to generate lsquorock lsquopaper or lsquoscissors each answer being equally likely. write a script

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd