/*** Program Name: LOANS.C Author: Edward Houston Program to produce quick reference for loan statistics for the loan and repayment period in years. Designed and created in microsoft visual studio 2005. ***/ /*** PREPROCESSOR DIRECTIVES ***/ /*** HEADER FILES ***/ #include "loans.h" /* custom header file containing some functions used here */ #include /* Standard Input/Output Header File */ #include /* Console Input/Output Header File */ #include /* Standard Library */ #include /* String Handling */ /*** NAMED CONSTANTS ***/ #define FIVE 5000 /* Five thousand */ #define TEN 10000 /* Ten Thousand */ #define FIFTEEN 15000 /* Fifteen Thousand */ #define SIZE 20 /* length of filename */ /*** USER-DEFINED AND ENUMERATED DATA TYPES ***/ /* None Used */ /*** FUNCTION PROTOTYPES ***/ void DisplayMenu( void ); char process_menu_choice(char a); void Estimate_Loan( void ); void Calculate_Interest_Group( float a ); float Calculate_Interest( float a ); float Calculate_Total( float a, float b ); float Calculate_Monthly_Payment( void ); void Output_Information( void ); void Repayment_Information( void ); void Loan_Stats_To_File( void ); /*** GLOBAL VARIABLES ***/ /* Interest Rates */ float Upto5k; float Above5upto10k; float Above10upto15k; float Above15k; float InterestGroup; /* other */ float Interest; float Amount; float Total_Repayable; float Monthly; float Monthly_Interest; int Years = 0, i = 0; /*** MAIN FUNCTION ***/ void main( void ) { char Choice = '\0'; do { textcolor(WHITE, BLUE); DisplayMenu(); Choice = process_menu_choice(toupper(_getch())); } while (toupper(Choice) != 'Q'); printf("\nClosing Down.."); _getch(); } /*** OTHER FUNCTONS ***/ void DisplayMenu( void ) { clrscr(); textcolor(WHITE, BLUE); printf("---\t\t\t \t\t\t---\n"); printf("-- \t\t\t"); textcolor(RED,BLUE); /* red on black */ printf("Menu"); textcolor(WHITE,BLUE); /* white on black */ printf("\t\t\t --\n"); printf("- 1. Estimate Loan\t\t\t \t-\n"); printf("- 2. Output Loan Statistics\t\t \t-\n"); printf("- 3. Display Repayment Information in Table \t-\n"); printf("- 4. Write Loan Statistics to file\t\t-\n"); printf("- Q. Exit \t\t\t\t \t-\n"); printf("-- \t\t\t \t\t\t --\n"); printf("---\t\t\t \t\t\t---\n"); printf("\nEnter your choice:\n"); } char process_menu_choice(char a) { switch(a) { case '1': /* Estimate Loan */ Estimate_Loan(); break; case '2': /* Output Loan Information */ Output_Information(); break; case '3': /* Display Repayment Information */ Repayment_Information(); break; case '4': /* Save Loan Statistics to a file */ Loan_Stats_To_File(); break; default: /* not a valid menu choice */ break; } /* return the selected menu item (to determine whether to quit) */ return a; } void Estimate_Loan( void ) { /* initialise float variables */ Upto5k = 16.5; Above5upto10k = 12.5; Above10upto15k = 8.5; Above15k = 6.5; InterestGroup = 0.0; Monthly_Interest = 0.0; clrscr(); printf("Enter Amount to be borrowed\n:œ"); scanf_s(" %f", &Amount); printf("Enter repayment period\n:"); scanf_s(" %d", &Years); Calculate_Interest_Group( Amount ); Interest = Calculate_Interest( Amount ); Total_Repayable = Calculate_Total( Interest, Amount ); Monthly = Calculate_Monthly_Payment(); /* Display Output automatically then drop back to the menu */ Output_Information(); return; } void Calculate_Interest_Group( float a ) { if( a <= FIVE ) { InterestGroup = Upto5k; } else if( a > FIVE && a <= TEN ) { InterestGroup = Above5upto10k; } else if( a > TEN && a <= FIFTEEN ) { InterestGroup = Above10upto15k; } else if( a > FIFTEEN ) { InterestGroup = Above15k; } return; } float Calculate_Interest( float a ) { /* Calculate the interest */ Interest = ( (a * InterestGroup) / 100 ); return Interest; } float Calculate_Total( float a, float b ) { /* a = interest b = amount */ float Total; Total = (a + b); return Total; } float Calculate_Monthly_Payment( void ) { int a; /* To Hold Months */ float b; /* To Hold Monthly Repayable */ float Repayable; a = (Years * 12); b = Total_Repayable; Repayable = ( b / a ); return Repayable; } void Output_Information( void ) { clrscr(); printf("The Loan Amount is: œ%.2f\n", Amount); printf("The Loan Period in years is: %i\n", Years); printf("The Loan Period in months is: %i\n", (Years * 12)); printf("The monthly repayment cost is: œ%.2f\n", Monthly); printf("The total amount repayable is: œ%.2f\n", Total_Repayable); printf("The rate of interest is: %.2f%%\n", InterestGroup); _getch(); } void Repayment_Information( void ) { int TempMonths; clrscr(); textcolor(GREEN,RED); for(i = 0; i <= 4; i++) { printf("---------------"); } textcolor(YELLOW,RED); printf("\nTime Elapsed\t"); printf("Interest\t"); printf("\tTotal"); printf("\t\t\tAmount"); /* make the whole box one colour */ textcolor(RED,RED); printf("#####\n"); textcolor(YELLOW,RED); /* for the titles */ printf("(Months)\t"); printf("paid\t\t"); printf("\tpaid"); printf("\t\t\tOutstanding\n"); textcolor(GREEN,RED); for(i = 0; i <= 4; i++) { printf("---------------"); } printf("\n"); textcolor(WHITE,BLUE); TempMonths = (Years * 12); for( i = 0; i <= TempMonths; i += 3 ) { printf("%i\t\t",i ); /*Time elapsed every 3 months */ printf("œ%7.2f\t\t", (i * Monthly * InterestGroup / 100)); /*Interest paid */ printf("œ%7.2f\t\t", (Monthly * i)); /* Total paid */ printf("œ%7.2f\n", (Total_Repayable - (Monthly * i))); /*Amount Outstanding */ } gotoxy(0,0); _getch(); } void Loan_Stats_To_File( void ) { // Summarised statistics to a file: FILE *fp; char FileName[SIZE]; clrscr(); fflush(stdin); printf("Enter a filename to write to:\n"); gets_s(FileName, SIZE); /* Open file for write */ fopen_s( &fp, FileName, "w+" ); fprintf(fp, "The Loan Amount is: œ%.2f\n", Amount); fprintf(fp,"The Loan Period in years is: %i\n", Years); fprintf(fp,"The Loan Period in months is: %i\n", (Years * 12)); fprintf(fp,"The monthly repayment cost is: œ%.2f\n", Monthly); fprintf(fp,"The total amount repayable is: œ%.2f\n", Total_Repayable); fprintf(fp,"The rate of interest is: %.2f%%\n", InterestGroup); /* close open files */ _fcloseall(); _getch(); } /*** Program Name: LOANS.H Author: MY BAWS Header file including certain windows function used in the program. ***/ #include // constants: #define BLACK 0 #define BLUE 1 #define GREEN 2 #define CYAN 3 #define RED 4 #define MAGENTA 5 #define BROWN 6 #define LIGHTGREY 7 #define DARKGREY 8 #define LIGHTBLUE 9 #define LIGHTGREEN 10 #define LIGHTCYAN 11 #define LIGHTRED 12 #define LIGHTMAGENTA 13 #define YELLOW 14 #define WHITE 15 #define BLINK 128 void clrscr(void) { COORD coordScreen = { 0, 0 }; /* here's where we'll home the cursor */ BOOL bSuccess; DWORD cCharsWritten; CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */ DWORD dwConSize; /* number of character cells in the current buffer */ HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); /* get the number of character cells in the current buffer */ bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi ); dwConSize = csbi.dwSize.X * csbi.dwSize.Y; /* fill the entire screen with blanks */ bSuccess = FillConsoleOutputCharacter( hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten ); /* get the current text attribute */ bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi ); /* now set the buffer's attributes accordingly */ bSuccess = FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten ); /* put the cursor at (0, 0) */ bSuccess = SetConsoleCursorPosition( hConsole, coordScreen ); } void gotoxy(int x, int y) { HANDLE hConsoleOutput; COORD dwCursorPosition; dwCursorPosition.X = x; dwCursorPosition.Y = y; hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition); } void textcolor(int fontcolor, int backgroundcolor) { HANDLE screen; int color_attribute; color_attribute = backgroundcolor; color_attribute = _rotl(color_attribute,4) | fontcolor; screen = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(screen, (WORD)color_attribute); }