Completed scenarios.xml.

Added Customer protection against eating non existing food.
Added blocking of logs from Money.class
Fixed some stuff and bugs.
This commit is contained in:
Daniel Poustka 2021-01-05 21:42:52 +01:00
parent f1e66fc4b7
commit 6c11f188ee
8 changed files with 81 additions and 9 deletions

View File

@ -53,6 +53,23 @@ pass
<steps>
pass
pass
pass
pass
pass
doaction STORE_FRUIT hruška
doaction WRAP_INTO_GIFT_PACKAGE banán
sell VEGETABLE okurka
sell FRUIT banán
doaction WRAP_INTO_GIFT_PACKAGE jablko
sell FRUIT hruška
sell FRUIT jablko
doaction WRAP_INTO_GIFT_PACKAGE paprika
sell VEGETABLE paprika
pass
sell MEAT vepřové
doaction WRAP_INTO_GIFT_PACKAGE kuřecí
sell MEAT hovězí
sell MEAT kuřecí
</steps>
</scenario>
<scenario type="Distributor">
@ -60,6 +77,24 @@ pass
pass
pass
pass
pass
pass
pass
pass
pass
pass
sell VEGETABLE okurka
sell FRUIT banán
pass
sell FRUIT hruška
sell FRUIT jablko
pass
sell VEGETABLE paprika
pass
sell MEAT vepřové
pass
sell MEAT hovězí
sell MEAT kuřecí
</steps>
</scenario>
<scenario type="Customer">
@ -68,6 +103,25 @@ pass
pass
pass
pass
pass
pass
pass
pass
pass
pass
pass
consume
consume
pass
pass
consume
consume
pass
pass
consume
consume
consume
consume
</steps>
</scenario>
</scenarios>

View File

@ -1,6 +1,9 @@
package cz.cvut.fel.sit.omo.foodchain.node;
import cz.cvut.fel.sit.omo.foodchain.product.Product;
import java.util.Random;
import java.util.logging.Level;
/**
* Customer node
@ -15,7 +18,14 @@ public class Customer extends NodeBase {
}
public void consumeProduct() {
if(getStorage().getNumberOfStoredProducts() == 0)
{
LOGGER.log(Level.WARNING, this.nodeType + " tried to consume non existing product.");
return;
}
int i = new Random().nextInt(getStorage().getNumberOfStoredProducts());
Product temp = getStorage().getProducts().get(i);
getStorage().getProducts().remove(i);
LOGGER.log(Level.INFO, this.nodeType + " consumed " + temp.getName() + " " + temp.getProductType());
}
}

View File

@ -53,7 +53,7 @@ public class Storage
*/
public Product getProduct(String name, ProductType type)
{
return products.stream().filter(ch -> ch.getProductType() == type && ch.getName().equals(name)).findFirst().orElse(null);
return products.stream().filter(ch -> (ch.getProductType().equals(type) || type.equals(ProductType.ALL)) && ch.getName().equals(name)).findFirst().orElse(null);
}
public List<Product> getProducts()

View File

@ -50,7 +50,8 @@ public class ProductTransaction extends Transaction {
public String toString() {
return "ProductTransaction{" +
"product=" + product.getName() +
'}';
", creator="+this.getNodeCreator().getNodeType() +
", receiver="+this.getNodeReceiver().getNodeType() + "}";
}
@Override

View File

@ -14,6 +14,7 @@ public class FoodChainReportVisitor implements ReportTextVisitor {
@Override
public String visit(Product product) {
StringBuilder builder = new StringBuilder();
builder.append("-----PRODUCT-----").append(ln)
.append("Name: ").append(product.getName()).append(ln)
.append("Type: ").append(product.getProductType()).append(ln)

View File

@ -7,10 +7,7 @@ import cz.cvut.fel.sit.omo.foodchain.product.Product;
public class SecurityReportVisitor implements ReportTextVisitor {
@Override
public String visit(NodeBase node) {
return
;
}
public String visit(NodeBase node) { return null; }
@Override
public String visit(Product product) {

View File

@ -1,5 +1,12 @@
package cz.cvut.fel.sit.omo.foodchain.util;
import org.javamoney.moneta.Money;
import org.javamoney.moneta.format.CurrencyStyle;
import javax.money.format.AmountFormatQueryBuilder;
import javax.money.format.MonetaryAmountFormat;
import javax.money.format.MonetaryFormats;
import java.util.Locale;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
@ -18,15 +25,16 @@ public class ReportLogger extends Handler
StringBuilder sb = new StringBuilder();
sb.append(record.getMessage());
// Blocking Money logs because spams "Using default formatter toString()." after printing MonetaryAmount in reports
if(record.getSourceClassName().equals(Money.class.getName()))return;
if(record.getLevel().equals(Level.WARNING) || record.getLevel().equals(Level.SEVERE))
{
System.err.println(sb.toString());
//todo add some outprint to report log
}
else
{
System.out.println(sb.toString());
//todo add some outprint to report log
}
}

View File

@ -2,6 +2,7 @@ package cz.cvut.fel.sit.omo.foodchain.util;
import cz.cvut.fel.sit.omo.foodchain.FoodChain;
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.pool.PoolDatabase;
import cz.cvut.fel.sit.omo.foodchain.product.Product;
import cz.cvut.fel.sit.omo.foodchain.scenario.Scenario;
@ -28,7 +29,7 @@ public class Ticker {
PoolDatabase db = FoodChain.getFactoryManager().getPoolDatabaseInstance();
boolean productBool = db.getProductPoolInstance().findAll().stream().filter(Product::tick).count() == 0; // change duration of action
boolean channelBool = db.getChannelPoolInstance().findAll().stream().filter(ChannelBase::tick).count() == 0; // change TTL of requirements
db.getNodePoolInstance().findAll().forEach(n -> n.tick()); // receive transactions
db.getNodePoolInstance().findAll().forEach(NodeBase::tick); // receive transactions
boolean scenarioBool = db.getScenarioPoolInstance().findAll().stream().filter(Scenario::tick).count() == 0; // do next step in scenario
if (channelBool && productBool && scenarioBool) stop();
});