Moving nodes classes to directories, create new enum, limit sellers and purchasers

This commit is contained in:
Nikita Iryupin 2020-12-26 15:43:33 +01:00
parent 3aaa5c72e6
commit 853bf05ee5
27 changed files with 86 additions and 34 deletions

View File

@ -17,8 +17,8 @@
</dependencies> </dependencies>
<properties> <properties>
<maven.compiler.target>12</maven.compiler.target> <maven.compiler.target>14</maven.compiler.target>
<maven.compiler.source>12</maven.compiler.source> <maven.compiler.source>14</maven.compiler.source>
</properties> </properties>
</project> </project>

View File

@ -2,7 +2,7 @@ package cz.cvut.fel.sit.omo.foodchain;
import cz.cvut.fel.sit.omo.foodchain.channel.ChannelBase; import cz.cvut.fel.sit.omo.foodchain.channel.ChannelBase;
import cz.cvut.fel.sit.omo.foodchain.factory.FactoryManager; import cz.cvut.fel.sit.omo.foodchain.factory.FactoryManager;
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase; import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.parser.ConfigurationParser; import cz.cvut.fel.sit.omo.foodchain.parser.ConfigurationParser;
import cz.cvut.fel.sit.omo.foodchain.parser.ParserResult; import cz.cvut.fel.sit.omo.foodchain.parser.ParserResult;
import cz.cvut.fel.sit.omo.foodchain.parser.ParserResultElement; import cz.cvut.fel.sit.omo.foodchain.parser.ParserResultElement;

View File

@ -3,6 +3,8 @@ package cz.cvut.fel.sit.omo.foodchain.factory;
import cz.cvut.fel.sit.omo.foodchain.FoodChain; import cz.cvut.fel.sit.omo.foodchain.FoodChain;
import cz.cvut.fel.sit.omo.foodchain.channel.ChannelSubscriptionType; import cz.cvut.fel.sit.omo.foodchain.channel.ChannelSubscriptionType;
import cz.cvut.fel.sit.omo.foodchain.node.*; import cz.cvut.fel.sit.omo.foodchain.node.*;
import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.node.enums.NodeType;
import cz.cvut.fel.sit.omo.foodchain.parser.ParserResultElement; import cz.cvut.fel.sit.omo.foodchain.parser.ParserResultElement;
import cz.cvut.fel.sit.omo.foodchain.pool.ChannelPool; import cz.cvut.fel.sit.omo.foodchain.pool.ChannelPool;
import cz.cvut.fel.sit.omo.foodchain.product.ProductType; import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
@ -12,11 +14,6 @@ import java.util.Objects;
public class NodeFactory public class NodeFactory
{ {
enum NodeType
{
Farmer, Processor, Distributor, Seller, Customer;
}
/** /**
* Creates Node based on input ParserResultElement * Creates Node based on input ParserResultElement
* @param input data from Node is created * @param input data from Node is created

View File

@ -3,7 +3,7 @@ package cz.cvut.fel.sit.omo.foodchain.factory;
import cz.cvut.fel.sit.omo.foodchain.FoodChain; import cz.cvut.fel.sit.omo.foodchain.FoodChain;
import cz.cvut.fel.sit.omo.foodchain.node.Customer; import cz.cvut.fel.sit.omo.foodchain.node.Customer;
import cz.cvut.fel.sit.omo.foodchain.node.Farmer; import cz.cvut.fel.sit.omo.foodchain.node.Farmer;
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase; import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.operation.ProductOperationType; import cz.cvut.fel.sit.omo.foodchain.operation.ProductOperationType;
import cz.cvut.fel.sit.omo.foodchain.operation.TransactionType; import cz.cvut.fel.sit.omo.foodchain.operation.TransactionType;
import cz.cvut.fel.sit.omo.foodchain.parser.ParserResultElement; import cz.cvut.fel.sit.omo.foodchain.parser.ParserResultElement;

View File

@ -1,11 +1,16 @@
package cz.cvut.fel.sit.omo.foodchain.node; package cz.cvut.fel.sit.omo.foodchain.node;
import cz.cvut.fel.sit.omo.foodchain.node.base.BuyerNodeBase;
import cz.cvut.fel.sit.omo.foodchain.node.enums.NodeType;
import java.util.Random; import java.util.Random;
public class Customer extends BuyerNodeBase { public class Customer extends BuyerNodeBase {
public Customer(double startMoney) { public Customer(double startMoney) {
super(); super(startMoney);
allowedSeller = NodeType.Distributor;
allowedPurchaser = NodeType.None;
} }
public void consumeProduct() { public void consumeProduct() {

View File

@ -1,8 +1,13 @@
package cz.cvut.fel.sit.omo.foodchain.node; package cz.cvut.fel.sit.omo.foodchain.node;
import cz.cvut.fel.sit.omo.foodchain.node.base.RetailerNodeBase;
import cz.cvut.fel.sit.omo.foodchain.node.enums.NodeType;
public class Distributor extends RetailerNodeBase { public class Distributor extends RetailerNodeBase {
public Distributor(double startMoney) { public Distributor(double startMoney) {
super(); super(startMoney);
allowedSeller = NodeType.Seller;
allowedPurchaser = NodeType.Customer;
} }
} }

View File

@ -1,12 +1,16 @@
package cz.cvut.fel.sit.omo.foodchain.node; package cz.cvut.fel.sit.omo.foodchain.node;
import cz.cvut.fel.sit.omo.foodchain.node.base.SellerNodeBase;
import cz.cvut.fel.sit.omo.foodchain.node.enums.NodeType;
import cz.cvut.fel.sit.omo.foodchain.operation.TransactionType; import cz.cvut.fel.sit.omo.foodchain.operation.TransactionType;
import cz.cvut.fel.sit.omo.foodchain.product.Product; import cz.cvut.fel.sit.omo.foodchain.product.Product;
public class Farmer extends SellerNodeBase { public class Farmer extends SellerNodeBase {
public Farmer(double startMoney) { public Farmer(double startMoney) {
super(); super(startMoney);
allowedSeller = NodeType.None;
allowedPurchaser = NodeType.Processor;
} }
public void generateProduct(Product product) { public void generateProduct(Product product) {

View File

@ -1,8 +1,13 @@
package cz.cvut.fel.sit.omo.foodchain.node; package cz.cvut.fel.sit.omo.foodchain.node;
import cz.cvut.fel.sit.omo.foodchain.node.base.RetailerNodeBase;
import cz.cvut.fel.sit.omo.foodchain.node.enums.NodeType;
public class Processor extends RetailerNodeBase { public class Processor extends RetailerNodeBase {
public Processor(double startMoney) { public Processor(double startMoney) {
super(); super(startMoney);
allowedSeller = NodeType.Farmer;
allowedPurchaser = NodeType.Seller;
} }
} }

View File

@ -1,8 +1,13 @@
package cz.cvut.fel.sit.omo.foodchain.node; package cz.cvut.fel.sit.omo.foodchain.node;
import cz.cvut.fel.sit.omo.foodchain.node.base.RetailerNodeBase;
import cz.cvut.fel.sit.omo.foodchain.node.enums.NodeType;
public class Seller extends RetailerNodeBase { public class Seller extends RetailerNodeBase {
public Seller(double startMoney) { public Seller(double startMoney) {
super(); super(startMoney);
allowedSeller = NodeType.Processor;
allowedPurchaser = NodeType.Distributor;
} }
} }

View File

@ -1,8 +1,13 @@
package cz.cvut.fel.sit.omo.foodchain.node; package cz.cvut.fel.sit.omo.foodchain.node.base;
import cz.cvut.fel.sit.omo.foodchain.node.interfaces.IBuyerNode;
import cz.cvut.fel.sit.omo.foodchain.product.Product; import cz.cvut.fel.sit.omo.foodchain.product.Product;
public class BuyerNodeBase extends NodeBase implements IBuyerNode { public class BuyerNodeBase extends NodeBase implements IBuyerNode {
public BuyerNodeBase(double m){
super(m);
}
@Override @Override
public void buyProduct(Product product) { public void buyProduct(Product product) {

View File

@ -1,7 +1,8 @@
package cz.cvut.fel.sit.omo.foodchain.node; package cz.cvut.fel.sit.omo.foodchain.node.base;
import cz.cvut.fel.sit.omo.foodchain.channel.ChannelBase; import cz.cvut.fel.sit.omo.foodchain.channel.ChannelBase;
import cz.cvut.fel.sit.omo.foodchain.channel.ChannelSubscriptionType; import cz.cvut.fel.sit.omo.foodchain.channel.ChannelSubscriptionType;
import cz.cvut.fel.sit.omo.foodchain.node.enums.NodeType;
import cz.cvut.fel.sit.omo.foodchain.operation.*; import cz.cvut.fel.sit.omo.foodchain.operation.*;
import cz.cvut.fel.sit.omo.foodchain.product.Product; 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;
@ -21,10 +22,12 @@ import java.util.Map;
public class NodeBase implements ChannelObserver { public class NodeBase implements ChannelObserver {
private final Map<ChannelBase, ChannelSubscriptionType> subscribedChannels; private final Map<ChannelBase, ChannelSubscriptionType> subscribedChannels;
private final List<Product> products; private final List<Product> products;
private final MonetaryAmount money;
private final List<Transaction> refusedTransactions; private final List<Transaction> refusedTransactions;
private final MonetaryAmount money;
protected NodeType allowedSeller;
protected NodeType allowedPurchaser;
public NodeBase() { public NodeBase() {
subscribedChannels = new HashMap<>(); subscribedChannels = new HashMap<>();
products = new LinkedList<>(); products = new LinkedList<>();
@ -140,9 +143,17 @@ public class NodeBase implements ChannelObserver {
return SignatureManager.validateSignature(transaction); return SignatureManager.validateSignature(transaction);
} }
boolean senderPurchaserCheck(Requirement req) {
String simpleName = req.getCreator().getClass().getSimpleName();
return !req.getCreator().equals(this) &&
(((allowedSeller == NodeType.valueOf(simpleName) || allowedSeller == NodeType.All) && (req.getTransactionType() == TransactionType.SALE)) ||
((allowedPurchaser == NodeType.valueOf(simpleName) || allowedPurchaser == NodeType.All) && req.getTransactionType() == TransactionType.PURCHASE));
}
@Override @Override
public void update(Requirement requirement) { public void update(Requirement requirement) {
if (!requirement.getCreator().equals(this)) if (senderPurchaserCheck(requirement))
acceptRequirement(requirement); acceptRequirement(requirement);
} }
} }

View File

@ -1,8 +1,14 @@
package cz.cvut.fel.sit.omo.foodchain.node; package cz.cvut.fel.sit.omo.foodchain.node.base;
import cz.cvut.fel.sit.omo.foodchain.node.interfaces.IBuyerNode;
import cz.cvut.fel.sit.omo.foodchain.node.interfaces.ISellerNode;
import cz.cvut.fel.sit.omo.foodchain.product.Product; import cz.cvut.fel.sit.omo.foodchain.product.Product;
public class RetailerNodeBase extends NodeBase implements IBuyerNode, ISellerNode { public class RetailerNodeBase extends NodeBase implements IBuyerNode, ISellerNode {
public RetailerNodeBase(double startMoney) {
super(startMoney);
}
@Override @Override
public void sellProduct(Product product) { public void sellProduct(Product product) {

View File

@ -1,8 +1,13 @@
package cz.cvut.fel.sit.omo.foodchain.node; package cz.cvut.fel.sit.omo.foodchain.node.base;
import cz.cvut.fel.sit.omo.foodchain.node.interfaces.ISellerNode;
import cz.cvut.fel.sit.omo.foodchain.product.Product; import cz.cvut.fel.sit.omo.foodchain.product.Product;
public class SellerNodeBase extends NodeBase implements ISellerNode { public class SellerNodeBase extends NodeBase implements ISellerNode {
public SellerNodeBase(double startMoney) {
super(startMoney);
}
@Override @Override
public void sellProduct(Product product) { public void sellProduct(Product product) {

View File

@ -0,0 +1,5 @@
package cz.cvut.fel.sit.omo.foodchain.node.enums;
public enum NodeType {
Farmer, Processor, Distributor, Seller, Customer, None, All
}

View File

@ -1,4 +1,4 @@
package cz.cvut.fel.sit.omo.foodchain.node; package cz.cvut.fel.sit.omo.foodchain.node.interfaces;
import cz.cvut.fel.sit.omo.foodchain.product.Product; import cz.cvut.fel.sit.omo.foodchain.product.Product;

View File

@ -1,4 +1,4 @@
package cz.cvut.fel.sit.omo.foodchain.node; package cz.cvut.fel.sit.omo.foodchain.node.interfaces;
import cz.cvut.fel.sit.omo.foodchain.product.Product; import cz.cvut.fel.sit.omo.foodchain.product.Product;

View File

@ -1,6 +1,6 @@
package cz.cvut.fel.sit.omo.foodchain.operation; package cz.cvut.fel.sit.omo.foodchain.operation;
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase; import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.product.Product; import cz.cvut.fel.sit.omo.foodchain.product.Product;
public class Action extends OperationBase { public class Action extends OperationBase {

View File

@ -1,6 +1,6 @@
package cz.cvut.fel.sit.omo.foodchain.operation; package cz.cvut.fel.sit.omo.foodchain.operation;
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase; import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
import javax.money.MonetaryAmount; import javax.money.MonetaryAmount;

View File

@ -1,6 +1,6 @@
package cz.cvut.fel.sit.omo.foodchain.operation; package cz.cvut.fel.sit.omo.foodchain.operation;
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase; import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
import java.time.LocalDateTime; import java.time.LocalDateTime;

View File

@ -1,6 +1,6 @@
package cz.cvut.fel.sit.omo.foodchain.operation; package cz.cvut.fel.sit.omo.foodchain.operation;
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase; import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.product.Product; import cz.cvut.fel.sit.omo.foodchain.product.Product;
public class ProductTransaction extends Transaction { public class ProductTransaction extends Transaction {

View File

@ -1,6 +1,6 @@
package cz.cvut.fel.sit.omo.foodchain.operation; package cz.cvut.fel.sit.omo.foodchain.operation;
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase; import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
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.signature.SignatureManager;

View File

@ -1,6 +1,6 @@
package cz.cvut.fel.sit.omo.foodchain.pool; package cz.cvut.fel.sit.omo.foodchain.pool;
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase; import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
public class NodePool extends PoolListBase<NodeBase> { public class NodePool extends PoolListBase<NodeBase> {
} }

View File

@ -1,7 +1,7 @@
package cz.cvut.fel.sit.omo.foodchain.reporting; package cz.cvut.fel.sit.omo.foodchain.reporting;
import cz.cvut.fel.sit.omo.foodchain.channel.ChannelBase; import cz.cvut.fel.sit.omo.foodchain.channel.ChannelBase;
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase; import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.operation.OperationBase; import cz.cvut.fel.sit.omo.foodchain.operation.OperationBase;
public class ReportTextFileVisitor { public class ReportTextFileVisitor {

View File

@ -1,6 +1,6 @@
package cz.cvut.fel.sit.omo.foodchain.requirement; package cz.cvut.fel.sit.omo.foodchain.requirement;
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase; import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.operation.TransactionType; import cz.cvut.fel.sit.omo.foodchain.operation.TransactionType;
import cz.cvut.fel.sit.omo.foodchain.product.ProductType; import cz.cvut.fel.sit.omo.foodchain.product.ProductType;

View File

@ -1,6 +1,6 @@
package cz.cvut.fel.sit.omo.foodchain.scenario; package cz.cvut.fel.sit.omo.foodchain.scenario;
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase; import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.operation.ProductOperationType; import cz.cvut.fel.sit.omo.foodchain.operation.ProductOperationType;
import cz.cvut.fel.sit.omo.foodchain.product.Product; import cz.cvut.fel.sit.omo.foodchain.product.Product;

View File

@ -2,7 +2,6 @@ package cz.cvut.fel.sit.omo.foodchain.scenario;
import cz.cvut.fel.sit.omo.foodchain.FoodChain; import cz.cvut.fel.sit.omo.foodchain.FoodChain;
import cz.cvut.fel.sit.omo.foodchain.node.Farmer; import cz.cvut.fel.sit.omo.foodchain.node.Farmer;
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.product.Product; import cz.cvut.fel.sit.omo.foodchain.product.Product;
public class GenerateProductStep extends ScenarioStep { public class GenerateProductStep extends ScenarioStep {

View File

@ -1,6 +1,6 @@
package cz.cvut.fel.sit.omo.foodchain.scenario; package cz.cvut.fel.sit.omo.foodchain.scenario;
import cz.cvut.fel.sit.omo.foodchain.node.NodeBase; import cz.cvut.fel.sit.omo.foodchain.node.base.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.operation.TransactionType; import cz.cvut.fel.sit.omo.foodchain.operation.TransactionType;
import cz.cvut.fel.sit.omo.foodchain.product.ProductType; import cz.cvut.fel.sit.omo.foodchain.product.ProductType;