Merge branch 'feature/node_impl' into develop
This commit is contained in:
commit
f1cad5d636
@ -6,24 +6,30 @@ import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
|||||||
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
|
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
|
||||||
import cz.cvut.fel.sit.omo.foodchain.requirement.Requirement;
|
import cz.cvut.fel.sit.omo.foodchain.requirement.Requirement;
|
||||||
import cz.cvut.fel.sit.omo.foodchain.signature.Signature;
|
import cz.cvut.fel.sit.omo.foodchain.signature.Signature;
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.signature.SignatureManager;
|
||||||
|
import cz.cvut.fel.sit.omo.foodchain.util.ChannelObserver;
|
||||||
|
|
||||||
import javax.money.MonetaryAmount;
|
import javax.money.MonetaryAmount;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
public class NodeBase {
|
public class NodeBase implements ChannelObserver {
|
||||||
private final List<ChannelBase> subscribedChannels;
|
private final List<ChannelBase> subscribedChannels;
|
||||||
private final List<Product> products;
|
private final List<Product> products;
|
||||||
|
private MonetaryAmount money;
|
||||||
|
|
||||||
|
private final List<Transaction> refusedTranslations;
|
||||||
|
|
||||||
public NodeBase() {
|
public NodeBase() {
|
||||||
subscribedChannels = new LinkedList<>();
|
subscribedChannels = new LinkedList<>();
|
||||||
products = new LinkedList<>();
|
products = new LinkedList<>();
|
||||||
|
refusedTranslations = new LinkedList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void sendTransaction(Product product, NodeBase node) {
|
public void sendTransaction(Product product, NodeBase node) {
|
||||||
Transaction transaction = new ProductTransaction(this, node, TransactionType.SALE, product); //TODO wrong constructor
|
Transaction transaction = new ProductTransaction(this, node, TransactionType.SALE, product);
|
||||||
|
|
||||||
subscribedChannels.stream()
|
subscribedChannels.stream()
|
||||||
.filter(ch -> ch.getProductType().equals(product.getProductType()))
|
.filter(ch -> ch.getProductType().equals(product.getProductType()))
|
||||||
@ -31,8 +37,12 @@ public class NodeBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void sendTransaction(MonetaryAmount amount, NodeBase node) {
|
public void sendTransaction(MonetaryAmount amount, NodeBase node, ProductTransaction prevTransaction) {
|
||||||
|
Transaction transaction = new FinancialTransaction(this, node, TransactionType.PURCHASE, amount, prevTransaction);
|
||||||
|
|
||||||
|
subscribedChannels.stream()
|
||||||
|
.filter(ch -> ch.getProductType().equals(prevTransaction.getProduct().getProductType()))
|
||||||
|
.findAny().ifPresent(channel -> channel.addTransaction(transaction));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -46,26 +56,18 @@ public class NodeBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void refuseTransaction(ProductTransaction transaction) {
|
public void refuseTransaction(Transaction transaction) {
|
||||||
|
transaction.endTransaction();
|
||||||
|
refusedTranslations.add(transaction); //TODO maybe need more work on it
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void refuseTransaction(FinancialTransaction transaction) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO another way to read transaction
|
|
||||||
|
|
||||||
public Transaction readNextTransaction(ChannelBase channel) {
|
public Transaction readNextTransaction(ChannelBase channel) {
|
||||||
|
|
||||||
|
|
||||||
return channel.getNextTransaction();
|
return channel.getNextTransaction();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Requirement readNextRequirement(ChannelBase channel) {
|
|
||||||
return channel.getNextRequirement();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void acceptRequirement(Requirement requirement) {
|
public void acceptRequirement(Requirement requirement) {
|
||||||
|
|
||||||
@ -83,7 +85,7 @@ public class NodeBase {
|
|||||||
|
|
||||||
|
|
||||||
public void doAction(ProductOperationType action, Product product) {
|
public void doAction(ProductOperationType action, Product product) {
|
||||||
//create new action
|
Action actionProcess = new Action(this, product);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -93,26 +95,32 @@ public class NodeBase {
|
|||||||
|
|
||||||
|
|
||||||
public void storeProduct(Product product) {
|
public void storeProduct(Product product) {
|
||||||
|
this.products.add(product);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void storeProducts(List<Product> products) {
|
public void storeProducts(List<Product> products) {
|
||||||
|
this.products.addAll(products);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Product getProductFromStorage(String name, ProductType type) {
|
public Product getProductFromStorage(String name, ProductType type) {
|
||||||
return null;
|
return products.stream().filter(ch -> ch.getProductType() == type && ch.getName().equals(name)).findFirst().orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Signature generateSignature(Transaction transaction) {
|
public Signature generateSignature(Transaction transaction) {
|
||||||
return null;
|
return SignatureManager.signTransaction(transaction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean validateSignature(Signature signature) {
|
public boolean validateSignature(Transaction transaction) {
|
||||||
return false;
|
return SignatureManager.validateSignature(transaction);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update(Requirement requirement) {
|
||||||
|
if (!requirement.getCreator().equals(this))
|
||||||
|
acceptRequirement(requirement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,7 +9,7 @@ public class FinancialTransaction extends Transaction {
|
|||||||
private final MonetaryAmount amount;
|
private final MonetaryAmount amount;
|
||||||
private final ProductTransaction transactionPaid;
|
private final ProductTransaction transactionPaid;
|
||||||
|
|
||||||
FinancialTransaction(NodeBase creator, NodeBase receiver, TransactionType type, MonetaryAmount amount, ProductTransaction transactionPaid) {
|
public FinancialTransaction(NodeBase creator, NodeBase receiver, TransactionType type, MonetaryAmount amount, ProductTransaction transactionPaid) {
|
||||||
super(creator, receiver, type);
|
super(creator, receiver, type);
|
||||||
this.amount = amount;
|
this.amount = amount;
|
||||||
this.transactionPaid = transactionPaid;
|
this.transactionPaid = transactionPaid;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user