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; 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.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; 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; return factoryManager;
} }
} }

View File

@ -17,7 +17,7 @@ public class ChannelFactory
ChannelBase c = null; ChannelBase c = null;
try try
{ {
ProductType channelType = ProductType.valueOf(input.getAttribute("channel_type")); ProductType channelType = ProductType.valueOf(input.getAttribute("type"));
c = makeChannelFromChannelType(channelType); 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.parser.ParserResultElement;
import cz.cvut.fel.sit.omo.foodchain.pool.ChannelPool; import cz.cvut.fel.sit.omo.foodchain.pool.ChannelPool;
import javax.money.Monetary;
import javax.money.MonetaryAmount;
import java.util.Objects; import java.util.Objects;
public class NodeFactory public class NodeFactory
@ -27,40 +25,40 @@ public class NodeFactory
NodeBase n = null; NodeBase n = null;
NodeType nodeType = null; NodeType nodeType = null;
String subsChannels = ""; String subsChannels = "";
MonetaryAmount startMoney = null; int startMoney = 0;
try try
{ {
nodeType = NodeType.valueOf(input.getAttribute("node_type")); nodeType = NodeType.valueOf(input.getAttribute("type"));
subsChannels = input.getAttribute("subscribed_channels");
double money = Double.parseDouble(input.getAttribute("money"));
startMoney = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(money).create();
subsChannels = input.getChild("subscribed_channels");
startMoney = Integer.getInteger(input.getChild("money"));
} }
catch (IllegalArgumentException e) 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) catch (Exception e)
{ {
System.err.println("! Fatal error in NodeFactory: " + e); System.err.println("! Fatal error in NodeFactory: " + e.getMessage());
} }
switch (Objects.requireNonNull(nodeType)) switch (Objects.requireNonNull(nodeType))
{ {
case Farmer: case Farmer:
n = new Farmer(); n = new Farmer(startMoney);
break; break;
case Processor: case Processor:
n = new Processor(); n = new Processor(startMoney);
break; break;
case Distributor: case Distributor:
n = new Distributor(); n = new Distributor(startMoney);
break; break;
case Seller: case Seller:
n = new Seller(); n = new Seller(startMoney);
break; break;
case Customer: case Customer:
n = new Customer(); n = new Customer(startMoney);
break; break;
} }

View File

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