• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cloudacademy / java-tdd-bitcoinconverter / #30

26 Jul 2023 06:37AM CUT coverage: 91.176%. Remained the same
#30

push

jeremycook123
added talisman pre-commit

31 of 34 relevant lines covered (91.18%)

0.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.18
/src/main/java/com/cloudacademy/bitcoin/ConverterSvc.java
1
package com.cloudacademy.bitcoin;
2

3
import java.io.IOException;
4
import java.text.ParseException;
5

6
import org.apache.http.client.methods.HttpGet;
7
import org.apache.http.impl.client.CloseableHttpClient;
8
import org.apache.http.client.methods.CloseableHttpResponse;
9
import org.apache.http.impl.client.HttpClients;
10

11
import com.google.gson.JsonObject;
12
import com.google.gson.JsonParser;
13

14
import java.text.NumberFormat;
15
import java.io.BufferedReader;
16
import java.io.InputStream;
17
import java.io.InputStreamReader;
18

19
public final class ConverterSvc {
20
    static final String BITCOIN_CURRENTPRICE_URL = "https://api.coindesk.com/v1/bpi/currentprice.json";
21
    private final HttpGet httpget = new HttpGet(BITCOIN_CURRENTPRICE_URL);
1✔
22

23
    private CloseableHttpClient httpclient;
24

25
    /*
26
    //curl -s https://api.coindesk.com/v1/bpi/currentprice.json | jq .
27
    //example data json response from coindesk api:
28
    {
29
        "time": {
30
            "updated": "Oct 15, 2020 22:55:00 UTC",
31
            "updatedISO": "2020-10-15T22:55:00+00:00",
32
            "updateduk": "Oct 15, 2020 at 23:55 BST"
33
        },
34
        "disclaimer": "This data was produced from the CoinDesk Bitcoin Price Index (USD)",
35
        "chartName": "Bitcoin",
36
        "bpi": {
37
            "USD": {
38
            "code": "USD",
39
            "symbol": "$",
40
            "rate": "11,486.5341",
41
            "description": "United States Dollar",
42
            "rate_float": 11486.5341
43
            },
44
            "GBP": {
45
            "code": "GBP",
46
            "symbol": "£",
47
            "rate": "8,900.8693",
48
            "description": "British Pound Sterling",
49
            "rate_float": 8900.8693
50
            },
51
            "EUR": {
52
            "code": "EUR",
53
            "symbol": "€",
54
            "rate": "9,809.3278",
55
            "description": "Euro",
56
            "rate_float": 9809.3278
57
            }
58
        }
59
    }
60
    */
61

62
    public ConverterSvc() {
×
63
        this.httpclient = HttpClients.createDefault();
×
64
    }
×
65

66
    public ConverterSvc(final CloseableHttpClient httpClient) {
1✔
67
        this.httpclient = httpClient;
1✔
68
    }
1✔
69

70
    public enum Currency {
1✔
71
        USD,
1✔
72
        GBP,
1✔
73
        EUR
1✔
74
    }
75

76
    public double getExchangeRate(final Currency currency) {
77
        double rate = 0;
1✔
78

79
        try (CloseableHttpResponse response = this.httpclient.execute(httpget)) {
1✔
80
            switch (response.getStatusLine().getStatusCode()) {
1✔
81
                case org.apache.http.HttpStatus.SC_OK:
82
                    InputStream inputStream = response.getEntity().getContent();
1✔
83
                    var json = new BufferedReader(new InputStreamReader(inputStream));
1✔
84

85
                    @SuppressWarnings("deprecation")
86
                    JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
1✔
87
                    String n = jsonObject.get("bpi").getAsJsonObject().get(currency.toString()).getAsJsonObject().get("rate").getAsString();
1✔
88
                    NumberFormat nf = NumberFormat.getInstance();
1✔
89
                    rate = nf.parse(n).doubleValue();
1✔
90

91
                    break;
1✔
92
                default:
93
                    rate = -1;
1✔
94
            }
95
        } catch (IOException | ParseException ex) {
1✔
96
            rate = -1;
1✔
97
        }
1✔
98

99
        return rate;
1✔
100
    }
101

102
    public double convertBitcoins(final Currency currency, final double coins) throws IllegalArgumentException {
103
        double dollars = 0;
1✔
104

105
        if (coins < 0) {
1✔
106
            throw new IllegalArgumentException("Number of coins must not be less than zero");
1✔
107
        }
108

109
        var exchangeRate = getExchangeRate(currency);
1✔
110

111
        if (exchangeRate >= 0) {
1✔
112
            dollars = exchangeRate * coins;
1✔
113
        } else {
114
            dollars = -1;
1✔
115
        }
116

117
        return dollars;
1✔
118
    }
119
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc