Code:
switch(x)
{
// ENQUIRY SECTION
case 1:
endwin();
e();
flag=1;
break;
// ISSUE NEW POLICY SECTION
case 2:
endwin();
system("clear");
add_policy();
main();
flag=1;
break;
// CLAIM SECTION
case 3:
system("clear");
claim();
main();
}
claim module
-----------
void settle_claim(int policy_num_i, int claim_amount_i, char *);
claim()
{
//endwin();
clear();
char policy_num_s[10],claim_amount_s[10],claim_reason_s[60];
int policy_num_i, claim_amount_i;
int eof_flag = 0;
initscr();
start_color();
init_pair(1,COLOR_MAGENTA,COLOR_BLACK);
// Input and validate policy id, claim amount and claim reason
//c/lear();
//refresh();
move(LINES/2-10,COLS/2-10);
//attron(A_BOLD|A_UNDERLINE);
printw("INSUREFAST: CLAIM MODULE");
mvchgat(LINES/2-10,COLS/2-10,25,A_BOLD|A_UNDERLINE,1,NULL);
//attroff(A_UNDERLINE);
refresh();
//refresh();
//mvchgat(LINES/2-10,COLS/2-10,-1,A_BOLD,1,NULL);
//refresh();
move(LINES/2-8,COLS/2-8);
policy_num_i = 0;
while(policy_num_i == 0)
{
printw("Enter Policy number: ");
mvchgat(LINES/2-8,COLS/2-8,-1,A_BOLD,1,NULL);
refresh();
move(LINES/2-8,COLS/2+13);
getnstr(policy_num_s,9);
policy_num_i = atoi(policy_num_s);
}
move(LINES/2-6,COLS/2-8);
claim_amount_i = 0;
while(claim_amount_i == 0)
{
printw("Enter Claim Amount: ");
mvchgat(LINES/2-6,COLS/2-8,-1,A_BOLD,1,NULL);
refresh();
move(LINES/2-6,COLS/2+13);
getnstr(claim_amount_s,9);
claim_amount_i = atoi(claim_amount_s);
}
move(LINES/2-4,COLS/2-8);
printw("Enter Claim Reason: ");
mvchgat(LINES/2-4,COLS/2-8,-1,A_BOLD,1,NULL);
refresh();
move(LINES/2-4,COLS/2+13);
getnstr(claim_reason_s,29);
attron(A_BOLD);
// Settle the claim
settle_claim(policy_num_i, claim_amount_i, claim_reason_s);
//endwin();
attroff(A_BOLD);
return;
}
// What does the settle_claim function do?
void settle_claim(int policy_num_i, int claim_amount_i, char claim_reason_s[])
{
FILE *policy_data_fp, *policy_detail_fp, *policy_cust_nom_fp, *claim_fp, *claim_temp_fp;
FILE *scheme_years_fp;
int policy_exist_flag = 0; // To check if policy actually exists in file
int eof_flag = 0;
char cont_c;
// Define structures; these structures *should* be created in structs.h
policy_data_s claim_policy_data_s;
policy_detail_s claim_policy_detail_s;
policy_detail_s claim_policy_detail_tocheck_s;
policy_data_s claim_policy_data_tocheck_s;
policy_cust_nom_s claim_policy_cust_nom_s;
claim_s claim_new_s;
scheme_years_s claim_scheme_years_s;
// Open file with policy data to check if policy number exists
policy_data_fp = fopen("policy_data.dat","rb");
if (policy_data_fp == NULL)
{
printw("\nThe file policy_data.dat does not exist or cannot be accessed. Exiting...");
refresh();
printw("\nPress ENTER to continue...");
scanw("%c",&cont_c);
return;
}
// Checking if policy number is in file...
// At the end, also check for EOF (through fgetc()) to avoid infinite loop
do
{
if (fread(&claim_policy_data_s,1,sizeof(policy_data_s),policy_data_fp) == 0)
{
eof_flag = 1;
printw("\nSorry, that policy number does not exist.");
refresh();
printw("\nHave a nice day.");
refresh();
fcloseall();
printw("\n\nPress ENTER to continue...");
scanw("%c",&cont_c);
return;
}
} while (claim_policy_data_s.pol_no_i != policy_num_i);
// If we reach here, the record has been FOUND and is stored in the relevant structure
// Now open policy_detail.dat file
policy_detail_fp = fopen("policy_detail.dat","rb");
if (policy_detail_fp == NULL)
{
printw("\nSorry, your policy number exists, but the file policy_detail.dat does not exist or cannot be accessed. Exiting...");
refresh();
return;
}
scheme_years_fp = fopen("scheme_years.dat","rb");
if (scheme_years_fp == NULL)
{
printw("\nSorry, you policy number exists, but the file scheme_years.dat does not exist or cannot be accessed. Exiting...");
refresh();
return;
}
// The file is open. Now access details about the policy.
// And compare each record's policy id and scheme id with record from policy_data fi
do
{
if (fread(&claim_policy_detail_s,sizeof(policy_detail_s),1,policy_detail_fp) == 0)
{
printw("\nSorry, your policy number exists, but the policy id and scheme id stored are invalid. Exiting...");
refresh();
fcloseall();
return;
}
} while (claim_policy_detail_s.pol_id_i != claim_policy_data_s.pol_id_i);
do
{
if (fread(&claim_scheme_years_s,sizeof(scheme_years_s),1,scheme_years_fp) == 0)
{
printw("\nSorry, the scheme id cannot be found!");
refresh();
fcloseall();
return;
}
} while (claim_scheme_years_s.scheme_id_i != claim_policy_data_s.scheme_id_i);
if (claim_amount_i > claim_policy_detail_s.amt_covered_i)
{
printw("\nSorry! The amount claimed is greater than the amount covered under your policy and scheme!");
refresh();
return;
}
// Everything seems to be alright and the claim is valid
// Print the policy details
printw("\n\nCongratulations, the claim is valid and has been accepted!");
refresh();
printw("\nThe details are as below:");
refresh();
printw("\n\nPolicy number: %d",policy_num_i);
refresh();
printw("\nPolicy id: %d",claim_policy_data_s.pol_id_i);
refresh();
printw("\nScheme id: %d",claim_policy_data_s.scheme_id_i);
refresh();
printw("\nClaim Amount: %d",claim_amount_i);
refresh();
printw("\n\nThank you.");
refresh();
fclose(policy_detail_fp);
// We need to delete the customer record from the policy_data and policy_cust_nom files
// To accomplish this, we use our temporary file
policy_cust_nom_fp = fopen("policy_cust_nom.dat","rb");
if (policy_cust_nom_fp == NULL)
{
printw("\nThe policy_cust_nom does not exist or cannot be accessed. Exiting...");
refresh();
fcloseall();
return;
}
claim_temp_fp = fopen("claim_temp.dat","wb");
if (claim_temp_fp == NULL)
{
printw("\nThe claim temporary file claim_temp.dat cannot be opened for writing. Exiting...");
refresh();
fcloseall();
return;
}
eof_flag = 0;
do
{
if (fread(&claim_policy_cust_nom_s, sizeof(claim_policy_cust_nom_s),1,policy_cust_nom_fp) != 0)
{
if (claim_policy_cust_nom_s.pol_no_i != policy_num_i)
{
fwrite(&claim_policy_cust_nom_s,sizeof(policy_cust_nom_s),1,claim_temp_fp);
}
}
else eof_flag = 1;
} while (eof_flag == 0);
// Close both files, remove the original and rename the temporary as the original
fclose(claim_temp_fp);
fclose(policy_cust_nom_fp);
// NOTE: Platform dependent instructions ahead!
system("rm -f policy_cust_nom.dat");
system("mv claim_temp.dat policy_cust_nom.dat");
// Now for updating the policy_data.dat file
// The file is already open, but we will need to rewind
rewind(policy_data_fp);
// We use the _tocheck variable as a storage, since we will need the values ahead
claim_policy_data_tocheck_s = claim_policy_data_s;
claim_temp_fp = fopen("claim_temp.dat","wb");
if (claim_temp_fp == NULL)
{
printw("\nThe claim temporary file claim_temp.dat cannot be opened for writing. Exiting...");
refresh();
fcloseall();
return;
}
// Both files are open and ready for copying
eof_flag = 0;
do
{
if (fread(&claim_policy_data_s, sizeof(claim_policy_data_s),1,policy_data_fp) != 0)
{
if (claim_policy_data_s.pol_no_i != policy_num_i)
{
fwrite(&claim_policy_data_s,sizeof(policy_data_s),1,claim_temp_fp);
}
}
else eof_flag = 1;
} while (eof_flag == 0);
// Close both files, remove the original and rename the temporary as the original
fclose(claim_temp_fp);
fclose(policy_data_fp);
// NOTE: Platform dependent instructions ahead!
system("rm -f policy_data.dat");
system("mv claim_temp.dat policy_data.dat");
claim_fp = fopen("claim.dat","a");
if (claim_fp == NULL)
{
printw("\nThe claim report database cannot be opened! Exiting...");
refresh();
fcloseall();
return;
}
// Store the claim details in the claim_new_s structure for storage
claim_new_s.pol_no_i = policy_num_i;
claim_new_s.claim_amt_i = claim_amount_i;
strcpy(claim_new_s.claim_reason_ca,claim_reason_s);
// Store the structure; only one structure
fwrite(&claim_new_s,sizeof(claim_s),1,claim_fp);
// Close open files policy_data.dat and claim.dat
fclose(claim_fp);
fclose(policy_data_fp);
//printw("hello");
//refresh();
// Bye bye and thanks for calling, hope we will meet again...
refresh();
return;
}