Merge branch 'feature/factory_impl' into develop
This commit is contained in:
commit
9d4bb41b54
@ -1,16 +1,52 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain;
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.channel.ChannelBase;
|
||||
import cz.cvut.fel.sit.omo.foodchain.factory.ChannelFactory;
|
||||
import cz.cvut.fel.sit.omo.foodchain.factory.FactoryManager;
|
||||
import cz.cvut.fel.sit.omo.foodchain.parser.ConfigurationParser;
|
||||
import cz.cvut.fel.sit.omo.foodchain.parser.ParserResult;
|
||||
import cz.cvut.fel.sit.omo.foodchain.parser.ParserResultElement;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
public class FoodChain {
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public class FoodChain
|
||||
{
|
||||
|
||||
private static FactoryManager factoryManager = new FactoryManager();
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
// parse channel configuration
|
||||
ConfigurationParser.parseDocument("channels");
|
||||
ParserResult parserResult = ConfigurationParser.getElements("channels");
|
||||
//todo need getter for elemtens...
|
||||
List<ParserResultElement> elements = parserResult.getElements();
|
||||
for (ParserResultElement el : elements)
|
||||
{
|
||||
ChannelBase c = getFactoryManager().getChannelFactoryInstance().makeChannel(el);
|
||||
getFactoryManager().getPoolDatabaseInstance().getChannelPoolInstance().add(c);
|
||||
}
|
||||
|
||||
}
|
||||
catch (ParserConfigurationException | SAXException | IOException e)
|
||||
{
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static FactoryManager getFactoryManager() {
|
||||
public static FactoryManager getFactoryManager()
|
||||
{
|
||||
return factoryManager;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,50 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.factory;
|
||||
|
||||
public class ChannelFactory {
|
||||
|
||||
import cz.cvut.fel.sit.omo.foodchain.channel.*;
|
||||
import cz.cvut.fel.sit.omo.foodchain.parser.ParserResultElement;
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
|
||||
|
||||
public class ChannelFactory
|
||||
{
|
||||
/**
|
||||
* Creates channel based on channel type (Fruit, Vegetable, Meat, Other)
|
||||
* @param input data from channel is created
|
||||
* @return created Channel (FruitChannel, VegetableChannel, MeatChannel, OtherChannel)
|
||||
*/
|
||||
public ChannelBase makeChannel(ParserResultElement input)
|
||||
{
|
||||
ChannelBase c = null;
|
||||
try
|
||||
{
|
||||
ProductType channelType = ProductType.valueOf(input.getAttribute("type"));
|
||||
c = makeChannelFromChannelType(channelType);
|
||||
|
||||
}
|
||||
catch (IllegalArgumentException e)
|
||||
{
|
||||
System.err.println("! Wrong type of enum in ChannelFactory: " + e);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.err.println("! Fatal error in ChannelFactory: " + e);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
private ChannelBase makeChannelFromChannelType(ProductType typeOfChannel)
|
||||
{
|
||||
switch (typeOfChannel)
|
||||
{
|
||||
case FRUIT:
|
||||
return new FruitChannel();
|
||||
case VEGETABLE:
|
||||
return new VegetableChannel();
|
||||
case MEAT:
|
||||
return new MeatChannel();
|
||||
case OTHER:
|
||||
return new OtherChannel();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,48 +5,68 @@ import cz.cvut.fel.sit.omo.foodchain.util.Singleton;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
public class FactoryManager {
|
||||
|
||||
public NodeFactory getNodeFactoryInstance() {
|
||||
try {
|
||||
public class FactoryManager
|
||||
{
|
||||
public NodeFactory getNodeFactoryInstance()
|
||||
{
|
||||
try
|
||||
{
|
||||
return Singleton.getInstance(NodeFactory.class);
|
||||
} catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
|
||||
}
|
||||
catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public ChannelFactory getChannelFactoryInstance() {
|
||||
try {
|
||||
public ChannelFactory getChannelFactoryInstance()
|
||||
{
|
||||
try
|
||||
{
|
||||
return Singleton.getInstance(ChannelFactory.class);
|
||||
} catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
|
||||
}
|
||||
catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public ScenarioFactory getScenarioFactoryInstance() {
|
||||
try {
|
||||
public ScenarioFactory getScenarioFactoryInstance()
|
||||
{
|
||||
try
|
||||
{
|
||||
return Singleton.getInstance(ScenarioFactory.class);
|
||||
} catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
|
||||
}
|
||||
catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public ProductFactory getProductFactoryInstance() {
|
||||
try {
|
||||
public ProductFactory getProductFactoryInstance()
|
||||
{
|
||||
try
|
||||
{
|
||||
return Singleton.getInstance(ProductFactory.class);
|
||||
} catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
|
||||
}
|
||||
catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public PoolDatabase getPoolDatabaseInstance() {
|
||||
try {
|
||||
public PoolDatabase getPoolDatabaseInstance()
|
||||
{
|
||||
try
|
||||
{
|
||||
return Singleton.getInstance(PoolDatabase.class);
|
||||
} catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
|
||||
}
|
||||
catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
|
||||
@ -1,4 +1,76 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.factory;
|
||||
|
||||
public class NodeFactory {
|
||||
import cz.cvut.fel.sit.omo.foodchain.FoodChain;
|
||||
import cz.cvut.fel.sit.omo.foodchain.node.*;
|
||||
import cz.cvut.fel.sit.omo.foodchain.parser.ParserResultElement;
|
||||
import cz.cvut.fel.sit.omo.foodchain.pool.ChannelPool;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class NodeFactory
|
||||
{
|
||||
|
||||
private enum NodeType
|
||||
{
|
||||
Farmer, Processor, Distributor, Seller, Customer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates Node based on input ParserResultElement
|
||||
* @param input data from Node is created
|
||||
* @return created Node
|
||||
*/
|
||||
public NodeBase makeNode(ParserResultElement input)
|
||||
{
|
||||
NodeBase n = null;
|
||||
NodeType nodeType = null;
|
||||
String subsChannels = "";
|
||||
int startMoney = 0;
|
||||
|
||||
try
|
||||
{
|
||||
nodeType = NodeType.valueOf(input.getAttribute("type"));
|
||||
|
||||
subsChannels = input.getChild("subscribed_channels");
|
||||
startMoney = Integer.getInteger(input.getChild("money"));
|
||||
}
|
||||
catch (IllegalArgumentException e)
|
||||
{
|
||||
System.err.println("! Wrong type of enum in NodeFactory: " + e.getMessage());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.err.println("! Fatal error in NodeFactory: " + e.getMessage());
|
||||
}
|
||||
|
||||
switch (Objects.requireNonNull(nodeType))
|
||||
{
|
||||
case Farmer:
|
||||
n = new Farmer(startMoney);
|
||||
break;
|
||||
case Processor:
|
||||
n = new Processor(startMoney);
|
||||
break;
|
||||
case Distributor:
|
||||
n = new Distributor(startMoney);
|
||||
break;
|
||||
case Seller:
|
||||
n = new Seller(startMoney);
|
||||
break;
|
||||
case Customer:
|
||||
n = new Customer(startMoney);
|
||||
break;
|
||||
}
|
||||
|
||||
String[] subs = subsChannels.split(";");
|
||||
//todo Node subscription to channels by parser data
|
||||
|
||||
for (String s : subs)
|
||||
{
|
||||
ChannelPool temp = FoodChain.getFactoryManager().getPoolDatabaseInstance().getChannelPoolInstance();
|
||||
n.subscribeToChannel(temp.findByName(s));
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,39 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.factory;
|
||||
|
||||
public class ProductFactory {
|
||||
import cz.cvut.fel.sit.omo.foodchain.parser.ParserResultElement;
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.Product;
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.ProductState;
|
||||
import cz.cvut.fel.sit.omo.foodchain.product.ProductType;
|
||||
|
||||
|
||||
public class ProductFactory
|
||||
{
|
||||
/**
|
||||
* Creates Product based on input ParserResultElement
|
||||
* @param input data from Product is created
|
||||
* @return created Product
|
||||
*/
|
||||
public Product makeProduct(ParserResultElement input)
|
||||
{
|
||||
Product p = null;
|
||||
try
|
||||
{
|
||||
String name = input.getAttribute("name");
|
||||
ProductType productType = ProductType.valueOf(input.getChild("type"));
|
||||
//ProductState productState = ProductState.valueOf(input.getChild("state"));
|
||||
int price = Integer.getInteger(input.getChild("price"));
|
||||
|
||||
//p = new Product(name, price, productType, productState);
|
||||
p = new Product(name, productType, price);
|
||||
}
|
||||
catch (IllegalArgumentException e)
|
||||
{
|
||||
System.err.println("! Wrong type of enum in ProductFactory: " + e);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.err.println("! Fatal error in ProductFactory: " + e);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,27 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.factory;
|
||||
|
||||
public class ScenarioFactory {
|
||||
import cz.cvut.fel.sit.omo.foodchain.FoodChain;
|
||||
import cz.cvut.fel.sit.omo.foodchain.node.*;
|
||||
import cz.cvut.fel.sit.omo.foodchain.parser.ParserResultElement;
|
||||
import cz.cvut.fel.sit.omo.foodchain.pool.ChannelPool;
|
||||
import cz.cvut.fel.sit.omo.foodchain.scenario.Scenario;
|
||||
|
||||
import javax.money.Monetary;
|
||||
import javax.money.MonetaryAmount;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ScenarioFactory
|
||||
{
|
||||
/**
|
||||
* Creates Scenario based on input ParserResultElement
|
||||
* @param input data from Scenario is created
|
||||
* @return created Scenario
|
||||
*/
|
||||
public Scenario makeScenario(ParserResultElement input)
|
||||
{
|
||||
Scenario s = null;
|
||||
|
||||
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user