From e3377c7b5924085f5ae05c21e7a78a70408d5cad Mon Sep 17 00:00:00 2001 From: Leonie Eitze Date: Mon, 26 Jun 2023 10:03:03 +0200 Subject: [PATCH] Did Checkstyle improvements on OfferProv + Ident --- .../de/rwu/easydrop/core/OfferIdentifier.java | 57 +++++++---- .../rwu/easydrop/core/OfferProvisioner.java | 99 ++++++++++--------- 2 files changed, 90 insertions(+), 66 deletions(-) diff --git a/src/main/java/de/rwu/easydrop/core/OfferIdentifier.java b/src/main/java/de/rwu/easydrop/core/OfferIdentifier.java index 4f3ee39..b240d0f 100644 --- a/src/main/java/de/rwu/easydrop/core/OfferIdentifier.java +++ b/src/main/java/de/rwu/easydrop/core/OfferIdentifier.java @@ -20,58 +20,73 @@ public class OfferIdentifier { * Logger for main process. */ private static final Logger LOGGER = LoggerFactory.getLogger(OfferIdentifier.class); - - OfferRetriever offerRetriever; - + /** + * OfferRetriever gets the offer from persistence. + */ + private OfferRetriever offerRetriever; + /** + * OfferIdentifier identifies offers that are + * feasible to place on a target platform based on + * their margin. + * @throws ConfigurationException + */ public OfferIdentifier() throws ConfigurationException{ this.offerRetriever = new OfferRetriever(); } - public List runIdentifier(List pCats) { + /** + * runIdentifier calls the price function that decides + * if it is feasible to dropship products and if so, + * at which margin. + * @param pCats + * @return newOffers + */ + public List runIdentifier(final List pCats) { List identifiedOffers = new ArrayList<>(); - // Here we call the price function that decides if is feasible to dropship the product and if, at which - // margin for (ProductCatalogue pCat : pCats) { - try{ + try { // Call price finder for all catalogue - ProductPair pair = OrderManager.getHighestMarginProducts(pCat); + ProductPair pair = OrderManager.getHighestMarginProducts(pCat); Offer possibleOffer = new Offer(); possibleOffer.setCheckDate(new Date()); possibleOffer.setSourceProduct(pair.getProduct1()); possibleOffer.setSaleProduct(pair.getProduct2()); - identifiedOffers.add(possibleOffer); + identifiedOffers.add(possibleOffer); LOGGER.info( - "Identified offer " + - pair.getProduct1().getProductId() + - " -> " + + "Identified offer " + + + pair.getProduct1().getProductId() + + + " -> " + + pair.getProduct2().getProductId() - ); + ); // Following fields will be set if offer confirmed // creationDate, offerId - // Following fields will be set if offer needs update // upDate } - catch(InvalidCatalogueException e) { + catch (InvalidCatalogueException e) { // if no margin, getHighestMarginProducts will throw System.out.print("product has no margin"); } } - List newOffers = new ArrayList<>(); List existingOffers = offerRetriever.loadOffers(); - for(Offer identifiedOffer: identifiedOffers) { + for (Offer identifiedOffer: identifiedOffers) { boolean isNew = true; - for(Offer existingOffer: existingOffers) { - if(existingOffer.getSourceProduct().getProductId().equals(identifiedOffer.getSourceProduct().getProductId())) { + for (Offer existingOffer: existingOffers) { + if ( + existingOffer.getSourceProduct().getProductId(). + equals(identifiedOffer.getSourceProduct().getProductId())) { isNew = false; break; } } - if(isNew) { + if (isNew) { newOffers.add(identifiedOffer); } } return newOffers; - } + } } diff --git a/src/main/java/de/rwu/easydrop/core/OfferProvisioner.java b/src/main/java/de/rwu/easydrop/core/OfferProvisioner.java index 0209812..bd7c063 100644 --- a/src/main/java/de/rwu/easydrop/core/OfferProvisioner.java +++ b/src/main/java/de/rwu/easydrop/core/OfferProvisioner.java @@ -1,6 +1,5 @@ package de.rwu.easydrop.core; -import java.io.IOException; import java.util.List; import de.rwu.easydrop.util.Config; @@ -14,61 +13,72 @@ import de.rwu.easydrop.api.client.EbaySeller; import de.rwu.easydrop.api.dto.ProductDTO; public class OfferProvisioner { - - Config config; - ProductRetriever productRetriever; + /** + * Config. + */ + private Config config; + /** + * Gets the products from persistence. + */ + private ProductRetriever productRetriever; // ProductWriter - AbstractProductPersistence db; - OfferWriter offerWriter; - AmazonSeller amazonSeller; - EbaySeller ebaySeller; + /** + * Is the product databank. + */ + private AbstractProductPersistence db; + /** + * Writes offers into persistence. + */ + private OfferWriter offerWriter; - private void toSeller(Offer offer) throws IllegalArgumentException { - // TODO dataOrigin should use the webshop enum - if(offer.getSaleProduct().getDataOrigin().toLowerCase().equals("ebay")){ - this.ebaySeller.sellProduct(new ProductDTO( - offer.getSaleProduct().getProductId(), - "Amazon") - ); + /** + * Is the API for selling products on Amazon. + */ + private AmazonSeller amazonSeller; + /** + * Is the API for selling products on Ebay. + */ + private EbaySeller ebaySeller; - } else if (offer.getSaleProduct().getDataOrigin().toLowerCase().equals("Amazon")) { - this.amazonSeller.sellProduct(new ProductDTO( + private void toSeller(final Offer offer) throws IllegalArgumentException { + // TODO dataOrigin should use the webshop enum + if (offer.getSaleProduct().getDataOrigin().toLowerCase().equals("ebay")) { + this.ebaySeller.sellProduct(new ProductDTO( offer.getSaleProduct().getProductId(), - "eBay") - ); - } - else { - throw new IllegalArgumentException("Unsupported target plattform"); - } + "Amazon")); + + } else if (offer.getSaleProduct().getDataOrigin().toLowerCase().equals("Amazon")) { + this.amazonSeller.sellProduct(new ProductDTO( + offer.getSaleProduct().getProductId(), + "eBay")); + } else { + throw new IllegalArgumentException("Unsupported target plattform"); + } } - + /** + * Is the class for placing orders on a target platform. + */ + public OfferProvisioner(/* OfferWriter for database? */) { - public OfferProvisioner(/*OfferWriter for database? */) { - - //in die Klasse hardcoden, "webshop" nicht verwenden - //oder eine Konstante anlegen die das halten würde (besser skalierbar) - //die Konstante in model anlegen (in Product, Konstante valid origins) this.offerWriter = new OfferWriter(); this.config = Config.getInstance(); this.amazonSeller = new AmazonSeller( - config.getProperty("AMAZON_API_URL"), - config.getProperty("AMAZON_API_KEY") - ); + config.getProperty("AMAZON_API_URL"), + config.getProperty("AMAZON_API_KEY")); this.ebaySeller = new EbaySeller( - config.getProperty("EBAY_API_URL"), - config.getProperty("EBAY_API_KEY") - ); + config.getProperty("EBAY_API_URL"), + config.getProperty("EBAY_API_KEY")); } - public void runProvisioner(List offersToProvision) { - /* - * Bekommt vom Identifier eine Liste mit Angeboten, die er einstellen soll auf Zielplattformen - * Stellt Angebote bei der Zielplattform ein - * Schreibt eingestellte Angebote in die Datenbank - */ - for(Offer newOffer: offersToProvision){ + /** + * Method for placing orders on a target platform. + * @param offersToProvision + */ + public final void runProvisioner(final List offersToProvision) { + + for (Offer newOffer: offersToProvision){ try { this.toSeller(newOffer); @@ -77,10 +87,9 @@ public class OfferProvisioner { // "duplicate" the product with dataOrigin new platform and merchant = "me" try { offerWriter.writeOfferToPersistence(newOffer); - } - catch (Exception e){ + } catch (Exception e) { System.out.println("Could not write to persistence"); - } + } } catch (IllegalArgumentException e) { System.out.println( "Offer could not be placed, " +