Solved

Orange Juice Distributorship Program

Posted on 2006-11-30
6
392 Views
Last Modified: 2010-04-15
Problem Describtion:  suppose you own an orange juice distributorship that sells Columbia Gorge(ID number 1), Florida Natural(ID number 2), Tropicana(Id number 3), Minute Maid(ID number 4), and Odwalla(ID number 5)

Write a menu driven program that manages a collection of items the Menu commands will be:
A....Add a new Entry
D...Delete an Entry
E...Edit an Entry
P....Display the Inventory
S...Save Current Inventory to a file.
C...Clear the entire inventory
L....Load sales records from a file
Q...Quit

MOST OF THE CODE IS PROVIDED I NEED THE CODE FOR THE FUNCTIONS


//finished.c implementation of user defined structure bev_t
//to run a menu to add a bev-t, delete a bev_t, display the list, clear list
//save list to file, load list from file, edit one item by selecting the field

//*******************************
//this program assumes all incorrect input from the user
//*******************************



#include<stdio.h> //stndard I/O
#include<string.h> //c style string functions
#include <ctype.h> //toupper/ tolower character functions
#define SIZE 30
#define MAX 100


//user defined struct bev_t
typedef struct{
        char brand[SIZE];
        char company[SIZE];
        int inStock;
        char lastShipDate[SIZE];
        double cost;
        double price;
} orange_t;


//function prototypes

//runs the menu returns char selection
char menu( );

//add an entry to the array of bev_t increase size by one
void AddItem(orange_t or[], int *size);

//delete an entry move last item in list to location of entry
//decrease size by one
void DeleteItem(orange_t or[], int *size, int location);

//edit an entry in the bev_t array
void EditItem(orange_t or[], int location);

//select which field to edit in an entry
//returns the selection
int EditMenu( );

//display the entire list
void DisplayInventory(orange_t or[], int size);

//save the list to a file
void SaveInventory(orange_t or[], int size);

//clear the list - set size to 0
void ClearInventory(orange_t or[], int *size);

//load the list of bev-t entries from a file
void LoadSalesRecords(orange_t or[], int *size);

//find an item in the list
//returns the array subscript location of the item
int FindItem(orange_t or[], int size);

//display one bev_t entry in the list
void DisplayItem(orange_t or[], int location);

//hard code five entries into the list
void HardCodeEntries(orange_t entry[], int *size);

//convert a word to all upper case characters
char* BrandUpperCase(char word[]);


int main( )
{
        orange_t brand[MAX];

        char trash;
        char selection;

        int size =0;
        int location;

        HardCodeEntries(brand, &size);

        selection =menu( );
        while((selection!='Q')||(selection!='q'))
        {
                if ((selection == 'Q')||(selection =='q'))
                {
                        printf("\nGOODBYE in while\n");
                        return 0;
                }
                if ((selection == 'A')||(selection =='a'))
                {
                        printf("\nADD ENTRY\n");
                        AddItem(brand, &size);
                }
                else if ((selection == 'D')||(selection =='d'))
                {
                        printf("\nDELETE ENTRY\n");
                        location = FindItem(brand,size);
                        if(location == -1)
                        {
                                printf("Item not found, Nothing will be deleted");
                        }
                        else
                        {
                               
                                printf("\nLOCATION: %d\n",location);
                                DeleteItem(brand, &size, location);
                        }
                       
                }
                else if ((selection == 'E')||(selection =='e'))
                {
                        printf("\nEDIT\n");

                        location = FindItem(brand,size);
                        if(location == -1)
                        {
                                printf("Item not found, Nothing will be edited");
                        }
                        else
                        {
                               
                                printf("\nLOCATION: %d\n",location);
                                EditItem(brand, location);
                        }

               
                }
                else if ((selection == 'P')||(selection =='p'))
                {
                        printf("\nDISPLAY INVENTORY\n");
                        DisplayInventory(brand, size);
                }
                else if ((selection == 'S')||(selection =='s'))
                {
                        printf("\nSAVE\n");
                        SaveInventory(brand,size);
                }
                else if ((selection == 'C')||(selection =='c'))
                {
                        printf("\nCLEAR\n");
                        ClearInventory(brand,&size);
                }
                else if ((selection == 'L')||(selection =='l'))
                {
                        printf("\nLOAD\n");
                        LoadSalesRecords(brand, &size);
                }
                else
                {
                        printf("\nEntry not recognized\n");
                }
                scanf("%c",&trash);
                selection = menu( );
        }

        printf("\nGOODBYE\n");
        return 0;
}

void DisplayInventory(orange_t or[], int size)
{
        int i=0;

        for(i=0;i<size;i++)
        {
                printf("\n");
                printf("brand:\t\t%s\ncompany:\t%s\nin stock:\t%d\n",
or[i].brand,or[i].company,or[i].inStock);
                printf("ship
date:\t%s\ncost:\t\t%.2f\nprice:\t\t%.2f\n\n",or[i].lastShipDate,or[i].cost,or[i].price);
        }
}


char menu( )
{
        char choice;

        printf("Enter selection: \t(A) Add an entry\n");
        printf("\t\t\t(D) Delete an entry\n");
        printf("\t\t\t(E) Edit an entry\n");
        printf("\t\t\t(P) Display Inventory\n");
        printf("\t\t\t(S) Save current inventory to a file\n");
        printf("\t\t\t(C) Clear entire inventory\n");
        printf("\t\t\t(L) Load sales records from a file\n");
        printf("\t\t\t(Q) QUIT\n");

        scanf("%c",&choice);
               
        return choice;
}

void AddItem(orange_t or[], int *size)
{
        //prompt the user to input a brand,company,amount in stock,last ship date,cost and
price

}

char* BrandUpperCase(char word[])
{
        int wordSize;
        int i;
        wordSize = strlen(word);

        for(i = 0; i<wordSize; i++)
        {
                word[i] = toupper(word[i]);

        }
        printf ("\n Brand in al Caps: %s\n\n", word);
        return word;
       
}


void DeleteItem(orange_t or[], int *size, int location)
{
        //move the last element in the array to the location you want to delete
        //derement array size
}



void EditItem(orange_t or[],int location)
{
        int editField;

        printf("Here is the entry to edit:\n");
        DisplayItem(or,location);

        editField = EditMenu();

        if(editField == 1)
        {
                printf("\nEnter NEW brand: ");
                scanf("%s", or[location].brand);

                BrandUpperCase(or[location].brand);
        }
        else if(editField==2)
        {
                printf("\nEnter company: ");
                scanf("%s", or[location].company);
        }
        else if(editField==3)
        {
                printf("\nEnter amount in stock: ");
                scanf("%d", &or[location].inStock);
        }
        else if(editField==4)
        {
                printf("\nEnter last ship date: ");
                scanf("%s", or[location].lastShipDate);
        }
        else if(editField==5)
        {
                printf("\nEnter cost: ");
                scanf("%lf", &or[location].cost);
        }
        else if(editField==6)
        {
                printf("\nEnter price: ");
                scanf("%lf", &or[location].price);
        }

        else
        {
                printf("\nField selection not recognized\n");
        }


       
}

int EditMenu( )
{
        int select;
        printf("\nSelect the field to edit:\n");
        printf("1 to edit brand\n");
        printf("2 to edit company\n");
        printf("3 to edit amount in stock\n");
        printf("4 to edit last ship date\n");
        printf("5 to edit cost\n");
        printf("6 to edit price\n");

        scanf("%d", &select);
        return select;
}






void DisplayItem(orange_t or[], int i)
{
        printf("\n");
        printf("brand:\t\t%s\ncompany:\t%s\nin stock:\t%d\n",
or[i].brand,or[i].company,or[i].inStock);
        printf("ship
date:\t%s\ncost:\t\t%.2f\nprice:\t\t%.2f\n\n",or[i].lastShipDate,or[i].cost,or[i].price);
}


void SaveInventory(orange_t or[], int size)
{
        FILE * outp;
        if((outp = fopen("out.txt","w"))==0)
        {
                printf("\ncannot open file\n");
                return;
        }

        fprintf(outp,"%d", size);
        fwrite(or, sizeof(orange_t),size,outp);
        fclose(outp);

}



void ClearInventory(orange_t or[],int*size)
{
        *size = 0;
}



void LoadSalesRecords(orange_t or[], int*size)
{
//this function is supposed to retrive arraya elments from file
}


int FindItem(orange_t or[], int size)
{
        //search for the brand in the array, use strcmp functtion in a loop
        //return -1 if element not fond(no match)
       

}

void HardCodeEntries(orange_t entry[], int *size)
{
        //for charater arryas you need to use string copy function
         strcpy(entry[0].brand,"COLUMBIA");
         strcpy(entry[0].lastShipDate,"11/09/06");
         entry[0].cost=5.00;
         strcpy(entry[0].company,"Columbia Gorge Fruit Co");
         entry[0].price=6.00;
         entry[0].inStock=59;

        *size +=1;

         strcpy(entry[1].brand,"FLORIDA");
         strcpy(entry[1].lastShipDate,"11/09/06");
         entry[1].cost=5.00;
         strcpy(entry[1].company,"Floridas Natural LLC");
         entry[1].price=6.00;
         entry[1].inStock=59;

        *size +=1;

         strcpy(entry[2].brand,"TROPICANA");
         strcpy(entry[2].lastShipDate,"11/09/06");
         entry[2].cost=5.00;
         strcpy(entry[2].company,"PepsiCo");
         entry[2].price=6.00;
         entry[2].inStock=59;

        *size +=1;

         strcpy(entry[3].brand,"MINUTE");
         strcpy(entry[3].lastShipDate,"11/09/06");
         entry[3].cost=5.00;
         strcpy(entry[3].company,"Coca-Cola");
         entry[3].price=6.00;
         entry[3].inStock=59;
       
        *size +=1;

         strcpy(entry[4].brand,"ODWALLA");
         strcpy(entry[4].lastShipDate,"11/09/06");
         entry[4].cost=5.00;
         strcpy(entry[4].company,"Odwalla");
         entry[4].price=6.00;
         entry[4].inStock=59;

        *size +=1;

 
         
}
 
0
Comment
Question by:bestman711
  • 4
  • 2
6 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 500 total points
Comment Utility
Hi Bestman711,

This looks a lot like homework.  :)  Our responses are limited when it comes to providing code for homework.  We can assist you with most anything and answer virtually every question that you may have, but our offering of code is limited to snippets and demos.

Pick one of the functions with which you need and I'll walk you through the processes of coding it.

Kent
0
 

Author Comment

by:bestman711
Comment Utility
OK....can you walk me through this one


//add an entry to the array of bev_t increase size by one
void AddItem(orange_t or[], int *size);

0
 
LVL 45

Expert Comment

by:Kdo
Comment Utility

Cool.

The biggest hint(s) as to what we need to do are from the struct the defines the inventory:

//user defined struct bev_t
typedef struct{
        char brand[SIZE];
        char company[SIZE];
        int inStock;
        char lastShipDate[SIZE];
        double cost;
        double price;
} orange_t;


Add's pretty straight-forward.  You'll search for a free slot in the table and if there is one you'll prompt for each item in the structure, saving the user input in the record.  Actually, if we impose the rule that insert adds to the next slot, and delete shuffles items up if it deletes from the middle of the list, then we only need to add at the end.

Here's a sample.  It's very crude, but shows the necessary steps.  You might want to add error checking on input.



void AddItem (orange_t or[] int *size)
{
  char brand[SIZE];
  char company[SIZE];
  int inStock;
  char lastShipDate[SIZE];
  double cost;
  double price;

  if ((*size ) + 1 < SIZE)       //  Sanity check.  Make sure that the table isn't full
  {
    printf ("No room for more data....\n");
    return;
  }

  fprintf (stdout, "Enter:  Brand -- ");
  fgets (brand, SIZE, stdin);

  fprintf (stdout, "Enter:  Company -- ");
  fgets (company, SIZE, stdin);

  fprintf (stdout, "Enter:  instock -- ");
  fscanf (stdin, "%d", &inStock);
  fflush (stdin);

  fprintf (stdout, "Enter:  Last Ship Date -- ");
  fgets (lastShipDate, SIZE, stdin);

  fprintf (stdout, "Enter:  cost -- ");
  fscanf (stdin, "%f", &cost);
  fflush (stdin);

  fprintf (stdout, "Enter:  price -- ");
  fscanf (stdin, "%f", &price);
  fflush (stdin);

  char brand[SIZE];
  char company[SIZE];
  int inStock;
  char lastShipDate[SIZE];
  double cost;
  double price;

  strcpy (or[*size].brand, brand);
  or[*size].brand[strlen(brand)-1] = 0;   // fgets copies the carriage return so we need to delete it.

  strcpy (or[*size].company, company);
  or[*size].company[strlen(company)-1] = 0;   // fgets copies the carriage return so we need to delete it.

  or[*size].inStock = inStock;

  strcpy (or[*size].lastShipDate, lastShipDate);
  or[*size].lastShipDate[strlen(lastShipDate)-1] = 0;   // fgets copies the carriage return so we need to delete it.

  or[*size].cost = cost;

  or[*size].price = price;

  *size = *size + 1;
}



Kent
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 45

Expert Comment

by:Kdo
Comment Utility

Sorry -- delete the declarations from the middle of the function.  I'd copied them there for reference as this edit box is pretty darned small.  :O


Kent
0
 

Author Comment

by:bestman711
Comment Utility
is it possible for you to help me with the delete function, loadsale, and find?

i don't know how to do them.

i you can't can you give me some references
0
 
LVL 45

Expert Comment

by:Kdo
Comment Utility

I'll gladly give some more help, but we're wandering dangerously close to what code that I can provide.

Find is a snap.  You'll want to scan down the list to find the correct item.  The function skeleton is given as:

int FindItem(orange_t or[], int size)
{
        //search for the brand in the array, use strcmp functtion in a loop
        //return -1 if element not fond(no match)
       

}

This gives us a problem as the header doesn't tell us what we're searching for.  That is, from the header we have the array and the array size, but not the brand that we need to find.  To solve this we probably need to modify the header and function call.  Let's make it look like this:

int FindItem(orange_t or[], int size, char *Brand)
{
        //search for the brand in the array, use strcmp functtion in a loop
        //return -1 if element not fond(no match)
       
 
}

Now it's a simple matter to search through the table to find the Brand that we passed to the function.

int FindItem(orange_t or[], int size, char *Brand)
{
  int idx;

  //search for the brand in the array, use strcmp functtion in a loop
  for (idx = 0; idx < size; ++idx)
    if (strcmp (or[idx].brand, Brand) == 0)
      return (idx);

  //return -1 if element not found(no match)
       
  return (-1);
}


Delete will work much the same way.  You'll call FindItem to see if the item exists, if so you'll run a small loop like the one above to "squeeze" out the item that is being deleted.  For example, if the first 6 items are A, B, C, D, E, and F and we delete D, the new list is A, B, C, E, F.

Give it a try and post your code.  I'll help you to correct it.

Kent
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now