From 8b692d1b11c089a43670f8a1f8637ba06d07a283 Mon Sep 17 00:00:00 2001 From: Allink Date: Sat, 20 May 2023 04:57:19 +0100 Subject: [PATCH] Add financial transaction classes --- .../economy/SimpleCompletedTransaction.java | 30 ++++++++++ .../fossil/economy/SimpleTransaction.java | 56 +++++++++++++++++++ .../economy/SimpleTransactionResult.java | 42 ++++++++++++++ .../economy/CompletedTransaction.java | 8 +++ .../me/totalfreedom/economy/Transaction.java | 16 ++++++ .../economy/TransactionResult.java | 12 ++++ 6 files changed, 164 insertions(+) create mode 100644 Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleCompletedTransaction.java create mode 100644 Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransaction.java create mode 100644 Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransactionResult.java create mode 100644 Patchwork/src/main/java/me/totalfreedom/economy/CompletedTransaction.java create mode 100644 Patchwork/src/main/java/me/totalfreedom/economy/Transaction.java create mode 100644 Patchwork/src/main/java/me/totalfreedom/economy/TransactionResult.java diff --git a/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleCompletedTransaction.java b/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleCompletedTransaction.java new file mode 100644 index 0000000..9080019 --- /dev/null +++ b/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleCompletedTransaction.java @@ -0,0 +1,30 @@ +package me.totalfreedom.fossil.economy; + +import me.totalfreedom.economy.CompletedTransaction; +import me.totalfreedom.economy.Transaction; +import me.totalfreedom.economy.TransactionResult; + +public class SimpleCompletedTransaction implements CompletedTransaction +{ + private final Transaction transaction; + private final TransactionResult transactionResult; + + public SimpleCompletedTransaction(Transaction transaction, TransactionResult transactionResult) + { + this.transaction = transaction.copy(); + this.transactionResult = transactionResult; + } + + + @Override + public Transaction getTransaction() + { + return transaction.copy(); + } + + @Override + public TransactionResult getResult() + { + return transactionResult; + } +} diff --git a/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransaction.java b/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransaction.java new file mode 100644 index 0000000..17e0a13 --- /dev/null +++ b/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransaction.java @@ -0,0 +1,56 @@ +package me.totalfreedom.fossil.economy; + +import me.totalfreedom.economy.EconomicEntity; +import me.totalfreedom.economy.Transaction; + +import java.util.concurrent.atomic.AtomicLong; + +public class SimpleTransaction implements Transaction +{ + private final EconomicEntity source; + private final EconomicEntity destination; + private final AtomicLong transferAmount; + + public SimpleTransaction(EconomicEntity source, EconomicEntity destination, long transferAmount) + { + this.source = source; + this.destination = destination; + this.transferAmount = new AtomicLong(transferAmount); + } + + @Override + public Transaction copy() + { + return new SimpleTransaction(source, destination, transferAmount.get()); + } + + @Override + public EconomicEntity getSource() + { + return this.source; + } + + @Override + public EconomicEntity getDestination() + { + return this.destination; + } + + @Override + public long getTransferAmount() + { + return this.transferAmount.get(); + } + + @Override + public long addBalance(long amount) + { + return this.transferAmount.addAndGet(amount); + } + + @Override + public long removeBalance(long amount) + { + return this.addBalance(-amount); + } +} diff --git a/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransactionResult.java b/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransactionResult.java new file mode 100644 index 0000000..57f010a --- /dev/null +++ b/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransactionResult.java @@ -0,0 +1,42 @@ +package me.totalfreedom.fossil.economy; + +import me.totalfreedom.economy.TransactionResult; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; + +public class SimpleTransactionResult implements TransactionResult +{ + private final String message; + private final Component component; + private final boolean successful; + + public SimpleTransactionResult(String message, boolean successful) + { + this(message, Component.text(message, successful ? NamedTextColor.GREEN : NamedTextColor.RED), successful); + } + + public SimpleTransactionResult(String message, Component component, boolean successful) + { + this.message = message; + this.component = component; + this.successful = successful; + } + + @Override + public String getMessage() + { + return message; + } + + @Override + public boolean isSuccessful() + { + return successful; + } + + @Override + public Component getComponent() + { + return component; + } +} diff --git a/Patchwork/src/main/java/me/totalfreedom/economy/CompletedTransaction.java b/Patchwork/src/main/java/me/totalfreedom/economy/CompletedTransaction.java new file mode 100644 index 0000000..5c67cfa --- /dev/null +++ b/Patchwork/src/main/java/me/totalfreedom/economy/CompletedTransaction.java @@ -0,0 +1,8 @@ +package me.totalfreedom.economy; + +public interface CompletedTransaction +{ + Transaction getTransaction(); + + TransactionResult getResult(); +} diff --git a/Patchwork/src/main/java/me/totalfreedom/economy/Transaction.java b/Patchwork/src/main/java/me/totalfreedom/economy/Transaction.java new file mode 100644 index 0000000..d61b3c0 --- /dev/null +++ b/Patchwork/src/main/java/me/totalfreedom/economy/Transaction.java @@ -0,0 +1,16 @@ +package me.totalfreedom.economy; + +public interface Transaction +{ + Transaction copy(); + + EconomicEntity getSource(); + + EconomicEntity getDestination(); + + long getTransferAmount(); + + long addBalance(final long amount); + + long removeBalance(final long amount); +} diff --git a/Patchwork/src/main/java/me/totalfreedom/economy/TransactionResult.java b/Patchwork/src/main/java/me/totalfreedom/economy/TransactionResult.java new file mode 100644 index 0000000..fd4a13d --- /dev/null +++ b/Patchwork/src/main/java/me/totalfreedom/economy/TransactionResult.java @@ -0,0 +1,12 @@ +package me.totalfreedom.economy; + +import net.kyori.adventure.text.Component; + +public interface TransactionResult +{ + String getMessage(); + + boolean isSuccessful(); + + Component getComponent(); +}