Describe a requirement of the program

Assignment Help Other Subject
Reference no: EM132314104

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.

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.

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.

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.

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

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.

Attachment:- Programming Principles.rar

Reference no: EM132314104

Questions Cloud

Organizations to approve all project transactions : What are ways to empower buyer and seller organizations to approve all project transactions other than including a caveat
Guards against a breach of personal information : Could you describe two ways an organization guards against a breach of your personal information?
Understanding of the largest transformational change : Provide an understanding of the largest transformational change that you have been responsible for within an organisation that has involved some form
What factors have gone into making this team success : Reflect on a team of which you have been a part or a team. What factors have gone into making this team either success or dysfunctional
Describe a requirement of the program : CSI6208 - Programming Principles - Edith Cowan University - describe a requirement of the program, and bullet points (in italics) are additional details
Why would a business need to use a balanced scorecard : Why would a business need to use a balanced scorecard? Why is it so difficult to measure the success of an enterprise system?
Examples of strategies may be implemented : Give five examples of strategies may be implemented for the purpose of engendering an organisation's culture with the flexible workforce.
Project management options in terms of schedule : Evaluate your project management options in terms of schedule, performance, and cost for this scenario. Support your evaluation.
Understanding of the term return on investment : As a project leader, demonstrate your understanding of the term Return on investment (ROI).

Reviews

Write a Review

Other Subject Questions & Answers

  Cross-cultural opportunities and conflicts in canada

Short Paper on Cross-cultural Opportunities and Conflicts in Canada.

  Sociology theory questions

Sociology are very fundamental in nature. Role strain and role constraint speak about the duties and responsibilities of the roles of people in society or in a group. A short theory about Darwin and Moths is also answered.

  A book review on unfaithful angels

This review will help the reader understand the social work profession through different concepts giving the glimpse of why the social work profession might have drifted away from its original purpose of serving the poor.

  Disorder paper: schizophrenia

Schizophrenia does not really have just one single cause. It is a possibility that this disorder could be inherited but not all doctors are sure.

  Individual assignment: two models handout and rubric

Individual Assignment : Two Models Handout and Rubric,    This paper will allow you to understand and evaluate two vastly different organizational models and to effectively communicate their differences.

  Developing strategic intent for toyota

The following report includes the description about the organization, its strategies, industry analysis in which it operates and its position in the industry.

  Gasoline powered passenger vehicles

In this study, we examine how gasoline price volatility and income of the consumers impacts consumer's demand for gasoline.

  An aspect of poverty in canada

Economics thesis undergrad 4th year paper to write. it should be about 22 pages in length, literature review, economic analysis and then data or cost benefit analysis.

  Ngn customer satisfaction qos indicator for 3g services

The paper aims to highlight the global trends in countries and regions where 3G has already been introduced and propose an implementation plan to the telecom operators of developing countries.

  Prepare a power point presentation

Prepare the power point presentation for the case: Santa Fe Independent School District

  Information literacy is important in this environment

Information literacy is critically important in this contemporary environment

  Associative property of multiplication

Write a definition for associative property of multiplication.

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