Added channel and nodes parsing into main

Small refactoring
This commit is contained in:
Daniel Poustka 2020-12-25 20:47:39 +01:00
parent 88543d0c68
commit 643bf23c0b
3 changed files with 64 additions and 27 deletions

View File

@ -1,11 +1,12 @@
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.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.node.NodeBase;
import cz.cvut.fel.sit.omo.foodchain.parser.ConfigurationParser; 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.ParserResult;
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 org.xml.sax.SAXException; import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
@ -18,18 +19,31 @@ public class FoodChain
private static FactoryManager factoryManager = new FactoryManager(); private static FactoryManager factoryManager = new FactoryManager();
public static void main(String[] args) public static void main(String[] args)
{
parseChannels();
parseNodes();
}
public static FactoryManager getFactoryManager()
{
return factoryManager;
}
// todo maybe some refactoring needed?
// add other entities
public static void parseChannels()
{ {
try try
{ {
// parse channel configuration // parse channel configuration
ConfigurationParser.parseDocument("channels"); ConfigurationParser.parseDocument("channels.xml");
ParserResult parserResult = ConfigurationParser.getElements("channels"); ParserResult parserResult = ConfigurationParser.getElements("channels");
//todo need getter for elemtens...
List<ParserResultElement> elements = parserResult.getElements(); List<ParserResultElement> elements = parserResult.getElements();
for (ParserResultElement el : elements) for (ParserResultElement el : elements)
{ {
ChannelBase c = getFactoryManager().getChannelFactoryInstance().makeChannel(el); ChannelBase res = getFactoryManager().getChannelFactoryInstance().makeChannel(el);
getFactoryManager().getPoolDatabaseInstance().getChannelPoolInstance().add(c); getFactoryManager().getPoolDatabaseInstance().getChannelPoolInstance().add(res);
} }
} }
@ -41,12 +55,29 @@ public class FoodChain
{ {
System.err.println(e.getMessage()); System.err.println(e.getMessage());
} }
} }
public static void parseNodes()
public static FactoryManager getFactoryManager()
{ {
return factoryManager; try
{
// parse channel configuration
ConfigurationParser.parseDocument("nodes.xml");
ParserResult parserResult = ConfigurationParser.getElements("nodes");
List<ParserResultElement> elements = parserResult.getElements();
for (ParserResultElement el : elements)
{
NodeBase res = getFactoryManager().getNodeFactoryInstance().makeNode(el);
getFactoryManager().getPoolDatabaseInstance().getNodePoolInstance().add(res);
}
}
catch (ParserConfigurationException | SAXException | IOException e)
{
System.err.println(e.getMessage());
}
catch (Exception e)
{
System.err.println(e.getMessage());
}
} }
} }

View File

@ -21,7 +21,7 @@ public class ProductFactory
String name = input.getAttribute("name"); String name = input.getAttribute("name");
ProductType productType = ProductType.valueOf(input.getChild("type")); ProductType productType = ProductType.valueOf(input.getChild("type"));
//ProductState productState = ProductState.valueOf(input.getChild("state")); //ProductState productState = ProductState.valueOf(input.getChild("state"));
int price = Integer.getInteger(input.getChild("price")); double price = Double.parseDouble(input.getChild("price"));
//p = new Product(name, price, productType, productState); //p = new Product(name, price, productType, productState);
p = new Product(name, productType, price); p = new Product(name, productType, price);

View File

@ -16,46 +16,46 @@ public class Product
/** /**
* Creates Product with default state AVAILABLE * Creates Product with default state AVAILABLE
* @param name name of product * @param name name of product
* @param productType type of product * @param productType type of product
*/ */
public Product(String name, ProductType productType) public Product(String name, ProductType productType)
{ {
this.name = name; this.name = name;
this.productType = productType; this.productType = productType;
this.productState = ProductState.AVAILABLE; this.productState = ProductState.AVAILABLE;
this.money = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(0).create(); this.money = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(0).create();
} }
//TODO money int constructor //TODO money int constructor
public Product(String name, ProductType productType, MonetaryAmount money) public Product(String name, ProductType productType, Double money)
{ {
this.name = name; this.name = name;
this.productType = productType; this.productType = productType;
this.productState = ProductState.AVAILABLE; this.productState = ProductState.AVAILABLE;
this.money = money; this.money = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(money).create();
} }
/** /**
* Creates Product * Creates Product
* @param name name of product * @param name name of product
* @param productType type of product * @param productType type of product
* @param productState state of product * @param productState state of product
*/ */
public Product(String name, ProductType productType, ProductState productState) public Product(String name, ProductType productType, ProductState productState)
{ {
this.name = name; this.name = name;
this.productType = productType; this.productType = productType;
this.productState = productState; this.productState = productState;
this.money = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(0).create(); this.money = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(0).create();
} }
public Product(String name, ProductType productType, ProductState productState, MonetaryAmount money) public Product(String name, ProductType productType, ProductState productState, Double money)
{ {
this.name = name; this.name = name;
this.productType = productType; this.productType = productType;
this.productState = productState; this.productState = productState;
this.money = money; this.money = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(money).create();
} }
// "State machine methods" // "State machine methods"
@ -73,7 +73,7 @@ public class Product
*/ */
public void setStateToReadyForDispatch() public void setStateToReadyForDispatch()
{ {
if(this.productState.equals(ProductState.AVAILABLE)) if (this.productState.equals(ProductState.AVAILABLE))
this.productState = ProductState.READY_FOR_DISPATCH; this.productState = ProductState.READY_FOR_DISPATCH;
else else
throw new IllegalStateException("Cannot set state READY_FOR_DISPATCH. Product is not AVAILABLE!"); throw new IllegalStateException("Cannot set state READY_FOR_DISPATCH. Product is not AVAILABLE!");
@ -84,7 +84,7 @@ public class Product
*/ */
public void setStateToInTransit() public void setStateToInTransit()
{ {
if(this.productState.equals(ProductState.READY_FOR_DISPATCH)) if (this.productState.equals(ProductState.READY_FOR_DISPATCH))
this.productState = ProductState.IN_TRANSIT; this.productState = ProductState.IN_TRANSIT;
else else
throw new IllegalStateException("Cannot set state IN_TRANSIT. Product is not READY_FOR_DISPATCH!"); throw new IllegalStateException("Cannot set state IN_TRANSIT. Product is not READY_FOR_DISPATCH!");
@ -95,7 +95,7 @@ public class Product
*/ */
public void setStateToReadyForReception() public void setStateToReadyForReception()
{ {
if(this.productState.equals(ProductState.IN_TRANSIT)) if (this.productState.equals(ProductState.IN_TRANSIT))
this.productState = ProductState.READY_FOR_RECEPTION; this.productState = ProductState.READY_FOR_RECEPTION;
else else
throw new IllegalStateException("Cannot set state READY_FOR_RECEPTION. Product has not been in transit!"); throw new IllegalStateException("Cannot set state READY_FOR_RECEPTION. Product has not been in transit!");
@ -145,7 +145,13 @@ public class Product
this.currentOperation = currentOperation; this.currentOperation = currentOperation;
} }
public MonetaryAmount getMoney() { return money; } public MonetaryAmount getMoney()
{
return money;
}
public void setMoney(MonetaryAmount money) { this.money = money; } public void setMoney(MonetaryAmount money)
{
this.money = money;
}
} }