Added refusedRequirements

This commit is contained in:
Tomáš Musil 2020-12-27 20:42:02 +01:00
parent 4fba75b0e1
commit 1bea53d11b
3 changed files with 37 additions and 9 deletions

View File

@ -55,6 +55,9 @@ public abstract class ChannelBase implements Observable {
public boolean tick() { public boolean tick() {
requirements.forEach(Requirement::tick); requirements.forEach(Requirement::tick);
try {
requirements.stream().filter(r -> r.getTTL() == 0).forEach(re -> re.getCreator().getProductFromStorage(re.getProductName(), re.getProductType()).setStateToAvailable());
} catch (Exception ignored) {}
requirements = requirements.stream().filter(r -> r.getTTL() > 0).collect(Collectors.toCollection(LinkedList::new)); requirements = requirements.stream().filter(r -> r.getTTL() > 0).collect(Collectors.toCollection(LinkedList::new));
notifyAllObservers(); notifyAllObservers();
return requirements.size() != 0; return requirements.size() != 0;

View File

@ -15,16 +15,14 @@ import org.javamoney.moneta.Money;
import javax.money.MonetaryAmount; import javax.money.MonetaryAmount;
import javax.naming.InsufficientResourcesException; import javax.naming.InsufficientResourcesException;
import java.util.HashMap; import java.util.*;
import java.util.LinkedList;
import java.util.List;
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 List<Transaction> refusedTransactions; private final List<Transaction> refusedTransactions;
private final List<RefusedRequirement> refusedRequirements;
private MonetaryAmount money; private MonetaryAmount money;
@ -37,6 +35,7 @@ public class NodeBase implements ChannelObserver {
this.subscribedChannels = new HashMap<>(); this.subscribedChannels = new HashMap<>();
this.products = new LinkedList<>(); this.products = new LinkedList<>();
this.refusedTransactions = new LinkedList<>(); this.refusedTransactions = new LinkedList<>();
this.refusedRequirements = new LinkedList<>();
this.money = Money.of(0, "USD"); this.money = Money.of(0, "USD");
} }
@ -44,6 +43,7 @@ public class NodeBase implements ChannelObserver {
this.subscribedChannels = new HashMap<>(); this.subscribedChannels = new HashMap<>();
this.products = new LinkedList<>(); this.products = new LinkedList<>();
this.refusedTransactions = new LinkedList<>(); this.refusedTransactions = new LinkedList<>();
this.refusedRequirements = new LinkedList<>();
this.money = Money.of(m, "USD"); this.money = Money.of(m, "USD");
} }
@ -55,7 +55,7 @@ public class NodeBase implements ChannelObserver {
.filter(ch -> ch.getProductType().equals(product.getProductType())) .filter(ch -> ch.getProductType().equals(product.getProductType()))
.findAny().ifPresent(channel -> channel.addTransaction(transaction)); .findAny().ifPresent(channel -> channel.addTransaction(transaction));
} catch (InsufficientResourcesException e) { } catch (InsufficientResourcesException e) {
e.printStackTrace(); System.err.println(e.getMessage());
} }
} }
@ -67,7 +67,7 @@ public class NodeBase implements ChannelObserver {
.filter(ch -> ch.getProductType().equals(prevTransaction.getProduct().getProductType())) .filter(ch -> ch.getProductType().equals(prevTransaction.getProduct().getProductType()))
.findAny().ifPresent(channel -> channel.addTransaction(transaction)); .findAny().ifPresent(channel -> channel.addTransaction(transaction));
} catch (InsufficientResourcesException e) { } catch (InsufficientResourcesException e) {
e.printStackTrace(); System.err.println(e.getMessage());
} }
} }
@ -124,6 +124,16 @@ public class NodeBase implements ChannelObserver {
price = getProductFromStorage(pName, pType).getPrice(); price = getProductFromStorage(pName, pType).getPrice();
} }
catch (NullPointerException e) { catch (NullPointerException e) {
refusedRequirements.add(new RefusedRequirement(tType, pType, pName, price));
throw new InsufficientResourcesException("product unavailable");
}
}
if (tType == TransactionType.SALE) {
try {
getProductFromStorage(pName, pType).setStateToInRequirement();
}
catch (NullPointerException | IllegalStateException e) {
refusedRequirements.add(new RefusedRequirement(tType, pType, pName, price));
throw new InsufficientResourcesException("product unavailable"); throw new InsufficientResourcesException("product unavailable");
} }
} }
@ -239,9 +249,10 @@ public class NodeBase implements ChannelObserver {
} }
boolean requirementCheck(Requirement req) { boolean requirementCheck(Requirement req) {
Product product = getProductFromStorage(req.getProductName(), req.getProductType());
return (req.getTransactionType() == TransactionType.SALE && money.isGreaterThanOrEqualTo(req.getPrice())) || return (req.getTransactionType() == TransactionType.SALE && money.isGreaterThanOrEqualTo(req.getPrice())) ||
(req.getTransactionType() == TransactionType.PURCHASE && getProductFromStorage(req.getProductName(), req.getProductType()) != null && (req.getTransactionType() == TransactionType.PURCHASE && product != null &&
getProductFromStorage(req.getProductName(), req.getProductType()).getProductState() == ProductState.AVAILABLE); ((product.getProductState() == ProductState.AVAILABLE) || product.getProductState() == ProductState.IN_REQUIREMENT));
} }
@Override @Override
@ -261,3 +272,17 @@ public class NodeBase implements ChannelObserver {
//System.out.println(this.getClass().getSimpleName() + ": " + money.getNumber()); //System.out.println(this.getClass().getSimpleName() + ": " + money.getNumber());
} }
} }
class RefusedRequirement {
public final TransactionType tType;
public final ProductType pType;
public final String pName;
public final MonetaryAmount price;
public RefusedRequirement(TransactionType tType, ProductType pType, String pName, MonetaryAmount price) {
this.tType = tType;
this.pType = pType;
this.pName = pName;
this.price = price;
}
}

View File

@ -17,7 +17,7 @@ public class ProductTransaction extends Transaction {
this.product = product; this.product = product;
type = null; type = null;
lastOperation = product.getCurrentOperation(); lastOperation = product.getCurrentOperation();
if (product == null || product.getProductState() != ProductState.AVAILABLE) throw new InsufficientResourcesException("product not available"); if (product == null || product.getProductState() != ProductState.IN_REQUIREMENT) throw new InsufficientResourcesException("product unavailable");
product.setCurrentOperation(this); product.setCurrentOperation(this);
product.setStateToReadyForDispatch(); product.setStateToReadyForDispatch();
creator.getStorage().remove(product); creator.getStorage().remove(product);