|
|
#include "interestCalculator.h"
void troubleshoot(int errorCode){ printf("Error! The requested operation was terminated because of an issue. Here are some details about the error:\n---------------\n");
switch(errorCode){ case 0: printf("Principal amount not valid. Make sure it is a valid number over the value of zero."); break; case 1: printf("Interest rate not valid. Make sure it is a valid number over the value of zero."); break; case 2: printf("Duration not valid. Make sure it is a valid number over the value of zero."); break; case 3: printf("Invalid option. Aborting."); break; case 4: printf("Your goal cannot be smaller than your principal funds. Aborting."); break; } }
void askForSavingGoal(float principalAmount, float accInterestPerYear) { char c; float goal; float timeForGoal;
printf("\nWould you like to set a saving goal? [y/n]: "); scanf(" %c", &c);
if (c != 'y' && c != 'Y') { return; }
printf("\nPlease enter your goal amount in €: "); scanf("%f", &goal);
if (goal < principalAmount) { troubleshoot(4); return; }
timeForGoal = (goal - principalAmount) / accInterestPerYear; printf("\nIn %.1f years you will reach your goal of %.2f.\n", timeForGoal, goal); }
float initiateInterest(float principalAmount, float interest, float time){ return principalAmount*(1+(interest*time)); }
void calculateYearlyInterest(){ float principalAmount; float interestPerYear; float timeInYears; int choice;
printf("Please enter the principal amount:"); if (scanf("%f", &principalAmount) != 1 || principalAmount <= 0) { troubleshoot(0); return; }
printf("\nPlease enter interest per year (percentage):"); if (scanf("%f", &interestPerYear) != 1 || interestPerYear <= 0) { troubleshoot(0); return; } printf("\nWould you like to enter the time in [1]months or in [2]years?\n"); scanf("%d",&choice); if(choice==1){ printf("\nPlease enter interest time in months:"); if (scanf("%f", &timeInYears) != 1 || timeInYears <= 0) { troubleshoot(2); return; } timeInYears=timeInYears/12;
}else if(choice==2){ printf("\nPlease enter interest time in years:"); if (scanf("%f", &timeInYears) != 1 || timeInYears <= 0) { troubleshoot(2); return; } }else{ troubleshoot(2); }
float interestDecimal=interestPerYear/100;
float result= initiateInterest(principalAmount,interestDecimal,timeInYears); printf("\nAmount with the interest is %.2f€.",result); askForSavingGoal(principalAmount,principalAmount*interestDecimal); } void calculateMonthlyInterest(){ float principalAmount; float interestPerMonth; float timeInMonths;
printf("Please enter the principal amount:"); if (scanf("%f", &principalAmount) != 1 || principalAmount <= 0) { troubleshoot(0); return; }
printf("\nPlease enter interest per month (percentage):"); if (scanf("%f", &interestPerMonth) != 1 || interestPerMonth <= 0) { troubleshoot(0); return; }
printf("\nPlease enter interest time in months:"); if (scanf("%f", &timeInMonths) != 1 || timeInMonths <= 0) { troubleshoot(2); return; }
float interestDecimal=interestPerMonth/100;
float result= initiateInterest(principalAmount,interestDecimal,timeInMonths); printf("\nAmount with the interest is %.2f€.",result); }
void initiateCalculator() { int input; char c;
printf("Welcome to the interest calculator. Please select an option:\n" "[1] Calculate yearly interest\n" "[2] Calculate monthly interest\n"); scanf("%d", &input);
switch (input) { case 1: calculateYearlyInterest(); break; case 2: calculateMonthlyInterest(); break; default: break; }
printf("\nThank you for using our services. Would you like to do another calculation? [y/n]: "); scanf(" %c", &c);
if (c == 'y' || c == 'Y') { initiateCalculator(); } }
// int main(){
// initiateCalculator();
// }
|