Final JavaFX
Final JavaFX
Report
using JavaFX.
3. Objective:
Browse Products:
Complete Purchases:
4. Application window:
a. User-Friendly Interface:
7. Implementation Steps
1. Setting Up the Project:
8. Code :
hello-view.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.VBox?>
</VBox>
HelloController.java:
package com.example.demo;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.util.LinkedHashMap;
import java.util.Map;
@FXML
private ListView<Product> productListView;
@FXML
private ListView<Product> cartListView;
@FXML
private Label totalPriceLabel;
@FXML
public void initialize() {
products.addAll(
new Product("Snickers", 280, "/images/snickers.jpg"),
new Product("Twix", 280, "/images/twix.jpg"),
new Product("Kazakhstan", 754, "/images/kazakhstan.jpg"),
new Product("Kinder Bueno", 418, "/images/kinder-bueno.jpg"),
new Product("Milka", 910, "/images/milka.jpg"),
new Product("Lindt", 1609, "/images/lindt.jpg"),
new Product("Kit-Kat", 350, "/images/kit_kat.jpg"),
new Product("Albeni", 250, "/images/albeni.jpg"),
new Product("Alpen Gold", 450, "/images/alpen_gold.jpg"),
new Product("MilkyWay", 159, "/images/milkyway.jpg"),
new Product("Toblerone", 1700, "/images/toblerone.jpg"),
new Product("Bounty", 280, "/images/bounty.jpg"),
new Product("Picnic", 366, "/images/picnic.jpg"),
new Product("Roshen", 652, "/images/roshen.jpg"),
new Product("Nestle", 600, "/images/nestle.jpg")
);
configureProductListView();
configureCartListView();
productListView.setItems(products);
updateCartListView();
}
@Override
protected void updateItem(Product item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
setGraphic(null);
} else {
setText(item.getName() + " (" + item.getPrice() + " тг)");
try {
imageView.setImage(item.getImage());
} catch (Exception e) {
System.err.println("Ошибка загрузки изображения: " + item.getImagePath());
imageView.setImage(null);
}
imageView.setFitWidth(50);
imageView.setFitHeight(50);
setGraphic(imageView);
}
}
});
}
@Override
protected void updateItem(Product product, boolean empty) {
super.updateItem(product, empty);
if (empty || product == null) {
setText(null);
setGraphic(null);
} else {
int quantity = cart.getOrDefault(product, 0);
int subtotal = product.getPrice() * quantity;
try {
imageView.setImage(product.getImage());
} catch (Exception e) {
System.err.println("Ошибка загрузки изображения: " + product.getImagePath());
imageView.setImage(null);
}
imageView.setFitWidth(50);
imageView.setFitHeight(50);
@FXML
public void addToCart() {
Product selectedProduct = productListView.getSelectionModel().getSelectedItem();
if (selectedProduct != null) {
cart.put(selectedProduct, cart.getOrDefault(selectedProduct, 0) + 1);
updateCartListView();
}
}
@FXML
public void removeFromCart() {
Product selectedProduct = cartListView.getSelectionModel().getSelectedItem();
if (selectedProduct != null) {
int quantity = cart.getOrDefault(selectedProduct, 0);
if (quantity > 1) {
cart.put(selectedProduct, quantity - 1);
} else {
cart.remove(selectedProduct);
}
updateCartListView();
}
}
@FXML
public void clearCart() {
cart.clear();
updateCartListView();
}
@FXML
public void buySelectedProduct() {
Product selectedProduct = cartListView.getSelectionModel().getSelectedItem();
if (selectedProduct != null) {
int quantity = cart.getOrDefault(selectedProduct, 0);
int subtotal = selectedProduct.getPrice() * quantity;
// Удаляем товар из корзины
cart.remove(selectedProduct);
updateCartListView();
@FXML
public void buyAllProducts() {
if (!cart.isEmpty()) {
// Создаем сообщение о купленных товарах
StringBuilder receipt = new StringBuilder("Вы успешно купили:\n");
int totalPrice = 0;
for (Map.Entry<Product, Integer> entry : cart.entrySet()) {
Product product = entry.getKey();
int quantity = entry.getValue();
int subtotal = product.getPrice() * quantity;
receipt.append(product.getName())
.append(" x")
.append(quantity)
.append(" (")
.append(subtotal)
.append(" тг)\n");
totalPrice += subtotal;
}
// Очистка корзины
cart.clear();
updateCartListView();
HelloApplication.java:
package com.example.demo;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
Product.java:
package com.example.demo;
import javafx.scene.image.Image;
import java.util.Objects;
public class Product {
private String name;
private int price;
private String imagePath;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Product product = (Product) o;
return Objects.equals(name, product.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
}
9. Challenges Faced:
10. Conclusion: