Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#define true 0
#define false 1
int day1,month1,year1,day2,month2,year2;
struct tools
{
char id[10];
char PIC[20];
int quantity1,quantity2,quantity3;
int day1,month1,year1;
};
struct tools tool[30];
int count = 0;
FILE *f;
int writefile()
{
int i;
f = fopen("Inventory.text","w");
if (f==NULL)
return -1;
fprintf(f, "%d\n",count);
for (i=0;i<count;++i)
{
fputs(tool[i].id, f);
fprintf(f, "\n");
fputs(tool[i].PIC, f);
fprintf(f, "\n");
fprintf(f, "%d\n",tool[i].quantity1);
fprintf(f, "%d\n",tool[i].quantity2);
fprintf(f, "%d\n",tool[i].quantity3);
fprintf(f, "%d\n",tool[i].day1);
fprintf(f, "%d\n",tool[i].month1);
fprintf(f, "%d\n",tool[i].year1);
}
fclose(f);
return 0;
}
int readFile()
{
int n=0;
int i;
f=fopen("Inventory.txt", "r");
if (f==NULL)
return -1;
fscanf(f, "%d\n", &n);
for (i=0;i<n;++i)
{
fgets(tool[i].id, 10, f);
tool[i].id[strlen(tool[i].PIC)-1]=0;
fgets(tool[i].PIC, 20, f);
tool[i].id[strlen(tool[i].PIC)-1]=0;
fscanf(f, "%d", &tool[i].quantity1);
fscanf(f, "%d", &tool[i].quantity2);
fscanf(f, "%d", &tool[i].quantity3);
fscanf(f, "%d", &tool[i].day1);
fscanf(f, "%d", &tool[i].month1);
fscanf(f, "%d", &tool[i].year1);
}
fclose(f);
return n;
}
void addrecord()
{
printf("Enter The Ticket Number\n");
readFile();
if(count>0)
{
count=readFile();
IDChecker(0,count);
}
else
{
printf("\nTicket Number:");
fflush(stdin);
gets(tool[count].id);
}
printf("Person In Charge: ");gets(tool[count].PIC);
printf("Projector\t: ");scanf("%d", &tool[count].quantity1);
printf("White Screen\t: ");scanf("%d", &tool[count].quantity2);
printf("Audio System\t: ");scanf("%d", &tool[count].quantity3);
printf("Enter Today's Date (ex:XX/YY/ZZZZ):");
scanf("%d/%d/%d", &day1,&month1,&year1);
int daysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int n = day1;
day2 = day1 +7;
if (( month1 == 2 ) && (day1 == 29))
{
// Leap year checking, if yes, Feb is 29 days.
if(year1 % 400 == 0 || (year1 % 100 != 0 && year1 % 4 == 0))
{
daysInMonth[1] = 29;
}
}
if (day2 > daysInMonth[month1 -1])
{
day2 = (day2-*daysInMonth);
month2 = month1 +1;
if (month1 > 12)
{
month2 = 1;
year2 = year1 +1;
}
}
tool[count].day1=day2;
tool[count].month1=month2;
tool[count].year1=year2;
++count;
writefile();
}
void deleterecord()
{
count=readFile();
char id[10];
int i,j;
int z=false;
printf("Enter the Ticket Number thats been returned:");
fflush(stdin);
gets(id);
for(i=0;i<count;i++)
{
z=true;
if (strcmp(tool[i].id,id)==0)
{
for(j=i;j<(count-1);j++)
{
tool[j]=tool[j+1];
}
count--;
}
}
if (z==false)
{
printf("Can't ticket number:%s.",id);
}
writefile();
}
void displayrecord()
{
int i;
count = readFile();
if (count<0)
puts("cannot open file");
printf("\t\t\t wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww\n");
printf("\t\t\t Laboratory Inventory System\n");
printf("\t\t\t wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww\n");
printf("----------------------------------------------------------------\n");
printf("|No.| Person In Charge | Ticket ID | Projector | White Screen | Audio System | Due Date|\n");
printf("----------------------------------------------------------------\n");
for(i=0;i<count;i++)
{
printf("%d %s %s %d %d %d %d %d %d",i+1,tool[i].PIC,tool[i].id,tool[i].quantity1,tool[i].quantity2,tool[i].quantity3,tool[i].day1,tool[i].month1,tool[i].year1);
}
}
int IDChecker(int i,int j)
{
count=readFile();
printf("Product ID:");
fflush(stdin);
gets(tool[count].id);
if (strcmp(tool[i].id,tool[j].id)==0)
{
printf("ID Number is already taken!");
return IDChecker(i++,j--);
}
}
int checkID(char id[])
{
int i;
count=readFile();
readFile();
for(i=0;i<count;i++)
{
if(strcmp(id,tool[i].id)!=0)
{
fclose(f);
}
return 1;
}
fclose(f);
return 0;
}
int main()
{
int choice;
count=readFile();
if (count<0)
printf("Cannot locate file\n");
do {
printf("\n");
printf("\t\t\t wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww\n");
printf("\t\t\t Welcome To Laboratory Inventory System\n");
printf("\t\t\t wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww\n");
printf("\n\nPress:");
printf("\n\t1.)Add Record.");
printf("\n\t2.)Delete Record.");
printf("\n\t3.)Display Record.");
printf("\n\t4.)Exit the program.");
printf("\n\tChoice?\n");
scanf("%d", &choice);
switch(choice)
{
case 1: addrecord();
break;
case 2: deleterecord();
break;
case 3: displayrecord();
break;
case 4: exit(1);
break;
default: printf("Invalid Choices. Please try again.");
break;
}
}while(choice!=4);
printf("Thank You for using this program");
}