/*
Onur KARABAĞ
BİLGİSAYAR MÜH.
FATİH ÜNV.
onurkarabag@mynet.com
*/
#include<stdio.h>
#include<stdlib.h>
int menu();
void add();
void remove();
void info();
void rent();
void retun();
struct available_cars {
int ID;
char Make[15];
char Model[15];
int year;
int num_of_seats;
};
struct rented_cars {
int ID;
char Make[15];
char Model[15];
int year;
int num_of_seats;
char rented_date[10];
char return_date[10];
};
FILE *available, *rented;
int main()
{
int a, option;
struct available_cars info1={0, "", "", 0, 0};
struct rented_cars info2={0, "", "", 0, 0, "", ""};
if (( available=fopen( "Available Cars.dat", "rb+" )) == NULL)
{
if(( available=fopen("Available Cars.dat", "wb"))!= NULL)
{
printf("\n.::. The file [ -Available Cars- ] was created in your system .::.");
for(a=1; a<=50; a++)
fwrite(&info1, sizeof(struct available_cars), 1, available);
}
else
printf("\n\aThe system is unable to create the file \"Available Cars\" !");
}
if(( rented=fopen("Rented Cars.dat", "rb+")) == NULL)
{
if(( rented=fopen("Rented Cars.dat", "wb")) != NULL)
{
printf("\n\n.::. The file [ -Rented Cars- ] was created in your system .::.");
for(a=1; a<=50; a++)
fwrite(&info2, sizeof(struct rented_cars), 1, rented);
}
else
printf("\n\aThe system is unable to create the file \"Rented Cars\"!");
}
fclose(available);
fclose(rented);
if (( available=fopen( "Available Cars.dat", "rb+" )) == NULL)
{
printf("\nThe file 'Available Cars' could not be opened");
}
if (( rented=fopen( "Rented Cars.dat", "rb+" )) == NULL)
{
printf("\nThe file 'Rented Cars' could not be opened");
}
option=menu();
while(option!=6)
{
switch(option)
{
case 1:
add();
break;
case 2:
remove();
break;
case 3:
info();
break;
case 4:
rent();
break;
case 5:
retun();
break;
default:
;
}
option=menu();
}
fclose(available);
fclose(rented);
printf("\n---------------------------------------------------------");
printf("\n! Your data was saved succesfully! DO NOT WORRY! Goodby !");
printf("\n---------------------------------------------------------\n\n");
system("PAUSE");
return 0;
}
int menu()
{
int a;
printf("\n\n***************************************\n"
"\n* 1. Add a New Vehicle *\n"
"\n* 2. Remove an Existing Vehicle *\n"
"\n* 3. Vehicle information *\n"
"\n* 4. Rent a Vehicle *\n"
"\n* 5. Return a Vehicle *\n"
"\n* 6. Exit *\n"
"\n*************************************** ");
printf("\n\nEnter your option:");
scanf("%d", &a);
if(a<0 || a>6)
printf("\n\n\aInvalid option! please re-try!");
return a;
}
void add()
{
int id;
struct available_cars add_car;
printf("\n\nEnter Car's ID > 0 [-1 to end]: ");
scanf("%d", &id);
while(id>0)
{
fseek(available, (id - 1) * sizeof(struct available_cars), SEEK_SET);
fread(&add_car, sizeof(struct available_cars), 1, available);
if(add_car.ID!=0)
{
while(add_car.ID!=0)
{
printf("\n\a.::ID already exists or is out of bound! please re-enter ID [-1 to end]:");
scanf("%d", &id);
if(id == -1)
return;
fseek(available, (id - 1) * sizeof(struct available_cars), SEEK_SET);
fread(&add_car, sizeof(struct available_cars), 1, available);
}
}
printf("\n\nEnter Car's Make, Model, Number of Seats and Year : ");
scanf("%s %s %d %d",add_car.Make, add_car.Model, &add_car.num_of_seats, &add_car.year);
fseek(available, (id - 1) * sizeof(struct available_cars), SEEK_SET);
add_car.ID=id;
fwrite(&add_car, sizeof(struct available_cars), 1, available);
printf("\n\nEnter Car's ID > 0 [-1 to end]: ");
scanf("%d", &id);
}
}
void remove()
{
int delt;
struct available_cars del={0, "", "", 0, 0};
struct available_cars empty={0, "", "", 0, 0};
printf("\nEnter the Car's ID to Delete [-1 to end]: ");
scanf("%d", &delt);
if(delt == -1)
return;
fseek(available, (delt - 1) * sizeof(struct available_cars), SEEK_SET);
fread(∅, sizeof(struct available_cars), 1, available);
if(empty.ID==0 || delt<=0)
{
while(empty.ID==0 || delt<=0)
{
printf("\n\a.::Car ID # %d does not exist! please re-try: ", delt);
scanf("%d", &delt);
if(delt == -1)
return;
fseek(available, (delt - 1) * sizeof(struct available_cars), SEEK_SET);
fread(∅, sizeof(struct available_cars), 1, available);
}
}
printf("\nAre you sure you want to delete Car ID # %d [y/n]: ", delt);
if((getchar())== 'Y' || (getchar())== 'y')
{
fseek(available, (delt - 1) * sizeof(struct available_cars), SEEK_SET);
fwrite(&del, sizeof(struct available_cars), 1, available);
}
printf("\nDo you want to continue Deleting other Cars? [y/n]: ");
if((getchar())== 'Y' || (getchar())== 'y' )
remove();
}
void info()
{
int check=0;
struct available_cars car_info1;
struct rented_cars car_info2;
printf("\n\n A V A I L A B L E C A R S");
printf("\n***********************************\n");
printf("\n\n I D M A K E M O D E L S E A T S Y E A R ");
printf("\n--------------------------------------------------------------------\n");
rewind(available);
fread(&car_info1, sizeof(struct available_cars), 1, available);
while(!feof(available))
{
if(car_info1.ID!=0)
printf("\n %-12d %-15s %-18s %-12d %-14d\n", car_info1.ID, car_info1.Make, car_info1.Model, car_info1.num_of_seats, car_info1.year);
fread(&car_info1, sizeof(struct available_cars), 1, available);
}
rewind(available);
while(!feof(available))
{
fread(&car_info1, sizeof(struct available_cars), 1, available);
if(car_info1.ID!=0)
check++;
}
if(check==0)
printf("\n %3s %12s %16s %15s %13s", "...", "...", "...", "...", "...");
printf("\n");
printf("\n\n R E N T E D C A R S");
printf("\n******************************\n");
printf("\n\n ID MAKE MODEL SEATS YEAR RENTED DATE RETURN DATE");
printf("\n-------------------------------------------------------------------------------\n");
rewind(rented);
fread(&car_info2, sizeof(struct rented_cars), 1, rented);
while(!feof(rented))
{
if(car_info2.ID!=0)
printf("\n %-5d %-11s %-13s %-8d %-11d %-14s %-16s", car_info2.ID, car_info2.Make, car_info2.Model, car_info2.num_of_seats,
car_info2.year, car_info2.rented_date, car_info2.return_date);
fread(&car_info2, sizeof(struct rented_cars), 1, rented);
}
rewind(rented);
check=0;
while(!feof(rented))
{
fread(&car_info2, sizeof(struct rented_cars), 1, rented);
if(car_info2.ID!=0)
check++;
}
if(check==0)
printf("%3s %6s %12s %11s %10s %11s %15s", "..", "...", "...", "...", "...", "...", "...");
printf("\n");
}
void rent()
{
int id;
struct available_cars empty = {0, "", "", 0, 0};
struct available_cars info1;
struct rented_cars info2;
printf("\nEnter the Car's ID to rent [-1 to end]:");
scanf("%d", &id);
if(id == -1)
return;
fseek(available, (id - 1) * sizeof(struct available_cars), SEEK_SET);
fread(&info1, sizeof(struct available_cars), 1, available);
if(info1.ID==0 || info1.ID!=id)
{
while(info1.ID==0 || info1.ID!=id)
{
printf("\n\a.::.Car ID # %d does not exist in the available cars list!.::.\n\nPlease re-try [-1 to end]: ", id);
scanf("%d", &id);
if(id == -1)
return;
fseek(available, (id - 1) * sizeof(struct available_cars), SEEK_SET);
fread(&info1, sizeof(struct available_cars), 1, available);
}
}
fseek(available, (id - 1) * sizeof(struct available_cars), SEEK_SET);
fread(&info2, sizeof(struct rented_cars), 1, available);
printf("\nEnter the rented date and the return date [dd/mm/yyyy]:");
scanf("%s %s", info2.rented_date, info2.return_date);
fseek(rented, (id - 1) * sizeof(struct rented_cars), SEEK_SET);
fwrite(&info2, sizeof(struct rented_cars), 1, rented);
fseek(available, (id - 1) * sizeof(struct available_cars), SEEK_SET);
fwrite(∅, sizeof(struct available_cars), 1, available);
printf("\nDo you want to continue renting other Car's? [y/n]:");
if((getchar())== 'Y' || (getchar())== 'y' )
rent();
}
void retun()
{
int id;
struct rented_cars empty = {0, "", "", 0, 0, "", ""};
struct available_cars info1;
struct rented_cars info2;
printf("\nEnter the Car's ID to return [-1 to end]:");
scanf("%d", &id);
if(id == -1)
return;
fseek(rented, (id - 1) * sizeof(struct rented_cars), SEEK_SET);
fread(&info2, sizeof(struct rented_cars), 1, rented);
if(info2.ID==0 || info2.ID!=id)
{
while(info2.ID==0 || info2.ID!= id)
{
printf("\n\a.::.Car ID # %d does not exist in the rented cars list!.::.\n\nPlease re-try [-1 to end]: ", id);
scanf("%d", &id);
if(id == -1)
return;
fseek(rented, (id - 1) * sizeof(struct rented_cars), SEEK_SET);
fread(&info2, sizeof(struct rented_cars), 1, rented);
}
}
fseek(rented, (id - 1) * sizeof(struct rented_cars), SEEK_SET);
fread(&info1, sizeof(struct available_cars), 1, rented);
fseek(available, (id - 1) * sizeof(struct available_cars), SEEK_SET);
fwrite(&info1, sizeof(struct available_cars), 1, available);
fseek(rented, (id - 1) * sizeof(struct rented_cars), SEEK_SET);
fwrite(∅, sizeof(struct rented_cars), 1, rented);
printf("\nDo you want to continue returning other Car's? [y/n]:");
if((getchar())== 'Y' || (getchar())== 'y' )
retun();
}
|