Link to home
Start Free TrialLog in
Avatar of bestman711
bestman711

asked on

Orange Juice Distributorship Program

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;

 
         
}
 
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bestman711
bestman711

ASKER

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


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

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
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

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