-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
98 lines (76 loc) · 2.43 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// Currency Converter
//
// Created by deathlezz on 8/08/2021.
//
// https://api.frankfurter.app/latest?from=USD
import Foundation
// available currencies array
let currency = ["USD", "EUR", "JPY", "GBP"]
func convert(base: String, to: String, amount: Float) {
// specify how the json file looks like
struct Json: Codable {
let base: String
let rates: [String: Float]
}
// create url
if let url = URL(string: "https://api.frankfurter.app/latest?from=\(base)") {
do {
// make http GET call
let contents = try String(contentsOf: url)
// specify decoding format
let data = contents.data(using: .utf8)
// decode json data
let ratesData = try JSONDecoder().decode(Json.self, from: data!)
if base != to {
// multiply amount by rate
let multiply = amount * ratesData.rates["\(to)"]!
// output
print("\(amount) \(base) = \(multiply) \(to)")
}
} catch {
print("* Connection error *")
}
} else {
print("* Invalid URL *")
}
}
print("* Welcome to Currency Converter *")
func enterBase() {
print()
print("Enter base currency: [USD, EUR, JPY, GBP]")
let base = readLine()!.uppercased()
if base == "USD" || base == "EUR" || base == "JPY" || base == "GBP" {
func enterAmount() {
print()
print("Enter amount:")
if let number = Float(readLine()!) {
if number > 0 {
print()
// call the function
for name in currency {
convert(base: base!, to: name, amount: number)
}
return enterBase()
} else {
print()
print("* Enter value bigger than 0 *")
return enterAmount()
}
} else {
print()
print("* Enter numbers only *")
return enterAmount()
}
}
enterAmount()
} else {
print()
print("* Enter USD, EUR, JPY or GBP *")
return enterBase()
}
}
enterBase()
// 5.0 GBP = 6.9585 USD
// 5.0 GBP = 5.8935 EUR
// 5.0 GBP = 764.05 JPY