Improved and cleaned up examples

This commit is contained in:
Erik C. Thauvin 2022-09-08 11:53:24 -07:00
parent bef4b2ee20
commit cdba6b7597
3 changed files with 21 additions and 8 deletions

View file

@ -26,5 +26,10 @@
<option name="name" value="MavenLocal" /> <option name="name" value="MavenLocal" />
<option name="url" value="file:$MAVEN_REPOSITORY$/" /> <option name="url" value="file:$MAVEN_REPOSITORY$/" />
</remote-repository> </remote-repository>
<remote-repository>
<option name="id" value="MavenLocal" />
<option name="name" value="MavenLocal" />
<option name="url" value="file:$PROJECT_DIR$/../../maven/repository/" />
</remote-repository>
</component> </component>
</project> </project>

View file

@ -6,20 +6,22 @@ import org.json.JSONObject;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.Currency;
import java.util.List; import java.util.List;
import java.util.Locale;
public class CryptoPriceSample { public class CryptoPriceSample {
public static void main(final String[] args) { public static void main(final String[] args) {
try { try {
if (args.length > 0) { if (args.length > 0) {
final CryptoPrice price; final String currency;
if (args.length == 2) { if (args.length == 2) {
price = CryptoPrice.spotPrice(args[0], args[1]); currency = args[1];
} else { } else {
price = CryptoPrice.spotPrice(args[0]); currency = Currency.getInstance(Locale.getDefault()).getCurrencyCode();
} }
System.out.println("The current " + price.getBase() + " price is " + price.getAmount() + " in " final var price = CryptoPrice.spotPrice(args[0], currency);
+ price.getCurrency()); System.out.println("The current " + price.getBase() + " price is " + price.toCurrency());
} else { } else {
// Get current Bitcoin spot price. // Get current Bitcoin spot price.
final var price = CryptoPrice.spotPrice("BTC"); final var price = CryptoPrice.spotPrice("BTC");
@ -42,11 +44,13 @@ public class CryptoPriceSample {
final var response = CryptoPrice.apiCall(List.of("exchange-rates"), final var response = CryptoPrice.apiCall(List.of("exchange-rates"),
Collections.singletonMap("currency", "USD")); Collections.singletonMap("currency", "USD"));
final var rates = new JSONObject(response).getJSONObject("data").getJSONObject("rates"); final var rates = new JSONObject(response).getJSONObject("data").getJSONObject("rates");
System.out.printf("The USD-EUR exchange rate is: %s%n", rates.getString("EUR")); System.out.println("The USD-EUR exchange rate is: " + rates.getString("EUR"));
} }
} catch (CryptoException e) { } catch (CryptoException e) {
System.err.println("HTTP Status Code: " + e.getStatusCode()); System.err.println("HTTP Status Code: " + e.getStatusCode());
System.err.println(e.getMessage()); System.err.println(e.getMessage());
} catch (IllegalArgumentException e) {
System.err.println("Could not display the specified currency: " + args[1]);
} catch (IOException e) { } catch (IOException e) {
System.err.println(e.getMessage()); System.err.println(e.getMessage());
} }

View file

@ -7,12 +7,14 @@ import net.thauvin.erik.crypto.CryptoPrice.Companion.sellPrice
import net.thauvin.erik.crypto.CryptoPrice.Companion.spotPrice import net.thauvin.erik.crypto.CryptoPrice.Companion.spotPrice
import org.json.JSONObject import org.json.JSONObject
import java.io.IOException import java.io.IOException
import java.util.*
fun main(args: Array<String>) { fun main(args: Array<String>) {
try { try {
if (args.isNotEmpty()) { if (args.isNotEmpty()) {
val price = if (args.size == 2) spotPrice(args[0], args[1]) else spotPrice(args[0]) val currency = if (args.size == 2) args[1] else Currency.getInstance(Locale.getDefault()).currencyCode
println("The current ${price.base} price is ${price.amount} in ${price.currency}") val price = spotPrice(args[0], currency)
println("The current ${price.base} price is ${price.toCurrency()}")
} else { } else {
// Get current Bitcoin spot price. // Get current Bitcoin spot price.
val price = spotPrice("BTC") val price = spotPrice("BTC")
@ -39,6 +41,8 @@ fun main(args: Array<String>) {
} catch (e: CryptoException) { } catch (e: CryptoException) {
System.err.println("HTTP Status Code: ${e.statusCode}") System.err.println("HTTP Status Code: ${e.statusCode}")
System.err.println(e.message) System.err.println(e.message)
} catch (e: IllegalArgumentException) {
System.err.println("Could not display the specified currency: ${args[1]}")
} catch (e: IOException) { } catch (e: IOException) {
System.err.println(e.message) System.err.println(e.message)
} }