Merge branch 'Funktion-zur-automatischen-Margenermittlung' into dev

This commit is contained in:
Shan Ruhhammer
2023-06-25 03:43:40 +02:00
10 changed files with 315 additions and 14 deletions

View File

@@ -12,6 +12,7 @@ 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.ProductCatalogue;
import de.rwu.easydrop.service.processing.OrderManager;
import de.rwu.easydrop.service.retriever.CatalogueRetriever;
import de.rwu.easydrop.service.retriever.ProductRetriever;
import de.rwu.easydrop.service.writer.CatalogueWriter;
@@ -58,5 +59,7 @@ public final class Main {
String pCatStr = pCat.toString();
LOGGER.info(pCatStr);
}
OrderManager.createOrders(pCats);
}
}

View File

@@ -0,0 +1,27 @@
package de.rwu.easydrop.exception;
/**
* Exception that signifies the data of a Product are invalid.
*
* @since 0.3.0
*/
public class InvalidCatalogueException extends RuntimeException {
/**
* Throws an exception that signifies the data of a Product are invalid.
*
* @param message
*/
public InvalidCatalogueException(final String message) {
super(message);
}
/**
* Throws an exception that signifies the data of a Product are invalid.
*
* @param message
* @param cause
*/
public InvalidCatalogueException(final String message, final Throwable cause) {
super(message, cause);
}
}

View File

@@ -0,0 +1,31 @@
package de.rwu.easydrop.model;
import lombok.Data;
/**
* Associates two related Products to one another.
*
* @since 0.3.0
*/
@Data
public class ProductPair {
/**
* The first product.
*/
private final Product product1;
/**
* The second product.
*/
private final Product product2;
/**
* Constructs a Product Pair.
*
* @param a First product
* @param b Second product
*/
public ProductPair(final Product a, final Product b) {
this.product1 = a;
this.product2 = b;
}
}

View File

@@ -1,10 +1,87 @@
package de.rwu.easydrop.service.processing;
/**
* Processes dropshipping orders.
*
* TODO implement
*/
public class OrderManager {
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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);
// #12: 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);
}
}

View File

@@ -25,7 +25,7 @@ public final class FormattingUtil {
* @return formatted price
*/
public static String formatEuro(final double amount) {
return String.format(Locale.GERMAN, "%,.2f", amount) + " ";
return String.format(Locale.GERMAN, "%,.2f", amount) + " Euro";
}
/**