-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPowerCode_CompoundInterest_WithMonthlyDeposits.cs
73 lines (65 loc) · 2.66 KB
/
PowerCode_CompoundInterest_WithMonthlyDeposits.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Text;
namespace PowerCode_DepositInterest
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string inicialDeposit = req.Query["inicialDeposit"];
string depositContribution = req.Query["depositContribution"];
string contributions = req.Query["contributions"];
string rate = req.Query["rate"];
string years = req.Query["years"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
inicialDeposit = inicialDeposit ?? data?.inicialDeposit;
depositContribution = depositContribution ?? data?.depositContribution;
contributions = contributions ?? data?.contributions;
rate = rate ?? data?.rate;
years = years ?? data?.years;
float ys = float.Parse(years);
float id = float.Parse(inicialDeposit);
float dc = float.Parse(depositContribution);
float rt = float.Parse(rate);
float ct = float.Parse(contributions);
float td = 0;
float ti = 0;
for (int y=1;y<=ys; y++)
{
if (y < 2)
{
td = id + ct * dc;
ti = rt / 100f * id;
}
else
{
td = td + ct * dc;
ti = ti+(rt / 100f * id);
}
id += (rt / 100f * id)+(ct * dc);
}
StringBuilder sb = new StringBuilder();
sb.Append("Total after "+ years + " : " + id);
sb.Append("| Total deposit : " + td);
sb.Append("| Total interest : " + ti);
string s = sb.ToString();
string responseMessage = string.IsNullOrEmpty(s.ToString())
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: s.ToString();
return new OkObjectResult(responseMessage);
}
}
}