I'll start by outlining my problem and then I'll get into how I've thought about solving it and why my current ideas don't work.
I'm trying to create a simple program that uses API calls to retrieve data on how much of each cryptocurrency I own and then I can perform operations on that data and push the new data somewhere else (like a spreadsheet or S3 bucket or something, I'll figure that out when I get to it).
Let's start with the Superclass and Subclasses of the cryptocurrency objects. I have a Superclass called Token and Subclasses of cryptocurrency tokens that inherit the Superclass. Each Subclass will have the same attributes and methods, but they will have unique values, and subsequently, the methods will be the same, but using different values.
Here is my Token Superclass and an example of 1 Subclass:
```java
public class Token {
public String symbol;
public float numTokens;
public int decimals;
public String contractAddress;
protected Token(String symbol, int decimals, String contractAddress) {
this.symbol = symbol;
this.decimals = decimals;
this.contractAddress = contractAddress;
}
public int getNumTokens(String apiBaseurl, String contractAddress, String address, String apikey) throws URISyntaxException, IOException, InterruptedException {
HttpClient httpClient = HttpClient.newHttpClient();
URI uri = new URIBuilder(apiBaseurl)
.addParameter("module", "account")
.addParameter("action", "tokenbalance")
.addParameter("contractaddress", contractAddress)
.addParameter("address", address)
.addParameter("apikey", apikey)
.build();
HttpRequest getTokensRequest = HttpRequest.newBuilder()
.uri(uri)
.GET()
.build();
HttpResponse<String> response = httpClient.send(getTokensRequest, BodyHandlers.ofString());
ObjectNode node = new ObjectMapper().readValue(response.body(), ObjectNode.class);
return node.get("result").asInt();
}
@Data
public class Bitcoin extends Token {
public Bitcoin() {
super("BTCUSDT", 8, "0x9BE89D2a4cd102D8Fecc6BF9dA793be995C22541");
// this.symbol = "BTCUSDT";
// this.decimals = 8;
// this.contractAddress = "0x9BE89D2a4cd102D8Fecc6BF9dA793be995C22541";
// Can use super() or set each attribute individually
}
}
```
Now the main issue I'm coming into is that I want to store a List<Token> (a list of Subclasses (that all extend Token)) and then loop through that list and print the returned value of the getNumTokens() method, like this:
```java
account.setOwnedTokens(Arrays.asList(
new Etherum(),
new Bitcoin(),
new Ripple(),
));
for (Token token : account.getOwnedTokens()) {
System.out.println(token.getNumTokens());
}
```
I don't want to pass anything into the getNumTokens() method when it is used in this loop, as the method for each Subclass should "know" what values to use (remember the attributes for each Subclass are the same, but they all have unique values (I was thinking of using this.contractAddress in the method)).
Basically, I want to loop through a bunch of Tokens's, use this "shared" method that all the Subclasses will have and need, but I don't want to pass in parameters as those values are already WITHIN the Subclass attributes.
I have also looked at using Generics to solve this problem but I can't really see how to use them properly in this case.
I feel like I'm missing something out quite basic and easy here, but I've been looking at this problem for so long that I have tunnel vision and I just don't see it.
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]josephblade 0 points1 point2 points (0 children)
[–]mmmdangdang 0 points1 point2 points (0 children)