Saturday, 28 January 2017

Pharmacy Management System


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>


int main(){

FILE *fp,*ft;
char another,choice;

struct companyStock {

char stocks[50], sells[50], medicines[50], company[50];
};


struct companyStock e;
char numberOfProducts[50];
long int strSize;

fp=fopen("Stock Details.txt","rb+");

if(fp==NULL) {

fp=fopen("Stock Detail.txt","wb+");

if(fp==NULL){
puts("Cannot open file");

return 0;
}
}

strSize=sizeof(e);

while(1){

system("cls");

printf("Pharmacy Management System") ;
printf("\n\n");
printf("1. Add") ;
    printf("\n2. Show");
    printf("\n3. Modify");
    printf("\n4. Delete");
    printf("\n5. Exit");
    printf( "\n\n");
   
printf( " Select Your Choice : ");
   
    fflush(stdin);
choice = getche();
switch(choice){

case '1':

fseek(fp,0,SEEK_END);
another='Y';
while(another=='Y' || another=='y'){

system("cls");
           
   printf ( "Enter the number of Stocks : ");
                scanf ("%s",&e.stocks);getchar();
               
printf ("Enter the number of Sells : ");
                scanf ("%s",&e.sells);getchar();
               
printf ( "Enter the number of Medecines: ");
                scanf ("%s", &e.medicines);getchar();
               
printf (  "Enter the name of Company: ");
                scanf("%s", &e.company);getchar();
               
fwrite(&e,strSize,1,fp);
               
printf( "\n Add Another Record (Y/N) ");
                fflush(stdin); //
               
another = getchar();
            }
            break;

case '2':

system("cls");
rewind(fp);

printf( "=== View the Records in the Database ===");
         printf( "\n");
         
  while (fread(&e,strSize,1,fp) == 1){
           
  printf( "\n");
           printf("\n") ;

  printf ("Number of Stocks: %s",e.stocks) ;
 
  printf ("\nNumber of sells: %s",e.sells);
 
  printf("\nNumber of Medecines: %s",e.medicines);
 
  printf ("\nName of company: %s",e.company);
         
  }
           printf( "\n\n");
         
  system("pause");
           break;

 case '3' :
            system("cls");
            another = 'Y';
         
 while (another == 'Y'|| another == 'y')
          {
              printf("\n Enter the number of Stocks : ");
              scanf("%d", numberOfProducts);

            rewind(fp);
            while (fread(&e,strSize,1,fp) == 1)
            {
                if (strcmp(e.stocks,numberOfProducts) == 0)
                {
                printf( "Enter new number of Stocks : ");
                scanf("%c", e.stocks);
               
printf( "Enter new number of Sells : ");
                scanf("%c", e.sells);
               
printf( "Enter new number of Medecines : ");
                scanf("%c", e.medicines);
               
printf( "Enter name of new Company : ");
                scanf("%c" ,e.company);
               
fseek(fp, - strSize, SEEK_CUR);
                fwrite(&e,strSize,1,fp);
               
break;
            }
                else
               
                printf("Record not found");
           
}
           
printf( "\n Modify Another Record? (Y/N) ");
                fflush(stdin);
                another = getchar();
            }
           
break;
           
            case '4':
        system("cls");
            another = 'Y';
           
          while (another == 'Y'|| another == 'y')
          {
              printf( "\n Enter the number of stocks to delete : ");
              scanf("%d", numberOfProducts);

              ft = fopen("temp.dat", "wb");

              rewind(fp);
         
     while (fread (&e, strSize,1,fp) == 1)

                 if (strcmp(e.stocks,numberOfProducts) != 0)
                {
                    fwrite(&e,strSize,1,ft);
                }
                fclose(fp);
                fclose(ft);
         
       remove("Stock Details.txt");
         
       rename("temp.dat","Stock Details.txt");

                fp=fopen("Stock Details.txt","rb+");

                printf( "\n Delete Another Record (Y/N) ");
                fflush(stdin);
                another = getchar();
            }

              break;

              case '5':
              fclose(fp);
              printf( "\n\n");
           
 printf("\t\t     THANK YOU FOR USING THIS SOFTWARE");
           
 printf("\n\n");
           
 exit(0);
          }
     }


system("pause");

return 0;
}

No comments:

Post a Comment