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
This commit is contained in:
Daniel Poustka 2020-12-27 12:18:46 +01:00
parent 8f8bc9f4aa
commit e25879bcb6
7 changed files with 68 additions and 22 deletions

View File

@ -11,6 +11,7 @@ public class Customer extends BuyerNodeBase {
super(startMoney); super(startMoney);
allowedSeller = NodeType.Distributor; allowedSeller = NodeType.Distributor;
allowedPurchaser = NodeType.None; allowedPurchaser = NodeType.None;
this.nodeType = NodeType.Customer;
} }
public void consumeProduct() { public void consumeProduct() {

View File

@ -9,5 +9,6 @@ public class Distributor extends RetailerNodeBase {
super(startMoney); super(startMoney);
allowedSeller = NodeType.Seller; allowedSeller = NodeType.Seller;
allowedPurchaser = NodeType.Customer; allowedPurchaser = NodeType.Customer;
this.nodeType = NodeType.Distributor;
} }
} }

View File

@ -13,6 +13,7 @@ public class Farmer extends SellerNodeBase {
super(startMoney); super(startMoney);
allowedSeller = NodeType.None; allowedSeller = NodeType.None;
allowedPurchaser = NodeType.Processor; allowedPurchaser = NodeType.Processor;
this.nodeType = NodeType.Farmer;
} }
public void generateProduct(Product product) { public void generateProduct(Product product) {

View File

@ -9,5 +9,6 @@ public class Processor extends RetailerNodeBase {
super(startMoney); super(startMoney);
allowedSeller = NodeType.Farmer; allowedSeller = NodeType.Farmer;
allowedPurchaser = NodeType.Seller; allowedPurchaser = NodeType.Seller;
this.nodeType = NodeType.Processor;
} }
} }

View File

@ -9,5 +9,6 @@ public class Seller extends RetailerNodeBase {
super(startMoney); super(startMoney);
allowedSeller = NodeType.Processor; allowedSeller = NodeType.Processor;
allowedPurchaser = NodeType.Distributor; allowedPurchaser = NodeType.Distributor;
this.nodeType = NodeType.Seller;
} }
} }

View File

@ -27,21 +27,24 @@ public class NodeBase implements ChannelObserver {
private final List<Transaction> refusedTransactions; private final List<Transaction> refusedTransactions;
private MonetaryAmount money; private MonetaryAmount money;
protected NodeType nodeType;
protected NodeType allowedSeller; protected NodeType allowedSeller;
protected NodeType allowedPurchaser; protected NodeType allowedPurchaser;
public NodeBase() { public NodeBase() {
subscribedChannels = new HashMap<>(); this.nodeType = NodeType.None;
products = new LinkedList<>(); this.subscribedChannels = new HashMap<>();
refusedTransactions = new LinkedList<>(); this.products = new LinkedList<>();
money = Money.of(0, "USD"); this.refusedTransactions = new LinkedList<>();
this.money = Money.of(0, "USD");
} }
public NodeBase(double m) { public NodeBase(double m) {
subscribedChannels = new HashMap<>(); this.subscribedChannels = new HashMap<>();
products = new LinkedList<>(); this.products = new LinkedList<>();
refusedTransactions = new LinkedList<>(); this.refusedTransactions = new LinkedList<>();
money = Money.of(m, "USD"); this.money = Money.of(m, "USD");
} }
@ -131,7 +134,7 @@ public class NodeBase implements ChannelObserver {
} }
public void acceptRequirement(Requirement req) throws InsufficientResourcesException { 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) { if (req.getTransactionType() == TransactionType.SALE) {
createRequirement(TransactionType.PURCHASE, req.getProductType(), req.getProductName(), req.getPrice()); 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) { /**
* 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); 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) { boolean senderPurchaserCheck(Requirement req) {
String simpleName = req.getCreator().getClass().getSimpleName(); NodeType creatorType = req.getCreator().nodeType;
return !req.getCreator().equals(this) && return !req.getCreator().equals(this) &&
(((allowedSeller == NodeType.valueOf(simpleName) || allowedSeller == NodeType.All) && (req.getTransactionType() == TransactionType.SALE)) || (((allowedSeller == creatorType || allowedSeller == NodeType.All) && (req.getTransactionType() == TransactionType.SALE)) ||
((allowedPurchaser == NodeType.valueOf(simpleName) || allowedPurchaser == NodeType.All) && req.getTransactionType() == TransactionType.PURCHASE)); ((allowedPurchaser == creatorType || allowedPurchaser == NodeType.All) && req.getTransactionType() == TransactionType.PURCHASE));
} }
boolean transactionSubscriptionCheck(Requirement requirement, ChannelBase channel) { boolean transactionSubscriptionCheck(Requirement requirement, ChannelBase channel) {

View File

@ -2,27 +2,51 @@ package cz.cvut.fel.sit.omo.foodchain.operation;
import cz.cvut.fel.sit.omo.foodchain.node.enums.NodeType; 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 NodeType node;
private final int duration; private final int duration;
private final int temperature; private final int temperature;
ProductOperationType(NodeType node, int duration, int temperature) { ProductOperationType(NodeType node, int duration, int temperature)
{
this.node = node; this.node = node;
this.duration = duration; this.duration = duration;
this.temperature = temperature; this.temperature = temperature;
} }
public int getDuration() { public int getDuration()
{
return duration; return duration;
} }
public int getTemperature() { public int getTemperature()
{
return temperature; return temperature;
} }
public NodeType getNode() { return node; } public NodeType getNode()
{
return node;
}
} }