From 28b1b195cb137338fa27129a719c7e8ae7ab028f Mon Sep 17 00:00:00 2001 From: fdlt3817 Date: Fri, 10 Feb 2023 15:41:34 +0100 Subject: [PATCH] Refactoring: optimize saving request --- src/interestCalculator.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/interestCalculator.c b/src/interestCalculator.c index b2e5462..a458947 100644 --- a/src/interestCalculator.c +++ b/src/interestCalculator.c @@ -23,29 +23,31 @@ void troubleshoot(int errorCode){ } } -void askForSavingGoal(float principalAmount, float accInterestPerYear){ +void askForSavingGoal(float principalAmount, float accInterestPerYear) { char c; float goal; float timeForGoal; - printf("\nWould you like to set a saving goal? [y] yes [any] no"); - scanf(" %c",&c); - - if(c=='y'||c=='Y'){ - printf("\nPlease enter your goal amount in €:"); - scanf("%f",&goal); - if(goal>=principalAmount){ - timeForGoal=(goal-principalAmount)/accInterestPerYear; - printf("\nIn %.1f years you will reach your goal of %f.\n", timeForGoal, goal); - }else{ - troubleshoot(4); - return; - } - }else{ + + 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)); }