Adding comments to currency

This commit is contained in:
Alex Epstein 2017-06-27 11:42:17 -04:00
parent a84d83dd8b
commit 4efbfd7ec5
1 changed files with 12 additions and 5 deletions

View File

@ -3,6 +3,8 @@
base="" base=""
exchangeTo="" exchangeTo=""
## Grabs the base currency from the user and validates it with all the possible currency
## types available on the API
getBase() getBase()
{ {
echo -n "What is the base currency: " echo -n "What is the base currency: "
@ -21,6 +23,8 @@ getBase()
fi fi
} }
## Grabs the exchange to currency from the user and validates it with all the possible currency
## types available on the API
getExchangeTo() getExchangeTo()
{ {
echo -n "What currency to exchange to: " echo -n "What currency to exchange to: "
@ -39,6 +43,7 @@ getExchangeTo()
fi fi
} }
## Get the amount that will be exchanged and validate that the user has entered a number (decimals are allowed)
getAmount() getAmount()
{ {
echo -n "What is the amount being exchanged: " echo -n "What is the amount being exchanged: "
@ -62,14 +67,16 @@ checkInternet()
fi fi
} }
## Grabs the exchange rate and does the math for converting the currency
convertCurrency() convertCurrency()
{ {
exchangeRate=$(curl -s "http://api.fixer.io/latest?base=$base&symbols=$exchangeTo" | grep -Eo "[0-9][.][0-9]*") > /dev/null exchangeRate=$(curl -s "http://api.fixer.io/latest?base=$base&symbols=$exchangeTo" | grep -Eo "[0-9][.][0-9]*") > /dev/null
exchangeAmount=$(echo "$exchangeRate * $amount" | bc ) exchangeAmount=$(echo "$exchangeRate * $amount" | bc )
echo "$amount $base is equal to $exchangeAmount $exchangeTo" echo "$amount $base is equal to $exchangeAmount $exchangeTo"
} }
checkInternet || exit 1
getBase checkInternet || exit 1 # check if we have a valid internet connection if this isnt true the rest of the script will not work so stop here
getExchangeTo getBase # get base currency
getAmount getExchangeTo # get exchange to currency
convertCurrency getAmount # get the amount to be converted
convertCurrency # grab the exhange rate and perform the conversion