Sie sind auf Seite 1von 3

Nutricalc 1.

0 Functional Specification
This document describes version 1.0 of the Nutricalc™ application. Nutricalc is an
application that keeps a database of nutritional data for various foods, and (optionally)
calculates nutritional statistics for proposed meals.

1. Components
Nutricalc 1.0 consists of these components:
 a simple in-memory database of nutritional statistics for various foods
 a simple command-line user interface allowing the user to:
o add new foods to the database
o delete foods from the database
o list the foods in the database
o quit the application

 an optional meal calculator that allows the user to create “meals” and obtain
nutritional statistics for those them. A “meal” is a set of foods chosen from the
food database.

1.1 Food Database


Each food in the database has the following data associated with it:
char *name; // name of the food
Category category; // what kind of food
float calories; // calories
float carbohydrates; // grams
float fat; // grams
float cholesterol; // grams
float sodium; // grams
float protein; // grams

Note that “name” is a pointer to enough bytes to store the name as a C string.
We’re using float for the numerical data because we have no need for the increased
precision we get with double, so there’s no need to use up the extra memory.
“Category” is the name of a C++ enumerated type that should be defined as follows:
enum Category {
unknown = -1, meat, poultry, seafood, dairy, vegetable,
fruit, grain, sweet, nCategories
};
Foods are stored in the database in a linear, indexed sequence. The first item in the
database has index 0 (zero), the second has index 1 (one), etc. A database with n foods
in it will use index numbers in the range [0 .. (n-1)].

1.2 Database Command-Line User Interface (DCLUI)


The DCLUI consists of five commands:
add: adds a new food to the database. Prompts the user to type in all of the
data needed for that food. Input is read from the standard input (cin).
list: prints all of the foods in the database to the standard output (cout)
delete: deletes a food from the database, specified by the index of that food in
the database
clear: deletes all foods from the database
quit: quits the application

Each of these commands is typed in as a single letter, a, l, d, or q. For the delete


command, there is also an index number on the line with the d. The DCLUI should also
accept the uppercase letter for each command (i.e. you could type a D instead of a d).

1.3 Meal Calculator (Optional)


The meal calculator allows the user to choose a set of foods from the food database for a
meal. The calculator will then display the nutritional statistics for that meal.
To add this feature to the application, you must first add an additional command to the
database DCLUI:
meal: switches to meal calculator mode (typed in as m or M)
The first thing meal calculator mode does is to print the contents of the database, so that
the user will know which foods are in the database. (Of course, in this version of
Nutricalc, the user has just typed them all in, but we’ll ignore that inconvenient fact.)
The meal calculator then prompts the user to enter the index numbers (one at a time) for
the foods that are to be included in the meal. The user indicates when the meal is
complete by entering a –1 (which is not a legal index number).
A food cannot be included more than once in a meal. Your code needs to keep track of
this and report an error to users when they attempt to add a food to a meal that already
contains that food.
When the user indicates that the meal is complete, the meal calculator prints the names of
the foods chosen for the meal, and then prints the nutritional statistics for the meal. The
nutritional statistics for a meal are just the sum of the statistics for the foods included in
the meal. In other words, the calories for a meal is the sum of the calories for each
food in the meal, etc.
Once the meal calculator has printed the statistics for a meal, it asks whether the user
would like to calculate statistics for another meal. If the user wants to do so (indicated by
answering y or Y to the question), the meal calculator will run again.
Each time the meal calculator is run again, it starts with an empty list of foods. Note that
you will have to reset the mechanism that prevents a food from being entered more than
once into the meal. (In other words, if you entered “apple” into the previous meal, you
want to make sure that you can enter it (once!) into the current meal.)
If the user does not want to calculate statistics for another meal, the application will quit.

2. Food Database Implementation


Implement your food database as an array of pointers to instances of Food. You will
need to keep track of how many instances you have created. Your instances should be
“packed” at the beginning of the array. In other words, if your array has 3 instance
pointers in it, those three pointers should occupy slots 0, 1, and 2. If the user deletes a
food, the foods above it in the array will need to be moved down by one to keep the array
“packed.”
In your code, create your array with 50 slots.

3. Memory Management
It is your responsibility to manage your dynamically-allocated memory (i.e. the memory
you create with the new command). Your application must release all of the memory it
allocates before it quits. Failure to do so is a memory leak, which is a very serious bug
(and which will cause serious consequences for your grade).

4. User Interface Details


A document of this length cannot possibly specify every single detail of a user interface.
Fortunately for all concerned, the prototyping team (aka Michael Trigoboff) has built a
prototype of Nutricalc 1.0. You can examine this engineering prototype for any user
interface details that are not made obvious by this specification. The prototype is
available to you in the assignment on Blackboard.
Your application should duplicate the prototype’s user interface in all details*. This
includes spacing, skipped lines, etc. Deviations will affect your grade.

*
But also use your common sense – I expect to see your own name after “Implemented by:”, not my name.