#include <stdio.h>
struct emp
  {
    char name[40] ;
    int age ;
    float bs ;
  };
  
void add_record(FILE *);
void display_record(FILE *);
void modify_record(FILE *);
void delete_record(FILE *);
void query_record(FILE *);
void query_age(FILE *);
void query_salary(FILE *);
void query_name(FILE *);

struct emp e;
int recsize ;
main( )
{
  FILE *fp; 
  char another, choice ;
  //struct emp e ;
  char empname[40] ;
 
  fp = fopen ( "EMP.DAT", "rb+" ) ;
  if ( fp == NULL )
  {
    fp = fopen ( "EMP.DAT", "wb+" ) ;
    if ( fp == NULL )
    {
      printf( "Cannot create a file" ) ;
      system("pause");
      exit(0);
    }
  }
  recsize = sizeof ( e ) ;
  while ( 1 )
  {
    system("cls");
    printf("\nEmployee database management program \n");   
    printf ( "1. Add Records \n" ) ;
    printf ( "2. List Records \n" ) ;
    printf ( "3. Modify Records \n" ) ;
    printf ( "4. Delete Records \n" ) ;
    printf ( "5. Query \n");
    printf ( "0. Exit \n" ) ;
    printf ( "Your choice :  " ) ;
    fflush ( stdin ) ;
    scanf("%c", &choice);
    switch ( choice )
    {
      case '1' :
        system("cls");   
        add_record(fp);     
        break ;
      case '2' :
        system("cls");     
        display_record(fp);
        break ;
      case '3' :
        system("cls");
        modify_record(fp);
        break ;
      case '4' :
        system("cls");
        delete_record(fp);
        break ;
      case '5':
        system("cls");   
        query_record(fp);
        break;     
      case '0' :
        fclose ( fp ) ;
        exit(0) ;
      default:
        printf("enter correct choice \n"); 
        system("pause");    
    } // switch
  } // while(1)
}//main

void add_record(FILE *fp)
{
  char another;
  fseek ( fp, 0 , SEEK_END ) ;
  another = 'Y' ;
  while ( another == 'Y' || another == 'y' )
  {
    printf ( "\nEnter name, age and basic sal. " ) ;
    scanf ( "%s %d %f", e.name, &e.age, &e.bs ) ;
    fwrite ( &e, recsize, 1, fp ) ;
    printf ( "\nAdd another Record (Y/N) " ) ;
    fflush ( stdin ) ;
    another = getche( ) ;
  }     
}

void display_record(FILE *fp)
{
  rewind ( fp ) ;
  printf("Name  Age  Salary\n");
  while ( fread ( &e, recsize, 1, fp ) == 1 )
    printf ( "%s %d %0.2f \n", e.name, e.age, e.bs ) ;
  system("pause");     
}
void modify_record(FILE *fp)
{
   char another, empname[30];
   another = 'Y' ;
   while ( another == 'Y' )
   {
     printf ( "\nEnter name of employee to modify " ) ;
     scanf ( "%s", empname ) ;
     rewind ( fp ) ;
     while ( fread ( &e, recsize, 1, fp ) == 1 )
     {
       if ( strcmp ( e.name, empname ) == 0 )
       {
            printf ( "\nEnter new name, age & bs" ) ;
            scanf ( "%s %d %f", e.name, &e.age, &e.bs ) ;
            fseek ( fp, - recsize, SEEK_CUR ) ;
            fwrite ( &e, recsize, 1, fp ) ;
            break ;
          }
        }
        printf ( "\nModify another Record (Y/N) " ) ;
        fflush ( stdin ) ;
        another = getche( ) ;
     }    
}
void delete_record(FILE *fp)
{
  char another, empname[30];
  FILE *ft;
  another = 'Y' ;
  while ( another == 'Y' )
  {
        printf ( "\nEnter name of employee to delete " ) ;
        scanf ( "%s", empname ) ;
        ft = fopen ( "TEMP.DAT", "wb" ) ;
        rewind ( fp ) ;
        while ( fread ( &e, recsize, 1, fp ) == 1 )
        {
          if ( strcmp ( e.name, empname ) != 0 )
            fwrite ( &e, recsize, 1, ft ) ;
        }
        fclose ( fp ) ;
        fclose ( ft ) ;
        remove ( "EMP.DAT" ) ;
        rename ( "TEMP.DAT", "EMP.DAT" ) ;
        fp = fopen ( "EMP.DAT", "rb+" ) ;
        printf ( "Delete another Record (Y/N) " ) ;
        fflush ( stdin ) ;
        another = getche( ) ;
  }       
}
void query_record(FILE *fp)
{
  int choice;
  printf(":::: Query Record Section :::: \n");
  printf("1.  query by age \n");
  printf("2.  query by salary \n");
  printf("3.  query by name \n");
  scanf("%d", &choice);
  switch(choice)
  {
    case 1:
       query_age(fp);
       break;
    case 2:
       query_salary(fp); 
       break;
    case 3: 
       query_name(fp);
       break;      
     default:
       printf("please enter correct choice \n");
       system("pause");            
   }         
} 
void query_age(FILE *fp)
{  
  int age, choice;
  printf("Query record by Age \n");
  printf("Enter age for query : ");
  scanf("%d",&age);
  printf("Please select from the following \n");
  printf("1. greater than %d\n",age);
  printf("2. less than %d\n",age);
  printf("3. equal than %d\n",age);
  fflush(stdin);
  scanf("%d", &choice);
  switch(choice)
  {
    case 1:
      rewind ( fp ) ;
      printf("Name  Age  Salary\n");
      while ( fread ( &e, recsize, 1, fp ) == 1 )
      {
         if(e.age>age)   
           printf ( "%s %d %0.2f \n", e.name, e.age, e.bs ) ;
      }     
      break;
      case 2:
      rewind ( fp ) ;
      printf("Name  Age  Salary\n");
      while ( fread ( &e, recsize, 1, fp ) == 1 )
      {
         if(e.age<age)   
            printf ( "%s %d %0.2f \n", e.name, e.age, e.bs ) ;
      }     
      break;
      case 3:
      rewind ( fp ) ;
      printf("Name  Age  Salary\n");
      while ( fread ( &e, recsize, 1, fp ) == 1 )
      {
         if(e.age == age)   
            printf ( "%s %d %0.2f \n", e.name, e.age, e.bs ) ;
      }     
      break;
      default:
          printf("Please enter correct choice \n");         
    }
    system("pause"); 
}
void query_salary(FILE *fp)
{
     
}
void query_name(FILE *fp)
{

}
