added method checkoffer
This commit is contained in:
@@ -1,23 +1,80 @@
|
||||
package de.rwu.easydrop.core;
|
||||
|
||||
import de.rwu.easydrop.model.Offer;
|
||||
import de.rwu.easydrop.model.Product;
|
||||
import de.rwu.easydrop.model.Product.webshop;
|
||||
|
||||
import java.util.List;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class OfferReviewer {
|
||||
|
||||
public OfferReviewer(/*OfferReader/retriever for database? */){
|
||||
/**
|
||||
* OfferReviewer.
|
||||
* @return list of all items that need to be changed
|
||||
*/
|
||||
public List<Offer> checkOffers(/*OfferReader/retriever for database? */) {
|
||||
|
||||
}
|
||||
Connection connection = null;
|
||||
Statement statement = null;
|
||||
ResultSet resultSet = null;
|
||||
List<Product> changedProducts = new ArrayList<>();
|
||||
|
||||
public List<Offer> runReviewer() {
|
||||
/*
|
||||
* Liest eingestellte Angebote in der Datenbank
|
||||
* Prüft Zielplattformen der SourceProducts, ob diese noch verfügbar sind (Issue#12) bzw. ob sie sich im Preis geändert haben
|
||||
* Gibt Liste zurück von Offers, die geändert werden müssen, wird übergeben an OfferUpdater mit availability true oder false
|
||||
*/
|
||||
try {
|
||||
// Establish the database connection
|
||||
connection = DriverManager.getConnection("jdbc:sqlite:persistence.db");
|
||||
|
||||
// Create a SQL statement
|
||||
statement = connection.createStatement();
|
||||
|
||||
// Execute the query to retrieve the entries
|
||||
String query = "SELECT dataOrigin, productId, currentPrice FROM table";
|
||||
resultSet = statement.executeQuery(query);
|
||||
|
||||
// Process the retrieved entries
|
||||
while (resultSet.next()) {
|
||||
String webshop = resultSet.getString("webshop");
|
||||
String dataOrigin = resultSet.getString("data_origin");
|
||||
String productId = resultSet.getString("productId");
|
||||
double currentPrice = resultSet.getDouble("currentPrice");
|
||||
String merchant = resultSet.getString("merchant");
|
||||
double deliveryPrice = resultSet.getDouble("delivery_price");
|
||||
boolean available = resultSet.getBoolean("available");
|
||||
|
||||
// Call the API to get the current price
|
||||
double apiPrice = getPriceFromAPI(productId);
|
||||
|
||||
// Compare the prices
|
||||
if (currentPrice != apiPrice) {
|
||||
// Price has changed, create a Product object and add it to the changedProducts list
|
||||
Product product = new Product(webshop, dataOrigin, productId, currentPrice,
|
||||
merchant, deliveryPrice, available);
|
||||
changedProducts.add(product);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// Close the resources (resultSet, statement, connection) in a finally block
|
||||
try {
|
||||
if (resultSet != null) {
|
||||
resultSet.close();
|
||||
}
|
||||
if (statement != null) {
|
||||
statement.close();
|
||||
}
|
||||
if (connection != null) {
|
||||
connection.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return new ArrayList<Offer>();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user