From e25879bcb64412b806ce4d4f915319e03ca2011c Mon Sep 17 00:00:00 2001 From: Daniel Poustka Date: Sun, 27 Dec 2020 12:18:46 +0100 Subject: [PATCH] Added new enum instances of ProductOperationType, Added NodeType for each Node Added NodeBase.doAction over whole stock Added check if action is allowed to NodeBase.doAction --- .../fel/sit/omo/foodchain/node/Customer.java | 1 + .../sit/omo/foodchain/node/Distributor.java | 1 + .../fel/sit/omo/foodchain/node/Farmer.java | 1 + .../fel/sit/omo/foodchain/node/Processor.java | 1 + .../fel/sit/omo/foodchain/node/Seller.java | 1 + .../sit/omo/foodchain/node/base/NodeBase.java | 49 +++++++++++++------ .../operation/ProductOperationType.java | 36 +++++++++++--- 7 files changed, 68 insertions(+), 22 deletions(-) diff --git a/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/Customer.java b/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/Customer.java index 36cef45..d94670a 100644 --- a/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/Customer.java +++ b/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/Customer.java @@ -11,6 +11,7 @@ public class Customer extends BuyerNodeBase { super(startMoney); allowedSeller = NodeType.Distributor; allowedPurchaser = NodeType.None; + this.nodeType = NodeType.Customer; } public void consumeProduct() { diff --git a/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/Distributor.java b/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/Distributor.java index f3669d7..cf398e0 100644 --- a/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/Distributor.java +++ b/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/Distributor.java @@ -9,5 +9,6 @@ public class Distributor extends RetailerNodeBase { super(startMoney); allowedSeller = NodeType.Seller; allowedPurchaser = NodeType.Customer; + this.nodeType = NodeType.Distributor; } } diff --git a/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/Farmer.java b/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/Farmer.java index 23b9a6c..76bf758 100644 --- a/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/Farmer.java +++ b/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/Farmer.java @@ -13,6 +13,7 @@ public class Farmer extends SellerNodeBase { super(startMoney); allowedSeller = NodeType.None; allowedPurchaser = NodeType.Processor; + this.nodeType = NodeType.Farmer; } public void generateProduct(Product product) { diff --git a/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/Processor.java b/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/Processor.java index 1cb68a9..38237e4 100644 --- a/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/Processor.java +++ b/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/Processor.java @@ -9,5 +9,6 @@ public class Processor extends RetailerNodeBase { super(startMoney); allowedSeller = NodeType.Farmer; allowedPurchaser = NodeType.Seller; + this.nodeType = NodeType.Processor; } } diff --git a/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/Seller.java b/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/Seller.java index 47b9c5b..c2003d1 100644 --- a/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/Seller.java +++ b/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/Seller.java @@ -9,5 +9,6 @@ public class Seller extends RetailerNodeBase { super(startMoney); allowedSeller = NodeType.Processor; allowedPurchaser = NodeType.Distributor; + this.nodeType = NodeType.Seller; } } diff --git a/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/base/NodeBase.java b/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/base/NodeBase.java index 018139d..08df05d 100644 --- a/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/base/NodeBase.java +++ b/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/base/NodeBase.java @@ -27,21 +27,24 @@ public class NodeBase implements ChannelObserver { private final List refusedTransactions; private MonetaryAmount money; + + protected NodeType nodeType; protected NodeType allowedSeller; protected NodeType allowedPurchaser; public NodeBase() { - subscribedChannels = new HashMap<>(); - products = new LinkedList<>(); - refusedTransactions = new LinkedList<>(); - money = Money.of(0, "USD"); + this.nodeType = NodeType.None; + this.subscribedChannels = new HashMap<>(); + this.products = new LinkedList<>(); + this.refusedTransactions = new LinkedList<>(); + this.money = Money.of(0, "USD"); } public NodeBase(double m) { - subscribedChannels = new HashMap<>(); - products = new LinkedList<>(); - refusedTransactions = new LinkedList<>(); - money = Money.of(m, "USD"); + this.subscribedChannels = new HashMap<>(); + this.products = new LinkedList<>(); + this.refusedTransactions = new LinkedList<>(); + this.money = Money.of(m, "USD"); } @@ -131,7 +134,7 @@ public class NodeBase implements ChannelObserver { } public void acceptRequirement(Requirement req) throws InsufficientResourcesException { - System.out.println(this.getClass().getSimpleName() + " accepted " + req.toString()); + System.out.println(this.nodeType.toString() + " accepted " + req.toString()); if (req.getTransactionType() == TransactionType.SALE) { createRequirement(TransactionType.PURCHASE, req.getProductType(), req.getProductName(), req.getPrice()); } @@ -156,13 +159,27 @@ public class NodeBase implements ChannelObserver { } - public void doAction(ProductOperationType action, Product product) { - new Action(this, product, action); + /** + * Proceed specific type of action on provided product if its possible. + * @param action type of product operation to do + * @param product product to proceed + */ + public void doAction(ProductOperationType action, Product product) + { + if(action.getNode().equals(this.nodeType)) + new Action(this, product, action); + else + throw new IllegalCallerException("! IllegalCallerException in NodeBase. " + this.nodeType.toString() + " cannot do action available only for "+action.getNode()); } - public void doAction(ProductOperationType action) { - //create new action + /** + * Calls specific ProductOperation on each product in stock + * @param action ProductOperationType + */ + public void doAction(ProductOperationType action) + { + this.products.forEach(t -> new Action(this, t, action)); } @@ -201,11 +218,11 @@ public class NodeBase implements ChannelObserver { } boolean senderPurchaserCheck(Requirement req) { - String simpleName = req.getCreator().getClass().getSimpleName(); + NodeType creatorType = req.getCreator().nodeType; 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)); + (((allowedSeller == creatorType || allowedSeller == NodeType.All) && (req.getTransactionType() == TransactionType.SALE)) || + ((allowedPurchaser == creatorType || allowedPurchaser == NodeType.All) && req.getTransactionType() == TransactionType.PURCHASE)); } boolean transactionSubscriptionCheck(Requirement requirement, ChannelBase channel) { diff --git a/src/main/java/cz/cvut/fel/sit/omo/foodchain/operation/ProductOperationType.java b/src/main/java/cz/cvut/fel/sit/omo/foodchain/operation/ProductOperationType.java index 88868dc..594be25 100644 --- a/src/main/java/cz/cvut/fel/sit/omo/foodchain/operation/ProductOperationType.java +++ b/src/main/java/cz/cvut/fel/sit/omo/foodchain/operation/ProductOperationType.java @@ -2,27 +2,51 @@ package cz.cvut.fel.sit.omo.foodchain.operation; import cz.cvut.fel.sit.omo.foodchain.node.enums.NodeType; -public enum ProductOperationType { +public enum ProductOperationType +{ + STORE_MEAT(NodeType.All, 20, -8), + STORE_FRUIT(NodeType.All, 8, 2), + STORE_VEGETABLE(NodeType.All, 10, 2), + STORE_OTHER(NodeType.All, 30, 21), - STORE_MEAT(NodeType.All, 10, -8); + WRAP_INTO_GIFT_PACKAGE(NodeType.Seller, 1, -8), + UNWRAP(NodeType.All, 1, 21), + CUT_TO_PIECES(NodeType.All, 1, 21), + + BOIL_MEAT(NodeType.All, 1, 21), + GRILL_MEAT(NodeType.All, 1, 21), + SMOKE_MEAT(NodeType.All, 1, 21), + DRY_MEAT(NodeType.All, 1, 21), + + DRY_FRUIT(NodeType.All, 1, 21), + FREEZE_DRY_FRUIT(NodeType.All, 1, 21); + + + //todo add product type allowed for this action private final NodeType node; private final int duration; private final int temperature; - ProductOperationType(NodeType node, int duration, int temperature) { + ProductOperationType(NodeType node, int duration, int temperature) + { this.node = node; this.duration = duration; this.temperature = temperature; } - public int getDuration() { + public int getDuration() + { return duration; } - public int getTemperature() { + public int getTemperature() + { return temperature; } - public NodeType getNode() { return node; } + public NodeType getNode() + { + return node; + } }