-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
69 lines (58 loc) · 1.84 KB
/
main.swift
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
import Foundation
var rate: Double
var cashFlows: [Double] = []
print("Enter the discount rate: ", terminator: "")
if let input: String = readLine() {
if let rateInput: Double = Double(input) {
rate = rateInput
} else {
print("Invalid input")
exit(0)
}
} else {
print("Invalid input")
exit(0)
}
print("\nEnter cash flow for the Investment (Enter x to stop): ")
while true {
if let input: String = readLine() {
if input == "x" {
break
} else {
if let cashFlow: Double = Double(input) {
cashFlows.append(cashFlow)
} else {
print("Invalid input")
}
}
}
}
func roundtoDecimalThree(_ value: Double) -> Double {
return (value * 1000).rounded() / 1000
}
struct InvestmentData {
var year: Int
var investment: Double
var disFactor: Double
var pv: Double
}
var investmentData: [InvestmentData] = []
var npv: Double = 0
for i: Int in 0...cashFlows.count-1 {
let disFactor: Double = roundtoDecimalThree(1 / pow(1 + rate, Double(i)))
let pv: Double = cashFlows[i] * disFactor
investmentData.append(InvestmentData(year: i, investment: cashFlows[i], disFactor: disFactor, pv: pv))
npv += pv
}
func generateMarkdownTable(from data: [InvestmentData]) -> String {
let headers: String = "| Year | Investment | Discount Factor | Present Value |"
let separator: String = "| --- | --- | --- | --- |"
let rows: String = data.map { entry -> String in
return "| \(entry.year) | \(entry.investment) | \(entry.disFactor) | \(entry.pv) |"
}.joined(separator: "\n")
return [headers, separator, rows].joined(separator: "\n")
}
let markdownTable: String = generateMarkdownTable(from: investmentData)
print("\n")
print(markdownTable)
print("| **NPV:** | --- | --- | \(npv) |")