-
Notifications
You must be signed in to change notification settings - Fork 0
/
currency_converter.go
99 lines (77 loc) · 2.39 KB
/
currency_converter.go
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
99
package main
import (
"encoding/json"
"fmt"
"math"
"net/http"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/wzshiming/ctc"
)
// fetches the latest exchange rate from an API and performs the currency conversion.
func CurrencyConvert(repeat bool) {
var amount float64
from := ""
to := ""
type CurrencyResponse struct {
Amount float64 `json:"amount"`
Base string `json:"base"`
Date string `json:"date"`
Rates map[string]float64 `json:"rates"`
}
promptAmount := &survey.Input{
Message: "select the amount:",
Help: "amaout of money to convert, currency nonspecific",
}
if err := survey.AskOne(promptAmount, &amount, survey.WithValidator(survey.Required)); err != nil {
panic(err)
}
promptFrom := &survey.Input{
Message: "convert from:",
Help: "use an international currency code like 'USD', if it doesn't work it means the api i'm using doesn't support this currency",
}
if err := survey.AskOne(promptFrom, &from, survey.WithValidator(survey.Required)); err != nil {
panic(err)
}
promptTo := &survey.Input{
Message: "convert to:",
Help: "use an international currency code like 'USD', if it doesn't work it means the api i'm using doesn't support this currency",
}
if err := survey.AskOne(promptTo, &to, survey.WithValidator(survey.Required)); err != nil {
panic(err)
}
client := http.Client{}
from = strings.ToUpper(from)
to = strings.ToUpper(to)
url := fmt.Sprintf("https://www.frankfurter.app/latest?from=%v&to=%v", from, to)
request, err := http.NewRequest("GET", url, nil)
if err != nil {
panic(err)
}
resp, err := client.Do(request)
if err != nil {
panic(err)
}
var response CurrencyResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
panic(err)
}
fmt.Println("<----------CONVERSION---------->")
fmt.Println(ctc.ForegroundBright, "current exchange from", from, "to", to, ctc.Reset)
rate, ok := response.Rates[to]
if !ok {
// Try converting 'to' to uppercase in case of mismatch
to = strings.ToUpper(to)
rate, ok = response.Rates[to]
}
if !ok {
fmt.Println("Exchange rate not available for the selected currency pair")
} else {
exchangedAmount := math.Round((amount*rate)*100) / 100
fmt.Println(ctc.ForegroundBrightGreen, amount, from, "is", exchangedAmount, to, ctc.Reset)
}
fmt.Println("<------------------------------>")
if repeat {
main()
}
}