Added NodeFactory
This commit is contained in:
parent
bce2b74ef2
commit
71ef11f7e2
@ -3,14 +3,10 @@ package cz.cvut.fel.sit.omo.foodchain.factory;
|
|||||||
|
|
||||||
import cz.cvut.fel.sit.omo.foodchain.channel.*;
|
import cz.cvut.fel.sit.omo.foodchain.channel.*;
|
||||||
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.product.ProductType;
|
||||||
|
|
||||||
public class ChannelFactory
|
public class ChannelFactory
|
||||||
{
|
{
|
||||||
private enum ChannelType
|
|
||||||
{
|
|
||||||
Fruit, Vegetable, Meat, Other;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates channel based on channel type (Fruit, Vegetable, Meat, Other)
|
* Creates channel based on channel type (Fruit, Vegetable, Meat, Other)
|
||||||
* @param input data from channel is created
|
* @param input data from channel is created
|
||||||
@ -21,7 +17,7 @@ public class ChannelFactory
|
|||||||
ChannelBase c = null;
|
ChannelBase c = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ChannelType channelType = ChannelType.valueOf(input.getAttribute("channel_type"));
|
ProductType channelType = ProductType.valueOf(input.getAttribute("channel_type"));
|
||||||
c = makeChannelFromChannelType(channelType);
|
c = makeChannelFromChannelType(channelType);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -36,17 +32,17 @@ public class ChannelFactory
|
|||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ChannelBase makeChannelFromChannelType(ChannelType typeOfChannel)
|
private ChannelBase makeChannelFromChannelType(ProductType typeOfChannel)
|
||||||
{
|
{
|
||||||
switch (typeOfChannel)
|
switch (typeOfChannel)
|
||||||
{
|
{
|
||||||
case Fruit:
|
case FRUIT:
|
||||||
return new FruitChannel();
|
return new FruitChannel();
|
||||||
case Vegetable:
|
case VEGETABLE:
|
||||||
return new VegetableChannel();
|
return new VegetableChannel();
|
||||||
case Meat:
|
case MEAT:
|
||||||
return new MeatChannel();
|
return new MeatChannel();
|
||||||
case Other:
|
case OTHER:
|
||||||
return new OtherChannel();
|
return new OtherChannel();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@ -1,6 +1,78 @@
|
|||||||
package cz.cvut.fel.sit.omo.foodchain.factory;
|
package cz.cvut.fel.sit.omo.foodchain.factory;
|
||||||
|
|
||||||
|
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 javax.money.Monetary;
|
||||||
|
import javax.money.MonetaryAmount;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public class NodeFactory
|
public class NodeFactory
|
||||||
{
|
{
|
||||||
//make
|
|
||||||
|
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 = "";
|
||||||
|
MonetaryAmount startMoney = null;
|
||||||
|
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();
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (IllegalArgumentException e)
|
||||||
|
{
|
||||||
|
System.err.println("! Wrong type of enum in NodeFactory: " + e);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
System.err.println("! Fatal error in NodeFactory: " + e);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (Objects.requireNonNull(nodeType))
|
||||||
|
{
|
||||||
|
case Farmer:
|
||||||
|
n = new Farmer();
|
||||||
|
break;
|
||||||
|
case Processor:
|
||||||
|
n = new Processor();
|
||||||
|
break;
|
||||||
|
case Distributor:
|
||||||
|
n = new Distributor();
|
||||||
|
break;
|
||||||
|
case Seller:
|
||||||
|
n = new Seller();
|
||||||
|
break;
|
||||||
|
case Customer:
|
||||||
|
n = new Customer();
|
||||||
|
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,5 +1,27 @@
|
|||||||
package cz.cvut.fel.sit.omo.foodchain.factory;
|
package cz.cvut.fel.sit.omo.foodchain.factory;
|
||||||
|
|
||||||
|
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
|
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