Skip to content

Commit

Permalink
Merge pull request #201 from Guaranisgk/patch-1
Browse files Browse the repository at this point in the history
feat(ProfitCalculator): making the code more concise
  • Loading branch information
May2Beez authored Mar 2, 2024
2 parents de0af6f + 2a66cd0 commit fa417db
Showing 1 changed file with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -464,21 +464,22 @@ private void getPrices(JsonObject json1, List<BazaarItem> cropsToCount) {
getPricesPerList(json1, cropsToCount);
}

private void getPricesPerList(JsonObject json1, List<BazaarItem> list) {
for (BazaarItem item : list) {
JsonObject json2 = json1.getAsJsonObject(item.bazaarId);
JsonArray json3 = json2.getAsJsonArray("sell_summary");
JsonObject json4 = json3.size() > 1 ? json3.get(1).getAsJsonObject() : json3.get(0).getAsJsonObject();

double buyPrice = json4.get("pricePerUnit").getAsDouble();
APICrop apiCrop;
if (bazaarPrices.containsKey(item.localizedName)) {
apiCrop = bazaarPrices.get(item.localizedName);
apiCrop.currentPrice = buyPrice;
} else {
apiCrop = new APICrop(item.localizedName, buyPrice);
private void getPricesPerList(JsonObject bazaarData, List<BazaarItem> itemList) {
for (BazaarItem item : itemList) {
JsonObject itemData = bazaarData.getAsJsonObject(item.bazaarId);
JsonArray sellSummary = itemData.getAsJsonArray("sell_summary");

JsonObject summaryData = (sellSummary.size() > 1) ? sellSummary.get(1).getAsJsonObject() :
(sellSummary.size() > 0) ? sellSummary.get(0).getAsJsonObject() : null;

if (summaryData != null) {
double buyPrice = summaryData.get("pricePerUnit").getAsDouble();
bazaarPrices.computeIfPresent(item.localizedName, (name, apiCrop) -> {
apiCrop.currentPrice = buyPrice;
return apiCrop;
});
bazaarPrices.putIfAbsent(item.localizedName, new APICrop(item.localizedName, buyPrice));
}
bazaarPrices.put(item.localizedName, apiCrop);
}
}

Expand Down

0 comments on commit fa417db

Please sign in to comment.