mathieutozer.com

em ay tee aich ai ei you tee oh zed ei ar dot com




Fridge Master: My First Computer Application

0 comments

When I was studying 1st year CS at Monash, I wrote this program as my Major Assignment. I was pretty proud of the little bugger. It was meant to keep track of an inventory of food items in your fridge. I'm guessing the date of this.

#include
#include
#include
#define DOCKET_FILE "docket.txt"
#define MAX_PRODUCT_NAME 20
#define MAX_FRIDGE_SPACE 20


struct nutritionType //declares structure for nutrition units
{
float protein, fat, carbohydrate, kilojoules;
};



struct product // declares the structure for each product unit
{
char product_name [MAX_PRODUCT_NAME+1];
long int index_number;
float price, current_amount, container_Size, optimal_level,
dangerously_low_level;
struct nutritionType nutrition;
} ;

/***********************************Function prototypes*********************************************/
void print_contents_of_fridge (const struct product fridge[], int n);
int use_a_product (struct product fridge[]);
int find_product (char product_name[], const struct product fridge[]);
int generate_shopping_list (const struct product fridge[], int
number_of_items_in_fridge);
int put_shopping_in_fridge (struct product fridge[], int n);



/*******************************************Main****************************************************
* main() contains the menu and the declaration of the array of structs
for the fridge *

****************************************************************************************************/

int main()
{
struct product fridge[MAX_FRIDGE_SPACE] = /*array of stuctures, the
fridge*/
/*item name indexno price c_am container optimal danger p f c kj*/
{{"Milk", 1, 2.50, 2000, 1000, 2000, 250, {4.1, 1.2, 5.9, 215}},
{"Juice", 2, 3.50, 2000, 1000, 2000, 250, {0.8, 0.3, 9.4, 185}},
{"Beer", 3, 5.50, 2000, 1000, 2000, 250, {0.3, 0., 2.7, 285}},
{"Tofu", 4, 1.95, 400, 100, 400, 100, {14, 9.6, 2.8, 620}}};
/*unused spaces are initialised to 0 */
/*Local variables*/
int len = 0, i;
char code;

/* Determines number of products in the fridge array*/
for (i = 0; i < MAX_FRIDGE_SPACE; i++) {
if (fridge[i].index_number != 0){
len++;
}
else
continue; /* go onto next product*/
}


printf("You have %d things in your fridge. The max capacity of your fridge is %d.\n\n", len, MAX_FRIDGE_SPACE);

/*MENU*/
for (;;) {
printf("\nMenu fridge master v1.0\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("To use a product, press 'u'\n");
printf("To print an inventory of your fridge, press 'i'\n");
printf("To add shopping to the fridge, press 'd'\n");
printf("To generate a shopping list of things you need to buy, press's'\n");
printf("To end this program, press 'q'\n\n");
printf("Please make a selection: ");
scanf("%c", &code);
printf("\n\n");
while (getchar() != '\n'); //skips to end of line

switch (code) {
case 'u': if((use_a_product(fridge) != 0)) printf("\n\n");break;
case 's': if((generate_shopping_list(fridge, len)) != 0) printf ("Error printing shopping list"); break;
case 'd': if((put_shopping_in_fridge(fridge, len)) != 0) printf ("\nError adding shopping list to fridge"); break;
case 'i': print_contents_of_fridge(fridge, len); break;
case 'q': printf ("Goodbye."); return 0;
default: printf("!!!!!!That's not a legal selection. Please make another!!!!!!!!\n");
}
printf("\n");
}

}

/****************************************put shopping in fridge*********************************************
* Called when the docket.txt is to be added to the fridge *
* This reads from a txt file product name, match it with an index using
find product, *
* and then replace current level using a pointer. *

************************************************************************************************************/
int put_shopping_in_fridge (struct product fridge[], int n)
{
/*Local variables*/
char product_name[MAX_PRODUCT_NAME];
float quantity_bought;
int i,j;

/*Open a file*/
FILE *fp;

fp = fopen(DOCKET_FILE, "r");
if (fp == NULL) {
printf("Can't open %s\n", DOCKET_FILE);
exit(EXIT_FAILURE);
}
else
printf ("\n");

/*reads in each item from the docket.txt file and puts it in the array*/
i = 0;
while(i < n)
{
if(fscanf(fp, "%s %f", product_name, &quantity_bought) == 2){
}

j = (find_product(product_name, fridge)); /* Calls function to find the product in the fridge*/

if (j >= 0) {
fridge[j].current_amount = (fridge[j].current_amount) + (quantity_bought
* fridge[j].container_Size); /*updates the current amount*/
}
else
printf ("Oops. I Couldn't find ""%s"" in your fridge. Moving onto next the item on the docket.\n\n", product_name);
i++; /*Increment counter*/
}
/*Closes the file*/
fclose(fp);
printf("The file %s has been opened successfully and added to your fridge inventory.\n", DOCKET_FILE);
return 0;

}

/********************************************print a shopping list******************************************
* Takes fridge array and processes each item in the fridge one by one and prints items to be bought
* Whether the item needs to be bought is determined by the optimum level.
*function is passed fridge array and the length of it. returns o if successful. *************************************************************************************************************/
int generate_shopping_list (const struct product fridge[], int
number_of_items_in_fridge)
{
/*Local variables*/
int i, item_counter = 0;
float total_cost = 0.00, quantity_to_buy = 0.00,
number_of_containers_to_buy = 0.00;

printf ("Items with an asterix '*' beside their cost are at or below dangerous levels!\n");
printf ("No. Item.\t\tNo. of items.\t Cost\n"); //column headers

/*Go thru the entire fridge*/
for (i = 0; i < number_of_items_in_fridge; i++)
{
if ((fridge[i].optimal_level > fridge[i].current_amount) ||
(fridge[i].current_amount) == 0.00){ /*calcs. whether need to buy in terms*/
quantity_to_buy = (fridge[i].optimal_level) - (fridge[i].current_amount); /*quantity to buy and...*/
number_of_containers_to_buy = quantity_to_buy /
(fridge[i].container_Size); /*number of containers needed to buy*/
}
else
continue; //continue onto next item in the fridge

item_counter++;
printf ("%d: %-25s\t%.1f\t$%.2fc", item_counter, fridge[i].product_name,
number_of_containers_to_buy, (fridge[i].price *
number_of_containers_to_buy));

if (fridge[i].current_amount <= (fridge[i].dangerously_low_level)){
printf (" *");
}

printf("\n");
total_cost = (total_cost + (number_of_containers_to_buy) *
(fridge[i].price));
}

printf("\n\n");
printf ("The total estimated cost of your shop is: $%.2fc.\n\n",
total_cost);

return 0;
}

/************************************print inventory offridge**********************************************
*This function simply prints the contents of the fridge to the screen. *
*It returns nothing, and is passed the fridge array and the no of items in it. *
************************************************************************************************************/
void print_contents_of_fridge (const struct product fridge[], int n)
{
int i;

printf ("Here's what you've got in the fridge at the moment.\n");
printf ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf ("Item Name\t\tCurrent Amount in gm or ml\n");
for (i = 0; i < n; i++)
printf ("%-25s%11.2f\n", fridge[i].product_name,
fridge[i].current_amount);
printf("\n");

}

//printf("Product %s\n", fridge[1].product_name);
//printf("Protien content %f\n", fridge[1].nutrition.protein);


/*********************************use a product*****************************************************
*called when someone is going to take something out of the fridge. *
*Provides a variety of information to the screen and updates the fridge database. *
*Is passed the fridge array *
***************************************************************************************************/
int use_a_product (struct product fridge[])
{
/*Local variables*/
char product_to_use[MAX_PRODUCT_NAME+1];
int index;
float amount_used;
float new_amount;
float protein_eaten, carbohydrate_eaten, fat_eaten, kilojoules_consumed;

/*Get input from user*/
printf("Type the name of the product you are going to use: ");
scanf("%s", product_to_use);
printf("\n");

/* Find the product index with find_product*/
index = find_product(product_to_use, fridge);

if (index == -1){
printf ("Sorry, couln't find %s in the fridge. Did you spell it right?\n", product_to_use);
return 1; /*Prints error message if not found and exits*/
}
else
printf ("\n");

printf("You currently have %.1f mls or gms of %s.\n",
fridge[index].current_amount, fridge[index].product_name);
printf("Please enter the amount of %s that you are going to use: ",fridge[index].product_name); //prompts the user to enter change in quantity
scanf("%f", &amount_used);

if (amount_used < 0){
printf ("You entered a negative value.\n\n");
return 1;
}

printf("\n\n");
if (amount_used == 0) {
printf ("You used nothing (amount entered was 0.00)\n\n");
return 0;
}

if (amount_used > (fridge[index].current_amount) ) {
printf ("Sorry, you don't have enough %s.\nPlease try again.\n\n",
fridge[index].product_name);
return 1;
}

new_amount = fridge[index].current_amount - amount_used;

fridge[index].current_amount = new_amount; /*updates the database*/

printf("Now you have %.1f mls or gms of %s in your fridge, after you have used %.f mls or gms.\n\n", fridge[index].current_amount,
fridge[index].product_name, amount_used);

protein_eaten = (amount_used * fridge[index].nutrition.protein)/100;
carbohydrate_eaten = (amount_used *
fridge[index].nutrition.carbohydrate)/100;
fat_eaten = (amount_used * fridge[index].nutrition.fat)/100;
kilojoules_consumed = (amount_used *
fridge[index].nutrition.kilojoules)/100;

printf ("Nutritional Information:\n");
printf ("Amount consumed: %.1f mls or gms (%.1f left).\n\n", amount_used,
new_amount); // prints readout of info to screen
printf ("Protein: %.1fgm, Carbohydrate: %.1fgm, Fat: %.1fgm, Kilojoules: %.1f.\n\n", protein_eaten, carbohydrate_eaten, fat_eaten,
kilojoules_consumed);


if (new_amount <= fridge[index].dangerously_low_level) //Alerts the user if dangerously low
printf ("Item is at or below a dangerously low level!\n\n");

return 0;
}

/***************************************find a product index************************************************
* Passed a string product_name,then loops until it finds a match in the fridge *
* array with product name. returns the index the product was matched at.
*
************************************************************************************************************/
int find_product (char product_name[], const struct product fridge[])
/*pointer to the main fridge (unchangeable)*/
{
int i;
for (i=0; i < MAX_FRIDGE_SPACE; i++){
if (strcmp(fridge[i].product_name, product_name) == 0) {
return i;
}
else
continue;
}
return -1;
}


+RSS | dev blog | Portfolio

About me

My status

Last posts

www.flickr.com
This is a Flickr badge showing public photos from mathieutozer200. Make your own badge here.
Locations of visitors to this page