#11 Rewrote, moved code to OrderManager
This commit is contained in:
@@ -1,96 +0,0 @@
|
|||||||
package de.rwu.easydrop;
|
|
||||||
|
|
||||||
import java.net.MalformedURLException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.naming.ConfigurationException;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.sqlite.SQLiteDataSource;
|
|
||||||
|
|
||||||
import de.rwu.easydrop.api.client.DataSourceFactory;
|
|
||||||
import de.rwu.easydrop.data.connector.AbstractProductPersistence;
|
|
||||||
import de.rwu.easydrop.data.connector.SQLiteConnector;
|
|
||||||
import de.rwu.easydrop.model.Product;
|
|
||||||
import de.rwu.easydrop.model.ProductCatalogue;
|
|
||||||
import de.rwu.easydrop.service.retriever.CatalogueRetriever;
|
|
||||||
import de.rwu.easydrop.service.retriever.ProductRetriever;
|
|
||||||
import de.rwu.easydrop.service.writer.CatalogueWriter;
|
|
||||||
import de.rwu.easydrop.util.Config;
|
|
||||||
import de.rwu.easydrop.util.ProductsConfig;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Diese Klasse PriceFetcher enthält die Methode getProductPrice,
|
|
||||||
* die den Preis eines Produkts von den jeweiligen API's abruft.
|
|
||||||
*/
|
|
||||||
public final class PriceFetcher {
|
|
||||||
static List<Double> api1Prices = new ArrayList<>();
|
|
||||||
static List<Double> api2Prices = new ArrayList<>();
|
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(PriceFetcher.class);
|
|
||||||
|
|
||||||
//Methode, um Preise der Prdukte zu vergleichen -> MArgen ermitteln
|
|
||||||
public static void comparePrices() throws ConfigurationException {
|
|
||||||
List<Double> api1Prices = new ArrayList<>();
|
|
||||||
List<Double> api2Prices = new ArrayList<>();
|
|
||||||
|
|
||||||
for (int i = 0; i < api1Prices.size(); i++) {
|
|
||||||
double api1Price = api1Prices.get(i);
|
|
||||||
double api2Price = api2Prices.get(i);
|
|
||||||
String productId = "Product " + (i + 1);
|
|
||||||
|
|
||||||
if (api1Price < api2Price) {
|
|
||||||
double priceDifference = api2Price - api1Price;
|
|
||||||
LOGGER.info("API 1 hat einen günstigeren Preis für " + productId +
|
|
||||||
". Der Preisunterschied beträgt: " + priceDifference);
|
|
||||||
} else if (api2Price < api1Price) {
|
|
||||||
double priceDifference = api1Price - api2Price;
|
|
||||||
LOGGER.info("API 2 hat einen günstigeren Preis für " + productId +
|
|
||||||
". Der Preisunterschied beträgt: " + priceDifference);
|
|
||||||
} else {
|
|
||||||
LOGGER.info("Beide APIs haben den gleichen Preis für " + productId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Methode, um Preise der Produkte zu ermitteln
|
|
||||||
public static void getProductPrice(final String[] args) throws ConfigurationException{
|
|
||||||
|
|
||||||
Config config = Config.getInstance();
|
|
||||||
ProductsConfig pConfig = ProductsConfig.getInstance();
|
|
||||||
DataSourceFactory dataSourceFactory = new DataSourceFactory(config);
|
|
||||||
ProductRetriever retriever = new ProductRetriever(dataSourceFactory);
|
|
||||||
CatalogueRetriever catRetriever = new CatalogueRetriever(pConfig, retriever);
|
|
||||||
AbstractProductPersistence db = new SQLiteConnector(new SQLiteDataSource());
|
|
||||||
CatalogueWriter catWriter = new CatalogueWriter(db);
|
|
||||||
|
|
||||||
catRetriever.loadCatalogues();
|
|
||||||
List<ProductCatalogue> pCats = catRetriever.getProductCatalogues();
|
|
||||||
catWriter.writeCatalogues(pCats);
|
|
||||||
|
|
||||||
for (ProductCatalogue pCat : pCats) {
|
|
||||||
String pCatStr = pCat.toString();
|
|
||||||
LOGGER.info(pCatStr);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (ProductCatalogue pCat : pCats) {
|
|
||||||
List<Product> products = pCat.getProducts();
|
|
||||||
|
|
||||||
for (Product product : products) {
|
|
||||||
String productId = product.getProductId();
|
|
||||||
double price = product.getCurrentPrice();
|
|
||||||
LOGGER.info("Product ID: " + productId + ", Price: " + price);
|
|
||||||
if (productId == "B096Y2TYKV") {
|
|
||||||
api1Prices.add(price);
|
|
||||||
} else if (productId == "Gigabyte GeForce RTX 3060") {
|
|
||||||
api2Prices.add(price);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,10 +1,88 @@
|
|||||||
package de.rwu.easydrop.service.processing;
|
package de.rwu.easydrop.service.processing;
|
||||||
|
|
||||||
/**
|
import java.util.List;
|
||||||
* Processes dropshipping orders.
|
|
||||||
*
|
|
||||||
* TODO implement
|
|
||||||
*/
|
|
||||||
public class OrderManager {
|
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import de.rwu.easydrop.Main;
|
||||||
|
import de.rwu.easydrop.exception.InvalidCatalogueException;
|
||||||
|
import de.rwu.easydrop.model.Product;
|
||||||
|
import de.rwu.easydrop.model.ProductCatalogue;
|
||||||
|
import de.rwu.easydrop.model.ProductPair;
|
||||||
|
import de.rwu.easydrop.util.FormattingUtil;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates dropshipping orders based on price margin.
|
||||||
|
*
|
||||||
|
* @since 0.3.0
|
||||||
|
*/
|
||||||
|
public final class OrderManager {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Temporary logging instance.
|
||||||
|
*/
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(OrderManager.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Private constructor to prevent unwanted instantiation.
|
||||||
|
*
|
||||||
|
* @throws UnsupportedOperationException always
|
||||||
|
*/
|
||||||
|
private OrderManager() throws UnsupportedOperationException {
|
||||||
|
throw new UnsupportedOperationException("This is a stateless class, don't instantiate it.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates orders for products with sufficient margin.
|
||||||
|
*
|
||||||
|
* @param pCats Product Catalogues
|
||||||
|
*/
|
||||||
|
public static void createOrders(final List<ProductCatalogue> pCats) {
|
||||||
|
for (ProductCatalogue pCat : pCats) {
|
||||||
|
ProductPair pair = getHighestMarginProducts(pCat);
|
||||||
|
|
||||||
|
// TODO: create actual orders/transactions, remove logger
|
||||||
|
double margin = pair.getProduct2().getCurrentPrice() -
|
||||||
|
pair.getProduct1().getCurrentPrice();
|
||||||
|
String marginFormatted = FormattingUtil.formatEuro(margin);
|
||||||
|
LOGGER.info("{}: Margin {} ({} to {})",
|
||||||
|
pCat.getProductName(),
|
||||||
|
marginFormatted,
|
||||||
|
pair.getProduct1().getDataOrigin(),
|
||||||
|
pair.getProduct2().getDataOrigin());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the cheapest and most expensive product of a catalogue to guarantee
|
||||||
|
* the biggest margin as a pair.
|
||||||
|
*
|
||||||
|
* @param pCat Product Catalogue
|
||||||
|
* @return Cheapest Product, Most Expensive Product as a Product Pair
|
||||||
|
*/
|
||||||
|
public static ProductPair getHighestMarginProducts(final ProductCatalogue pCat) {
|
||||||
|
if (pCat.getProducts().size() < 2) {
|
||||||
|
throw new InvalidCatalogueException("Product Catalogue holds less than 2 products!");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize indexes
|
||||||
|
Product cheapestProduct = pCat.getProducts().get(0);
|
||||||
|
Product mostExpensiveProduct = pCat.getProducts().get(0);
|
||||||
|
|
||||||
|
for (Product product : pCat.getProducts()) {
|
||||||
|
if (product.getCurrentPrice() < cheapestProduct.getCurrentPrice()) {
|
||||||
|
cheapestProduct = product;
|
||||||
|
}
|
||||||
|
if (product.getCurrentPrice() > mostExpensiveProduct.getCurrentPrice()) {
|
||||||
|
mostExpensiveProduct = product;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cheapestProduct.getCurrentPrice() == mostExpensiveProduct.getCurrentPrice()) {
|
||||||
|
throw new InvalidCatalogueException("Price margin is zero!");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ProductPair(cheapestProduct, mostExpensiveProduct);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user