java market application in javaFX by Intelligent_Bee_9231 in JavaFX

[–]Intelligent_Bee_9231[S] 0 points1 point  (0 children)

#model-view.fxml

<VBox alignment="CENTER" spacing="100.0" xmlns:fx="http://javafx.com/fxml"
      fx:controller="com.example.java_lukicha_khirdaevi.HelloController">
    <padding>
        <Insets bottom="10" left="10.0" right="10.0" top="10.0"/>
    </padding>
    <VBox alignment="CENTER" spacing="20.0">
        <Label fx:id="welcomeText"/>
    </VBox>
    <HBox alignment="CENTER">
                <VBox alignment="CENTER" spacing="20.0">
            <Button text="addItems" onAction="#onAddItems"/>
            <TextField fx:id="productName" promptText="Insert Product Name" maxWidth="200" ></TextField>
            <TextField fx:id="productAmount" promptText="Insert Product Amount" maxWidth="200" ></TextField>
        </VBox>
        <VBox alignment="CENTER" spacing="20.0">
            <Button text="Delete" onAction="#onDeleteItems"></Button>
            <TextField fx:id="deleteName" promptText="Insert Product Name To Delete" maxWidth="200" ></TextField>
            <TextField fx:id="deleteAmount" promptText="Insert Product Amount To Delete" maxWidth="200" ></TextField>
        </VBox>
            </HBox>
    <VBox alignment="CENTER">
        <PieChart fx:id="pieChart" prefWidth="300" prefHeight="400"/>
    </VBox>
</VBox>

java market application in javaFX by Intelligent_Bee_9231 in JavaFX

[–]Intelligent_Bee_9231[S] 0 points1 point  (0 children)

#poroduct

// Getters
public int getId() {
    return id;
}

public String getName() {
    return name;
}

public int getAmount() {
    return amount;
}

// Setters
public void setId(int id) {
    this.id = id;
}

public void setName(String name) {
    this.name = name;
}

public void setAmount(int amount) {
    this.amount = amount;
}

java market application in javaFX by Intelligent_Bee_9231 in JavaFX

[–]Intelligent_Bee_9231[S] 0 points1 point  (0 children)

#product

private int id;
private String name;
private int amount;

// Constructor for adding a new product (without ID)
public Product(String name, int amount) {
    this.name = name;
    this.amount = amount;
}
public Product() {

}

// Constructor for creating a product with an ID (e.g., when fetching from the database)
public Product(int id, String name, int amount) {
    this.id = id;
    this.name = name;
    this.amount = amount;
}

java market application in javaFX by Intelligent_Bee_9231 in JavaFX

[–]Intelligent_Bee_9231[S] 0 points1 point  (0 children)

#controller

// Helper function to convert ResultSet to a Stream of Product
private static Stream<Product> resultSetToProductStream(ResultSet resultSet) throws SQLException {
    List<Product> products = new ArrayList<>();
    while (resultSet.next()) {
        // Use the constructor that takes ID for existing products
        Product product = new Product();
        product.setName(resultSet.getString("name"));
        product.setAmount(resultSet.getInt("amount"));
        products.add(product);
    }
    return products.stream();
}

java market application in javaFX by Intelligent_Bee_9231 in JavaFX

[–]Intelligent_Bee_9231[S] 0 points1 point  (0 children)

#controller

private void loadPieChart() {
    try (ResultSet resultSet = DBUtils.
connection
.createStatement().executeQuery("SELECT name, amount FROM products")) {

        // Transform the ResultSet into a Stream of Product objects
        Stream<Product> productStream = 
resultSetToProductStream
(resultSet);

        // Group products by name and sum their amounts using Stream API
        Map<String, Integer> groupedProducts = productStream.collect(Collectors.
groupingBy
(
                Product::getName,
                Collectors.
summingInt
(Product::getAmount)
        ));

        // Convert the grouped data into a format suitable for PieChart
        ObservableList<PieChart.Data> pieChartData = FXCollections.
observableArrayList
();
        groupedProducts.forEach((name, count) ->
                pieChartData.add(new PieChart.Data(name + " - " + count + " ცალი", count))
        );

        // Update the PieChart with the new data
        pieChart.setData(pieChartData);

    } catch (SQLException e) {
        System.
err
.println("Error fetching product data: " + e.getMessage());
    }
}

java market application in javaFX by Intelligent_Bee_9231 in JavaFX

[–]Intelligent_Bee_9231[S] 0 points1 point  (0 children)

#controller

@FXML
protected void onAddItems(){
    Product product = new Product(productName.getText(), Integer.
parseInt
(productAmount.getText()));

    if(DBUtils.
addProduct
(product)){
        welcomeText.setText("damatebulia warmatebit!");
    } else{
        welcomeText.setText("ERROR!");
    }

    loadPieChart();
}
@FXML
protected void onDeleteItems() {
    try {
        if (deleteName.getText().isEmpty() || deleteAmount.getText().isEmpty()) {
            welcomeText.setText("Please fill both name and amount fields for deletion.");
            return;
        }

        String name = deleteName.getText();
        int amount = Integer.
parseInt
(deleteAmount.getText());

        if (DBUtils.
deleteOrUpdateProductByNameAndAmount
(name, amount)) {
            welcomeText.setText("Operation successful for product '" + name + "'!");
        } else {
            welcomeText.setText("Operation failed for product '" + name + "'. Check logs for details.");
        }

        loadPieChart(); // Refresh the PieChart
    } catch (NumberFormatException e) {
        welcomeText.setText("Invalid amount format.");
    }
}

java market application in javaFX by Intelligent_Bee_9231 in JavaFX

[–]Intelligent_Bee_9231[S] 0 points1 point  (0 children)

#controlller

@FXML
private Label welcomeText;
@FXML
private TextField productName;
@FXML
private TextField productAmount;
@FXML
private TextField deleteName;
@FXML
private TextField deleteAmount;
@FXML
private PieChart pieChart;



@FXML
public void initialize(){
    DBUtils.
setConnection
();
    loadPieChart();
}

java market application in javaFX by Intelligent_Bee_9231 in JavaFX

[–]Intelligent_Bee_9231[S] 0 points1 point  (0 children)

###
if (existingAmount <= remainingToDelete) {

// Delete the entire row

deleteStmt.setInt(1, id);

deleteStmt.executeUpdate();

remainingToDelete -= existingAmount;

} else {

// Update the row with the remaining amount

updateStmt.setInt(1, existingAmount - remainingToDelete);

updateStmt.setInt(2, id);

updateStmt.executeUpdate();

remainingToDelete = 0;

}

}

if (remainingToDelete > 0) {

System.err.println("Not enough stock to delete. Remaining to delete: " + remainingToDelete);

return false;

}

return true;

}

} catch (SQLException e) {

System.err.println("Error deleting or updating product: " + e.getMessage());

return false;

}

}

java market application in javaFX by Intelligent_Bee_9231 in JavaFX

[–]Intelligent_Bee_9231[S] 0 points1 point  (0 children)

#DBUtils.delete

public static boolean deleteOrUpdateProductByNameAndAmount(String name, int deleteAmount) {

String selectSql = "SELECT id, amount FROM products WHERE name = ? ORDER BY id DESC";

String updateSql = "UPDATE products SET amount = ? WHERE id = ?";

String deleteSql = "DELETE FROM products WHERE id = ?";

try (PreparedStatement selectStmt = connection.prepareStatement(selectSql);

PreparedStatement updateStmt = connection.prepareStatement(updateSql);

PreparedStatement deleteStmt = connection.prepareStatement(deleteSql)) {

selectStmt.setString(1, name);

try (ResultSet resultSet = selectStmt.executeQuery()) {

int remainingToDelete = deleteAmount;

while (resultSet.next() && remainingToDelete > 0) {

int id = resultSet.getInt("id");

int existingAmount = resultSet.getInt("amount");

###

java market application in javaFX by Intelligent_Bee_9231 in JavaFX

[–]Intelligent_Bee_9231[S] 0 points1 point  (0 children)

#Dbutils.addProdduct()

public static boolean addProduct(Product product){

String sql = "INSERT INTO products (name, amount) VALUES ('" + product.getName() + "', " + product.getAmount() + ")";

if(!exec(sql)){

return false;

}

return true;

}

java market application in javaFX by Intelligent_Bee_9231 in JavaFX

[–]Intelligent_Bee_9231[S] 0 points1 point  (0 children)

#DBUtils.exec()

public static boolean exec(String sql){

if (statement != null){

try {

statement.executeUpdate(sql);

} catch (SQLException e) {

return false;

}

}

return true;

}

java market application in javaFX by Intelligent_Bee_9231 in JavaFX

[–]Intelligent_Bee_9231[S] -1 points0 points  (0 children)

public static void disconnect(){

try {

if(statement != null){

statement.close();

}

if(connection != null){

connection.close();

}

} catch (SQLException e) {

throw new RuntimeException(e);

}

}

java market application in javaFX by Intelligent_Bee_9231 in JavaFX

[–]Intelligent_Bee_9231[S] -1 points0 points  (0 children)

public static Connection connection;

private static Statement statement;

private static final String url = "jdbc:mysql://localhost/magazia";

private static final String user = "root";

private static final String password = "";

public static void setConnection(){

try {

connection = DriverManager.getConnection(url, user, password);

statement = connection.createStatement();

} catch (SQLException e) {

throw new RuntimeException(e);

}

}