fixed some issues in method checkOffers

This commit is contained in:
Alexander Maier
2023-06-27 00:14:02 +02:00
parent da7c4e9979
commit 7a19a38aa1

View File

@@ -11,19 +11,20 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
public class OfferReviewer { public class OfferReviewer {
/** /**
* OfferReviewer. * Check all Offers and compare them with the API.
* @return list of all items that need to be changed * @return list of all items that need to be changed
*/ */
public List<Offer> checkOffers(/*OfferReader/retriever for database? */) { public List<Offer> checkOffer(/*OfferReader/retriever for database? */) {
Connection connection = null; Connection connection = null;
Statement statement = null; Statement statement = null;
ResultSet resultSet = null; ResultSet resultSet = null;
List<Product> changedProducts = new ArrayList<>(); List<Offer> changedOffers = new ArrayList<>();
try { try {
// Establish the database connection // Establish the database connection
@@ -33,28 +34,27 @@ public class OfferReviewer {
statement = connection.createStatement(); statement = connection.createStatement();
// Execute the query to retrieve the entries // Execute the query to retrieve the entries
String query = "SELECT dataOrigin, productId, currentPrice FROM table"; String query = "SELECT sourceProduct, saleProduct, creationDate, upDate, "
+ "checkDate, offerId FROM table";
resultSet = statement.executeQuery(query); resultSet = statement.executeQuery(query);
// Process the retrieved entries // Process the retrieved entries
while (resultSet.next()) { while (resultSet.next()) {
String webshop = resultSet.getString("webshop"); Product sourceProduct = (Product) resultSet.getObject("sourceProduct");
String dataOrigin = resultSet.getString("data_origin"); Product saleProduct = (Product) resultSet.getObject("saleProduct");
String productId = resultSet.getString("productId"); java.sql.Date creationDate = resultSet.getDate("creationDate");
double currentPrice = resultSet.getDouble("currentPrice"); Date updateDate = resultSet.getDate("upDate");
String merchant = resultSet.getString("merchant"); Date checkDate = resultSet.getDate("checkDate");
double deliveryPrice = resultSet.getDouble("delivery_price"); String offerId = resultSet.getString("offerId");
boolean available = resultSet.getBoolean("available");
// Call the API to get the current price // Call the API to get the current price
double apiPrice = getPriceFromAPI(productId); double apiPrice = getPriceFromAPI(sourceProduct);
// Compare the prices // Compare the prices
if (currentPrice != apiPrice) { if (saleProduct.getCurrentPrice() != apiPrice) {
// Price has changed, create a Product object and add it to the changedProducts list // Price has changed, create a Product object and add it to the changedProducts list
Product product = new Product(webshop, dataOrigin, productId, currentPrice, Offer offer = new Offer();
merchant, deliveryPrice, available); changedOffers.add(offer);
changedProducts.add(product);
} }
} }
} catch (SQLException e) { } catch (SQLException e) {