Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Overview Parkland Food Service has asked you to develop a meal planning system t

ID: 3911075 • Letter: O

Question

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

#include

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();

}

I really need your help

Explanation / Answer

Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you

input file: foods.txt
========
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


main.cpp
-------
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

struct food{
string name,group;
float cal,per;
};

struct node{
food foodItem;
node *next;
};

class FoodList{
private:
node* first;
int size;
public:
FoodList(){
first = NULL;
}

void add(food f){
node *n = new node;
n->foodItem = f;
n->next = NULL;
if(first == NULL)
first = n;
else
{
node *last = first;
//find the last node
while(last->next != NULL)
last = last->next;
last->next = n;
}
size++;
}

void print() const{

cout <<left << setw(20) << "Name" << setw(15) <<"Food group" << setw(10) <<"Calories" << setw(15) << "Daily percentage" << endl;
for(node *curr = first; curr != NULL; curr= curr->next){
cout <<left << setw(20) << curr->foodItem.name
<< setw(15) << curr->foodItem.group
<< setw(10) << curr->foodItem.cal
<< setw(15) << curr->foodItem.per << endl;
}

cout << endl;

}

//searches and fills the param f with the found food and returns true if found, false if not found
bool findFood(string name, food &f) const
{
for(node *curr = first; curr != NULL; curr= curr->next){
if(curr->foodItem.name == name)
{
f = curr->foodItem;
return true;
}
}
return false;
}

int getSize(){
return size;
}

void deleteAbove(double calories){
node *curr = first;
node *prev = NULL;

while(curr != NULL)
{
if(curr->foodItem.cal > calories)
{
//delete the node
node* next = curr->next;
if(prev == NULL)
first = next;
else
prev->next = next;
delete curr;
curr = next;
size--;
}
else
{
prev = curr;
curr = curr->next;
}
}
}

food getRandomFood() const
{
int index = rand() % size;
node *curr = first;
for(int i = 0; i < index; i++)
curr = curr->next;
return curr->foodItem;
}


};

void printMeal(food mealArr[3]);
void userMeal(const FoodList &flist, food mealArr[3]);
void randomMeal(const FoodList &flist, food mealArr[3]);
void deleteMeals( FoodList &flist);

int main()
{

// Open file
//string filename = "/home/staff/dbock/csc125/projects/project1/foods.txt";
string filename = "foods.txt";
fstream in(filename.c_str() , ios::in );

if(in.fail())
{
cout << "ERROR: could not open input file " << filename << endl;
return 1;
}
// Read lines

string name,group;
float cal,per;
cout << fixed << setprecision(2);
FoodList flist;
while( in >> name >> group >> cal >> per )
{
food f;
f.name = name;
f.group = group;
f.cal = cal;
f.per = per;
flist.add(f);
}

// Close file
in.close();

int choice = 0;
food mealArr[3];
srand(time(0));
while(choice != 5)
{
cout << "Welcome to Parkland Meal Selector" << endl;
cout << "Please select from the following" << endl;
cout << "1 - List food database" << endl;
cout << "2 - Create meal by manual selection" << endl;
cout << "3 - Create meal by random selection" << endl;
cout << "4 - Remove foods high in calorie" << endl;
cout << "5 - Exit" << endl;
cin >>choice;

switch(choice)
{
case 1: flist.print();
break;
case 2: userMeal(flist, mealArr);
printMeal(mealArr);
break;
case 3: randomMeal(flist, mealArr);
printMeal(mealArr);
break;
case 4:
deleteMeals(flist);
break;

case 5:
break;
default:
cout << "Invalid menu choice!" << endl;
}
}

}


void printMeal(food mealArr[3]){
double totalCal = 0;
double totalPer = 0;
cout << "===============================" << endl;
cout << "Your selected meal" << endl;

cout << " Foods: ";
for(int i = 0; i < 3; i++){
cout << mealArr[i].name << " ";
totalCal += mealArr[i].cal;
totalPer += mealArr[i].per;
}
totalPer *= 100;
cout << endl << " Total Calorie count: " << totalCal << endl;
cout << " Total Daily percentage: %" << totalPer << endl;
cout << "===============================" << endl;
cout << endl;

}

void userMeal(const FoodList &flist, food mealArr[3]){
int i =0;
food f;
string name;
while(i < 3){
cout << "Enter food name " << i << ": ";
cin >> name;
if(flist.findFood(name, f))
{
mealArr[i] = f;
i++;
}
else
cout << "No matching food: " << name << endl;
}
}

void randomMeal(const FoodList &flist, food mealArr[3])
{
food f;

for(int i = 0; i < 3; )
{
f = flist.getRandomFood();

//check if food already in meals
bool dup = false;
for(int j = 0; j < i; j++)
{
if(mealArr[j].name == f.name)
{
dup = true;
break;
}
}

if(!dup)
{
mealArr[i++] = f;
}
}
}
void deleteMeals( FoodList &flist)
{
double calories;
cout << "Enter calorie limit: ";
cin >> calories;
flist.deleteAbove(calories);
}


output
-----
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.00 0.30
Salmon protein 600.00 0.20
Strawberries fruit 200.00 0.02
Asparagus vegetable 32.00 0.10
Salad vegetable 100.00 0.20
Spaghetti carbs 200.00 0.10
Potato carbs 200.00 0.20
Hamburger protein 300.00 0.20
Milk dairy 200.00 0.10
Yogurt dairy 200.00 0.20

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
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.00
Total Daily percentage: %60.00
===============================

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
3
===============================
Your selected meal
Foods: Salmon Hamburger Milk
Total Calorie count: 1100.00
Total Daily percentage: %50.00
===============================

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
4
Enter calorie limit: 250
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
5