You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.3 KiB

  1. #include "interestCalculator.h"
  2. void troubleshoot(int errorCode){
  3. printf("Error! The requested operation was terminated because of an issue. Here are some details about the error:\n---------------\n");
  4. switch(errorCode){
  5. case 0:
  6. printf("Principal amount not valid. Make sure it is a valid number over the value of zero.");
  7. break;
  8. case 1:
  9. printf("Interest rate not valid. Make sure it is a valid number over the value of zero.");
  10. break;
  11. case 2:
  12. printf("Duration not valid. Make sure it is a valid number over the value of zero.");
  13. break;
  14. case 3:
  15. printf("Invalid option. Aborting.");
  16. break;
  17. case 4:
  18. printf("Your goal cannot be smaller than your principal funds. Aborting.");
  19. break;
  20. }
  21. }
  22. void askForSavingGoal(float principalAmount, float accInterestPerYear) {
  23. char c;
  24. float goal;
  25. float timeForGoal;
  26. printf("\nWould you like to set a saving goal? [y/n]: ");
  27. scanf(" %c", &c);
  28. if (c != 'y' && c != 'Y') {
  29. return;
  30. }
  31. printf("\nPlease enter your goal amount in €: ");
  32. scanf("%f", &goal);
  33. if (goal < principalAmount) {
  34. troubleshoot(4);
  35. return;
  36. }
  37. timeForGoal = (goal - principalAmount) / accInterestPerYear;
  38. printf("\nIn %.1f years you will reach your goal of %.2f.\n", timeForGoal, goal);
  39. }
  40. float initiateInterest(float principalAmount, float interest, float time){
  41. return principalAmount*(1+(interest*time));
  42. }
  43. void calculateYearlyInterest(){
  44. float principalAmount;
  45. float interestPerYear;
  46. float timeInYears;
  47. int choice;
  48. printf("Please enter the principal amount:");
  49. if (scanf("%f", &principalAmount) != 1 || principalAmount <= 0) {
  50. troubleshoot(0);
  51. return;
  52. }
  53. printf("\nPlease enter interest per year (percentage):");
  54. if (scanf("%f", &interestPerYear) != 1 || interestPerYear <= 0) {
  55. troubleshoot(0);
  56. return;
  57. }
  58. printf("\nWould you like to enter the time in [1]months or in [2]years?\n");
  59. scanf("%d",&choice);
  60. if(choice==1){
  61. printf("\nPlease enter interest time in months:");
  62. if (scanf("%f", &timeInYears) != 1 || timeInYears <= 0) {
  63. troubleshoot(2);
  64. return;
  65. }
  66. timeInYears=timeInYears/12;
  67. }else if(choice==2){
  68. printf("\nPlease enter interest time in years:");
  69. if (scanf("%f", &timeInYears) != 1 || timeInYears <= 0) {
  70. troubleshoot(2);
  71. return;
  72. }
  73. }else{
  74. troubleshoot(2);
  75. }
  76. float interestDecimal=interestPerYear/100;
  77. float result= initiateInterest(principalAmount,interestDecimal,timeInYears);
  78. printf("\nAmount with the interest is %.2f€.",result);
  79. askForSavingGoal(principalAmount,principalAmount*interestDecimal);
  80. }
  81. void calculateMonthlyInterest(){
  82. float principalAmount;
  83. float interestPerMonth;
  84. float timeInMonths;
  85. printf("Please enter the principal amount:");
  86. if (scanf("%f", &principalAmount) != 1 || principalAmount <= 0) {
  87. troubleshoot(0);
  88. return;
  89. }
  90. printf("\nPlease enter interest per month (percentage):");
  91. if (scanf("%f", &interestPerMonth) != 1 || interestPerMonth <= 0) {
  92. troubleshoot(0);
  93. return;
  94. }
  95. printf("\nPlease enter interest time in months:");
  96. if (scanf("%f", &timeInMonths) != 1 || timeInMonths <= 0) {
  97. troubleshoot(2);
  98. return;
  99. }
  100. float interestDecimal=interestPerMonth/100;
  101. float result= initiateInterest(principalAmount,interestDecimal,timeInMonths);
  102. printf("\nAmount with the interest is %.2f€.",result);
  103. }
  104. void initiateCalculator() {
  105. int input;
  106. char c;
  107. printf("Welcome to the interest calculator. Please select an option:\n"
  108. "[1] Calculate yearly interest\n"
  109. "[2] Calculate monthly interest\n");
  110. scanf("%d", &input);
  111. switch (input) {
  112. case 1:
  113. calculateYearlyInterest();
  114. break;
  115. case 2:
  116. calculateMonthlyInterest();
  117. break;
  118. default:
  119. break;
  120. }
  121. printf("\nThank you for using our services. Would you like to do another calculation? [y/n]: ");
  122. scanf(" %c", &c);
  123. if (c == 'y' || c == 'Y') {
  124. initiateCalculator();
  125. }
  126. }
  127. // int main(){
  128. // initiateCalculator();
  129. // }