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:
parent
8f8bc9f4aa
commit
e25879bcb6
@ -11,6 +11,7 @@ public class Customer extends BuyerNodeBase {
|
||||
super(startMoney);
|
||||
allowedSeller = NodeType.Distributor;
|
||||
allowedPurchaser = NodeType.None;
|
||||
this.nodeType = NodeType.Customer;
|
||||
}
|
||||
|
||||
public void consumeProduct() {
|
||||
|
||||
@ -9,5 +9,6 @@ public class Distributor extends RetailerNodeBase {
|
||||
super(startMoney);
|
||||
allowedSeller = NodeType.Seller;
|
||||
allowedPurchaser = NodeType.Customer;
|
||||
this.nodeType = NodeType.Distributor;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -9,5 +9,6 @@ public class Processor extends RetailerNodeBase {
|
||||
super(startMoney);
|
||||
allowedSeller = NodeType.Farmer;
|
||||
allowedPurchaser = NodeType.Seller;
|
||||
this.nodeType = NodeType.Processor;
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,5 +9,6 @@ public class Seller extends RetailerNodeBase {
|
||||
super(startMoney);
|
||||
allowedSeller = NodeType.Processor;
|
||||
allowedPurchaser = NodeType.Distributor;
|
||||
this.nodeType = NodeType.Seller;
|
||||
}
|
||||
}
|
||||
|
||||
@ -27,21 +27,24 @@ public class NodeBase implements ChannelObserver {
|
||||
private final List<Transaction> 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) {
|
||||
/**
|
||||
* 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) {
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user