////%%%%%%%%%%%%%%%%%%%%%%%* Assignment 3 */%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void textfile(FILE*);
void Records_Init(FILE *);
void updateRecord(FILE *);
void deleteRecord(FILE *);
void Enter_Records(FILE *);
int  Processing_Options(void);
void Display_Records(FILE *);
void insertRecord(FILE *);
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
struct Library_Data {
   int BookNumber;
   char BookTitle[30];
   char UserName[30];
   char Date1[30];
   char Date2[30];
};
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
main()
{
   FILE *filePtr;
  char response;
   int process;

   printf("Should the file be initialized (Y or N): ");
   scanf("%c", &response);

   while (toupper(response) != 'Y' && toupper(response) != 'N') {
      printf("Invalid response. Enter Y or N: ");
      scanf("%c", &response);
   }

   if (toupper(response) == 'Y') 
{
      if ((filePtr = fopen("borrower.txt", "w")) == NULL) {
         printf("File could not be opened.\n");
         exit(1);
      }

      Records_Init(filePtr);
      Enter_Records(filePtr);
      fclose(filePtr);
 }

   if ((filePtr = fopen("borrower.txt", "r+")) == NULL) {
      printf("File could not be opened.\n");
      exit(1);
   }
   
   process = Processing_Options();
   while (process <=5 ) 
   {
      switch (process) 
	  {
         case 1:
            Display_Records(filePtr);
            break;
		case 2:
            deleteRecord(filePtr);
            break;

         case 3:
            insertRecord(filePtr);
            break;
		case 4:
            updateRecord(filePtr);
            break;

		case 5:
            textfile(filePtr);
			exit(0);

      }
	  process = Processing_Options();
   }

   fclose(filePtr);
   return 0;
}
//***********************************************************************
void Records_Init(FILE *fPtr)
{
	struct Library_Data blankItem = {0, "", "","",""};
   for (int i = 0; i <= 99; i++)
      fwrite(&blankItem, sizeof(struct Library_Data), 1, fPtr);
}
//************************************************************************
void Enter_Records(FILE *fPtr)
{
   struct Library_Data temp;

   printf("Enter the BookNumber (1 - 100, -1 to end input): ");
   scanf("%d", &temp.BookNumber);

   while (temp.BookNumber != -1) 
   {
      printf("Enter the Book Title, UserName, Lending Date, Return Date:\n");
      scanf(" %s%s%s%s", &temp.BookTitle, &temp.UserName, &temp.Date1, &temp.Date2);

      fseek(fPtr, temp.BookNumber * sizeof(struct Library_Data), SEEK_SET);
      fwrite(&temp, sizeof(struct Library_Data), 1, fPtr);

      printf("Enter the BookNumber (1 - 100, -1 to end input): ");
      scanf("%d", &temp.BookNumber);
   }
}
//*****************************************************************************
int Processing_Options(void)
{
   int selection;

   printf("\n%s\n%s\n%s\n%s\n%s\n%s\n", 
	      "Please select an option:",
         	  "1  Display", 
		  "2  Delete", 
		  "3  Insert", 
		  "4  Update",
		  "5  create a text file, then end.");

   scanf("%d", &selection);

   while (selection < 1 || selection > 5) {
      printf("Invalid selection. Please try again: ");
      scanf("%d", &selection);
   }
  

   return selection;
}
//******************************************************************************
void Display_Records(FILE *fPtr)
{
   struct Library_Data temp;

   fseek(fPtr, sizeof(struct Library_Data), SEEK_SET);

   while (!feof(fPtr)) {
      fread(&temp, sizeof(struct Library_Data), 1, fPtr);

      if (temp.BookNumber)
         printf( "%-9d%-20s%-20s%-20s%s\n",temp.BookNumber, temp.BookTitle, temp.UserName, temp.Date1, temp.Date2);
   }
}
//********************************************************************************
void updateRecord(FILE *fPtr)
{
   struct Library_Data temp;
   int BookNbr;

   printf("Enter the BookNumber for update: ");
   scanf("%d", &BookNbr);
   fseek(fPtr, BookNbr* sizeof(struct Library_Data), SEEK_SET);
   fread(&temp, sizeof(struct Library_Data), 1, fPtr);

   if (temp.BookNumber) {
      printf("Enter the Book title, UserName, Date lend, and Date back:\n");
      scanf(" %s%s%s%s", &temp.BookTitle, &temp.UserName, &temp.Date1, &temp.Date2);
      fseek(fPtr, temp.BookNumber * sizeof(struct Library_Data), SEEK_SET);
      fwrite(&temp, sizeof(struct Library_Data), 1, fPtr);
   }
   else
      printf("Cannot update. The record is empty.\n");
}
//*********************************************************************************
void insertRecord(FILE *fPtr)
{
   struct Library_Data temp;
   int BookNbr;

   printf("Enter the BookNumber for insertion: ");
   scanf("%d", &BookNbr);
   fseek(fPtr, BookNbr* sizeof(struct Library_Data), SEEK_SET);
   fread(&temp, sizeof(struct Library_Data), 1, fPtr);

   if (!temp.BookNumber) {
      temp.BookNumber = BookNbr;
      printf("Enter the Book title, UserName, Date lend, and Date back:\n");
      scanf(" %s%s%s%s", &temp.BookTitle, &temp.UserName, &temp.Date1,&temp.Date2);
      fseek(fPtr, temp.BookNumber * sizeof(struct Library_Data), SEEK_SET);
      fwrite(&temp, sizeof(struct Library_Data), 1, fPtr);
   }
   else
      printf("Cannot insert. The record contains information.\n");
}
//*********************************************************************************
void deleteRecord(FILE *fPtr)
{
   struct Library_Data blankItem = {0, "", "","",""}, temp;
   int part;

   printf("Enter the BookNumber for deletion: ");
   scanf("%d", &part);
   fseek(fPtr, part * sizeof(struct Library_Data), SEEK_SET);
   fread(&temp, sizeof(struct Library_Data), 1, fPtr);

   if (temp.BookNumber) {
      fseek(fPtr, part * sizeof(struct Library_Data), SEEK_SET);
      fwrite(&blankItem, sizeof(struct Library_Data), 1, fPtr);
      printf("Record deleted.\n");
   }
   else
      printf("Cannot delete. The record is empty.\n");
}
//**********************************************************************************
void textfile(FILE *fPtr)
{
	FILE * writePtr;
	struct Library_Data txtbook;
	fPtr = fopen("borrower.txt","r");
	writePtr= fopen("myborrower.txt","w");
	if(writePtr==NULL)
		printf("File could not be opened. \n");
	else
	{
	   //rewind(fPtr);
	   fprintf( writePtr,"%-9s%-20s%-20s%-20s%s\n","ID #  ", "Book "," UserName ", "Lend Date", "Return Date");
	   fprintf( writePtr,"%-9s%-20s%-20s%-20s%s\n","======  ", "====== "," ====== ", "=========", "========");
	   fread(&txtbook, sizeof(struct Library_Data), 1, fPtr);
		while(!feof(fPtr))
		{
		   if (txtbook.BookNumber !=0) 
			  fprintf(writePtr,"%-9d%-20s%-20s%-20s%-20s\n", txtbook.BookNumber, txtbook.BookTitle, txtbook.UserName, txtbook.Date1, txtbook.Date2);
		   fread(&txtbook, sizeof(struct Library_Data), 1, fPtr);
		}
	}
	fclose(fPtr);
	fclose(writePtr);
}
//***********************************************************************************