From 240943f9f3fb937e59493b5bcc5a47d3793544d2 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 30 Mar 2026 15:33:07 +0200 Subject: [PATCH] feat: added categorisation on the front page --- .../team6/controller/FrontpageController.java | 127 +++++++++++++----- .../src/main/resources/fxml/frontPage.fxml | 8 +- 2 files changed, 99 insertions(+), 36 deletions(-) diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/FrontpageController.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/FrontpageController.java index 52a4a34..709fd65 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/FrontpageController.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/FrontpageController.java @@ -1,10 +1,14 @@ package ntnu.systemutvikling.team6.controller; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import java.util.Random; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; +import javafx.scene.control.CheckBox; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.FlowPane; @@ -23,75 +27,68 @@ */ public class FrontpageController { @FXML private Charity featuredCharity; - @FXML private FlowPane cardsContainer; - @FXML private Label Carosel_Organisasjon; - @FXML private Label Carosel_Beskrivelse; - @FXML private Label Total_Orgnisasjon; - @FXML private Label Total_Donations; - @FXML private Label PreApproved_Percentage; - @FXML private TextField frontSearchField; + @FXML private CheckBox verifiedFilter; + @FXML private CheckBox childrenFilter; + @FXML private CheckBox healthFilter; + @FXML private CheckBox emergencyAidFilter; + + private List allCharities = new ArrayList<>(); /** * Initialize method for the front page. This method is called when the front page is loaded. It * retrieves the list of charities and donations from the database. The list of charities is * displayed as a list of cards, where each card represents a charity from the - * Innsamlingskontrollen A random charity is selected to be featured on the page, and its name and - * description are displayed in the carousel section. The total number of charities, total amount - * of donations, and percentage of pre-approved charities are also displayed on the page. + * Innsamlingskontrollen. A random charity is selected to be featured on the page, and its name + * and description are displayed in the carousel section. The total number of charities, total + * amount of donations, and percentage of pre-approved charities are also displayed on the page. */ @FXML public void initialize() { try { DatabaseManager db = new DatabaseManager(); - CharityRegistry Charities = db.getCharitiesFromDB(); - DonationRegistry Donations = db.getDonationFromDB(); - for (Charity ch : Charities.getAllCharities()) { - - FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/organizationCard.fxml")); + CharityRegistry charities = db.getCharitiesFromDB(); + DonationRegistry donations = db.getDonationFromDB(); - Parent card = loader.load(); - - OrganizationCardController cardController = loader.getController(); + allCharities = new ArrayList<>(charities.getAllCharities()); + displayCharities(allCharities); - // System.out.println("Added Name: " + ch.getName() + " Added Description: " + - // ch.getDescription()); - cardController.setOrganization(ch); - - cardsContainer.getChildren().add(card); - } - int Charities_size = Charities.getAllCharities().size(); + int charitiesSize = charities.getAllCharities().size(); Random random = new Random(); - int randomIndex = random.nextInt(Charities_size); - Charity randomCharity = Charities.getAllCharities().get(randomIndex); + int randomIndex = random.nextInt(charitiesSize); + Charity randomCharity = charities.getAllCharities().get(randomIndex); this.featuredCharity = randomCharity; Carosel_Organisasjon.setText(randomCharity.getName()); Carosel_Beskrivelse.setText(randomCharity.getDescription()); - Total_Orgnisasjon.setText(Integer.toString(Charities_size)); + Total_Orgnisasjon.setText(Integer.toString(charitiesSize)); Total_Donations.setText( Double.toString( - Donations.getAllDonations().stream().mapToDouble(Donation::getAmount).sum())); + donations.getAllDonations().stream().mapToDouble(Donation::getAmount).sum())); PreApproved_Percentage.setText( String.format( "%.2f", - Charities.getAllCharities().stream().filter(Charity::getPreApproved).count() + charities.getAllCharities().stream().filter(Charity::getPreApproved).count() * 100.0 - / Charities_size) + / charitiesSize) + "%"); - } catch (Exception e) { e.printStackTrace(); } } + @FXML + public void handleCategoryFilterChange(ActionEvent event) { + displayCharities(getFilteredCharities()); + } + /** * This method is used to switch to the charity page for the selected charity * @@ -120,4 +117,70 @@ public void handleFrontSearch(ActionEvent event) { LoaderScene.LoadScene("availableOrganization", event, null, query); } + + private List getFilteredCharities() { + if (!verifiedFilter.isSelected() + && !childrenFilter.isSelected() + && !healthFilter.isSelected() + && !emergencyAidFilter.isSelected()) { + return allCharities; + } + + List filteredCharities = new ArrayList<>(); + for (Charity charity : allCharities) { + if (matchesSelectedFilters(charity)) { + filteredCharities.add(charity); + } + } + return filteredCharities; + } + + private boolean matchesSelectedFilters(Charity charity) { + return (verifiedFilter.isSelected() && charity.getPreApproved()) + || (childrenFilter.isSelected() && matchesKeywordCategory(charity, "children")) + || (healthFilter.isSelected() && matchesKeywordCategory(charity, "health")) + || (emergencyAidFilter.isSelected() && matchesKeywordCategory(charity, "emergency")); + } + + private boolean matchesKeywordCategory(Charity charity, String category) { + String text = (charity.getName() + " " + charity.getDescription()).toLowerCase(); + + return switch (category) { + case "children" -> + text.contains("child") + || text.contains("children") + || text.contains("barn") + || text.contains("youth") + || text.contains("young"); + case "health" -> + text.contains("health") + || text.contains("medical") + || text.contains("helse") + || text.contains("hospital") + || text.contains("care"); + case "emergency" -> + text.contains("emergency") + || text.contains("relief") + || text.contains("crisis") + || text.contains("aid") + || text.contains("disaster"); + default -> false; + }; + } + + private void displayCharities(List charities) { + cardsContainer.getChildren().clear(); + + for (Charity charity : charities) { + try { + FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/organizationCard.fxml")); + Parent card = loader.load(); + OrganizationCardController cardController = loader.getController(); + cardController.setOrganization(charity); + cardsContainer.getChildren().add(card); + } catch (IOException e) { + throw new RuntimeException("Could not load organization card.", e); + } + } + } } diff --git a/helpmehelpapplication/src/main/resources/fxml/frontPage.fxml b/helpmehelpapplication/src/main/resources/fxml/frontPage.fxml index 9fe35a2..8d4edd5 100644 --- a/helpmehelpapplication/src/main/resources/fxml/frontPage.fxml +++ b/helpmehelpapplication/src/main/resources/fxml/frontPage.fxml @@ -238,7 +238,7 @@ - + @@ -249,7 +249,7 @@ - + @@ -260,7 +260,7 @@ - + @@ -271,7 +271,7 @@ - +