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;
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.node.NodeBase;
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 cz.cvut.fel.sit.omo.foodchain.pool.ChannelPool;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
@ -18,18 +19,31 @@ public class FoodChain
private static FactoryManager factoryManager = new FactoryManager();
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
{
// parse channel configuration
ConfigurationParser.parseDocument("channels");
ConfigurationParser.parseDocument("channels.xml");
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);
ChannelBase res = getFactoryManager().getChannelFactoryInstance().makeChannel(el);
getFactoryManager().getPoolDatabaseInstance().getChannelPoolInstance().add(res);
}
}
@ -41,12 +55,29 @@ public class FoodChain
{
System.err.println(e.getMessage());
}
}
public static FactoryManager getFactoryManager()
public static void parseNodes()
{
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");
ProductType productType = ProductType.valueOf(input.getChild("type"));
//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, productType, price);

View File

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