Project 1

Total Points:              10 pts.

Overview
Parkland Food Service has asked you to develop a meal planning system that allows users to create meals based on a list of foods stored in a file. Write a C/C++ program to read a list of foods from a file on disk including food name, food group, number of calories and daily percentage amounts. Allow a user to perform various operations on this food list including listing the foods, manually selecting foods to create a meal, randomly selecting foods to create a meal, computing total calorie and daily percentage amounts for created meals, and removing foods from the list over a specified amount input from the user.

Design
As discussed in the lecture notes (data modeling w/ structures), object-oriented program development begins with thinking about solutions differently. An essential element to well-developed OO programs is the design and construction of robust data models and data structures to reflect the program specification. After reading the program description above, one obvious candidate for a data model is a “food” object. Design this data model (and other candidates you identify) as a C/C++ struct as described in the notes. When reading, creating, and storing the list of foods from the file, you are to assume you do not know the number of foods in the file beforehand. For this reason, you must read and manage the list of foods as a linked list as described in the lecture notes (data structures – linked lists). As described in the notes, create and operate on this linked list of foods in dynamic memory during the execution of the program. You may not use the STL (e.g. vector) or a fixed-size array for this project. Specifically, your program must:

a) Open the following data file on disk containing information for each food (for information on opening, reading, and closing files, see the reference section below):
 

/home/staff/dbock/csc125/projects/project1/foods.txt


Each line in the file represents a single food with four pieces of information including the name of a food, the food group, the number of calories, and the daily percentage. Below are examples:
 

Salmon protein 600 0.2

Strawberries fruit 200 0.02

Asparagus vegetable 32 0.1

b) Create a data model definition (using a C/C++ struct) to organize and store the food information as discussed in the lecture notes (data modeling w/ structures).

c) Upon reading each line, create data structure objects dynamically (using new or malloc), store the information in the dynamically created object, and add the object to a linked list of data structures. Each line must create a new dynamic object added to your linked list as in the lecture notes example. You are to read the file under the assumption that you do not know the number of lines in the file ahead of time. You may not use the STL or a fixed-size array for this project. After reading is complete, close the data file.

d) After the linked list of food objects has been constructed, provide functionality to allow a user to perform the following operations:
    1) List all foods in the database file
    2) Create a meal by manually selecting three foods (print the meal showing total calories and daily percentages of three foods)
    3) Create a meal by randomly selecting three foods (print the meal showing total calories and daily percentages of three foods)
    4) Remove foods from the list that are high in calorie (using a calorie limit input from the user)

For removal (item 4 above), you are to remove from the linked list those foods with calories over a user-specified limit. To remove an object from the linked list, you must alter the pointers within the objects and delete the objects above the specified calorie count. Using an if statement to simply not print these records does not suffice as removal of an object from the linked list.

Example

An example session (only showing a subset of foods from the file) might look like the following using color key: user input, program output, Unix prompt:

 

dbock@shaula:/> project1

---------------------------------

Welcome to Parkland Meal Selector

---------------------------------

Please select from the following

1 - List food database

2 - Create meal by manual selection

3 - Create meal by random selection

4 - Remove foods high in calorie

5 - Exit

 

1

===============================================================================

Name                 Food group           Calories             Daily percentage

===============================================================================

Steak                protein              400                  0.30

Salmon               protein              600                  0.20

Strawberries         fruit                200                  0.02

Asparagus            vegetable            32                   0.10

Salad                vegetable            100                  0.20

Spaghetti            carbs                200                  0.10

Potato               carbs                200                  0.20

Hamburger            protein              300                  0.20

Milk                 dairy                200                  0.10

Yogurt               dairy                200                  0.20

 

2

Enter food name 0: Steak

Enter food name 1: Salad

Enter food name 2: Milk

===============================

Your selected meal

  Foods: Steak Salad Milk

  Total Calorie count: 700

  Total Daily percentage: %60

===============================

 

3

===============================

Your selected meal

  Foods: Yogurt Steak Asparagus

  Total Calorie count: 632

  Total Daily percentage: %60

===============================

 

4

Enter calorie limit: 250

 

1

===============================================================================

Name                 Food group           Calories             Daily percentage

===============================================================================

Strawberries         fruit                200                  0.02

Asparagus            vegetable            32                   0.10

Salad                vegetable            100                  0.20

Spaghetti            carbs                200                  0.10

Potato               carbs                200                  0.20

Milk                 dairy                200                  0.10

Yogurt               dairy                200                  0.20


Reference
If you are unfamiliar with how to open a file on disk, read lines of information, and close the file, below is an example of how to handle such operations using C++ streams. We’ll be addressing these topics in-depth later in the semester.

#include <iostream>

#include <fstream>

using namespace std;

 

main()

{

  // Open file

  fstream in( "/home/staff/dbock/csc125/projects/project1/foods.txt", ios::in );

 

  // Read lines

  string name,group;

  float cal,per;

  while( in >> name >> group >> cal >> per )

    {

      cout << "Read food: " << name << ", " << group << ", " << cal << ", " << per << endl;

    }

 

  // Close file

  in.close();

}