-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from fedy97/feature/add-balance-command
feat: add /balance command
- Loading branch information
Showing
10 changed files
with
124 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.bot.commands; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.bot.commands.base.Command; | ||
import org.bot.operations.Operations; | ||
import org.bot.operations.OperationsDispatcher; | ||
import org.bot.utils.formatters.BalanceDecorator; | ||
import org.bot.visitor.CommandVisitor; | ||
import org.telegram.telegrambots.meta.api.objects.Update; | ||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException; | ||
|
||
import java.util.Map; | ||
|
||
|
||
@Slf4j | ||
public class BalanceCommand implements Command { | ||
|
||
public BalanceCommand() { | ||
super(); | ||
} | ||
|
||
@Override | ||
public void execute(Update update) throws TelegramApiException { | ||
String[] parts = update.getMessage().getText().split(" "); | ||
String platform = parts[1].toLowerCase().trim(); | ||
|
||
Operations operations = OperationsDispatcher.getInstance().getOperations(platform); | ||
Map<String, Double> balance = operations.getBalance(); | ||
|
||
sendText(update.getMessage().getChatId(), new BalanceDecorator(balance.toString()).toString()); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "/balance"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "<platform> to see the balance of your account"; | ||
} | ||
|
||
@Override | ||
public void accept(CommandVisitor visitor) { | ||
visitor.visitBalanceCommand(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.bot.utils; | ||
|
||
import java.util.Map; | ||
|
||
public class Helpers { | ||
private Helpers() {} | ||
|
||
public static void addToMapOrSum(Map<String, Double> map, String key, Double value) { | ||
map.compute(key, (k, v) -> (v == null) ? value : v + value); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/org/bot/utils/formatters/BalanceDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.bot.utils.formatters; | ||
|
||
import java.util.Arrays; | ||
|
||
public class BalanceDecorator extends StringDecorator { | ||
|
||
public BalanceDecorator(String decoratedString) { | ||
super(decoratedString); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
String noCurlyBraces = decoratedString.replace("{", "").replace("}", ""); | ||
String[] coins = noCurlyBraces.split(","); | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 0; i < coins.length; i++) { | ||
String coin = coins[i].trim(); | ||
sb.append(coin.replace("=", ": ")); | ||
if (i < coins.length - 1) | ||
sb.append("\n"); | ||
} | ||
return sb.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters