Added config parse of channels to the FoodChain

Fixed stuff in factories
This commit is contained in:
Daniel Poustka 2020-12-25 17:46:55 +01:00
parent 71ef11f7e2
commit 7a92d623ac
4 changed files with 57 additions and 20 deletions

View File

@ -1,16 +1,55 @@
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;
public static void main(String[] args) {
public static void main(String[] args)
{
//todo static factories???
ChannelFactory channelFactory = new ChannelFactory();
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;
}
}

View File

@ -17,7 +17,7 @@ public class ChannelFactory
ChannelBase c = null;
try
{
ProductType channelType = ProductType.valueOf(input.getAttribute("channel_type"));
ProductType channelType = ProductType.valueOf(input.getAttribute("type"));
c = makeChannelFromChannelType(channelType);
}

View File

@ -5,8 +5,6 @@ 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 javax.money.Monetary;
import javax.money.MonetaryAmount;
import java.util.Objects;
public class NodeFactory
@ -27,40 +25,40 @@ public class NodeFactory
NodeBase n = null;
NodeType nodeType = null;
String subsChannels = "";
MonetaryAmount startMoney = null;
int startMoney = 0;
try
{
nodeType = NodeType.valueOf(input.getAttribute("node_type"));
subsChannels = input.getAttribute("subscribed_channels");
double money = Double.parseDouble(input.getAttribute("money"));
startMoney = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(money).create();
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);
System.err.println("! Wrong type of enum in NodeFactory: " + e.getMessage());
}
catch (Exception e)
{
System.err.println("! Fatal error in NodeFactory: " + e);
System.err.println("! Fatal error in NodeFactory: " + e.getMessage());
}
switch (Objects.requireNonNull(nodeType))
{
case Farmer:
n = new Farmer();
n = new Farmer(startMoney);
break;
case Processor:
n = new Processor();
n = new Processor(startMoney);
break;
case Distributor:
n = new Distributor();
n = new Distributor(startMoney);
break;
case Seller:
n = new Seller();
n = new Seller(startMoney);
break;
case Customer:
n = new Customer();
n = new Customer(startMoney);
break;
}

View File

@ -19,8 +19,8 @@ public class ProductFactory
try
{
String name = input.getAttribute("name");
ProductType productType = ProductType.valueOf(input.getAttribute("product_type"));
ProductState productState = ProductState.valueOf(input.getAttribute("product_state"));
ProductType productType = ProductType.valueOf(input.getChild("type"));
ProductState productState = ProductState.valueOf(input.getChild("state"));
p = new Product(name, productType, productState);
}