Add financial transaction classes

This commit is contained in:
Allink 2023-05-20 04:57:19 +01:00
parent 2f18e4d8ef
commit 8b692d1b11
No known key found for this signature in database
6 changed files with 164 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,8 @@
package me.totalfreedom.economy;
public interface CompletedTransaction
{
Transaction getTransaction();
TransactionResult getResult();
}

View File

@ -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);
}

View File

@ -0,0 +1,12 @@
package me.totalfreedom.economy;
import net.kyori.adventure.text.Component;
public interface TransactionResult
{
String getMessage();
boolean isSuccessful();
Component getComponent();
}