|
| 1 | +package org.allaymc.economyapi; |
| 2 | + |
| 3 | +import org.allaymc.economyapi.event.BalanceChangeEvent; |
| 4 | +import org.allaymc.economyapi.event.BalanceTransferEvent; |
| 5 | +import org.jetbrains.annotations.Unmodifiable; |
| 6 | + |
| 7 | +import java.math.BigDecimal; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.UUID; |
| 10 | + |
| 11 | +/** |
| 12 | + * Represents an account, which stores amounts of various {@link Currency currencies}. |
| 13 | + * <p> |
| 14 | + * Every account is bound to a {@link UUID}, it usually should be same to the player's |
| 15 | + * uuid if the account belongs to a player, or a random uuid in other cases. |
| 16 | + * |
| 17 | + * @author daoge_cmd |
| 18 | + */ |
| 19 | +public interface Account { |
| 20 | + |
| 21 | + /** |
| 22 | + * Gets the unique id of this account. |
| 23 | + * |
| 24 | + * @return the unique id of this account |
| 25 | + */ |
| 26 | + UUID getUniqueId(); |
| 27 | + |
| 28 | + /** |
| 29 | + * Gets the name of this account. |
| 30 | + * |
| 31 | + * @return the name of this account |
| 32 | + */ |
| 33 | + String getName(); |
| 34 | + |
| 35 | + /** |
| 36 | + * Gets a {@link BigDecimal} representative of the balance for the given {@link Currency}. |
| 37 | + * |
| 38 | + * @param currency the currency to get the balance for |
| 39 | + * @return the balance of the currency |
| 40 | + */ |
| 41 | + BigDecimal getBalance(Currency currency); |
| 42 | + |
| 43 | + /** |
| 44 | + * Retrieves an unmodifiable map of all balances associated with this account. Each entry |
| 45 | + * in the map represents a {@link Currency} and its corresponding balance as a {@link BigDecimal}. |
| 46 | + * |
| 47 | + * @return an unmodifiable map containing currencies as keys and their respective balances as values |
| 48 | + */ |
| 49 | + @Unmodifiable |
| 50 | + Map<Currency, BigDecimal> getBalances(); |
| 51 | + |
| 52 | + /** |
| 53 | + * Sets the balance for the given currency to the specified amount. Event {@link BalanceChangeEvent} |
| 54 | + * will be called when this method is called. |
| 55 | + * |
| 56 | + * @param currency the {@link Currency} for which the balance is being set |
| 57 | + * @param amount the {@link BigDecimal} value representing the new balance |
| 58 | + * @return {@code true} if the balance was successfully set, otherwise {@code false} |
| 59 | + */ |
| 60 | + boolean setBalance(Currency currency, BigDecimal amount); |
| 61 | + |
| 62 | + /** |
| 63 | + * Deposits a specified amount of the given currency into the account. |
| 64 | + * The new balance is calculated by adding the deposit amount to the current balance. |
| 65 | + * |
| 66 | + * @param currency the {@link Currency} being deposited |
| 67 | + * @param amount the {@link BigDecimal} value of the amount to deposit |
| 68 | + * @return {@code true} if the new balance was successfully updated, otherwise {@code false} |
| 69 | + */ |
| 70 | + default boolean deposit(Currency currency, BigDecimal amount) { |
| 71 | + return setBalance(currency, getBalance(currency).add(amount)); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Withdraws a specified amount of the given currency from the account. |
| 76 | + * The new balance is calculated by subtracting the withdrawal amount |
| 77 | + * from the current balance. |
| 78 | + * |
| 79 | + * @param currency the {@link Currency} from which the amount is being withdrawn |
| 80 | + * @param amount the {@link BigDecimal} value of the amount to withdraw |
| 81 | + * @return {@code true} if the new balance was successfully updated, otherwise {@code false} |
| 82 | + */ |
| 83 | + default boolean withdraw(Currency currency, BigDecimal amount) { |
| 84 | + return setBalance(currency, getBalance(currency).subtract(amount)); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Transfers a specified amount of a given currency from this account to another account. |
| 89 | + * Event {@link BalanceTransferEvent} will be called when this method is called. |
| 90 | + * |
| 91 | + * @param to the recipient {@link Account} to which the amount will be transferred |
| 92 | + * @param currency the {@link Currency} of the amount being transferred |
| 93 | + * @param amount the {@link BigDecimal} value representing the amount to be transferred |
| 94 | + * @return {@code true} if the transfer was successful, otherwise {@code false} |
| 95 | + */ |
| 96 | + boolean transfer(Account to, Currency currency, BigDecimal amount); |
| 97 | +} |
0 commit comments