From 0987cd0fc3365627d514b4c7a126ef271ecda56d Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 30 Mar 2026 15:58:56 +0200 Subject: [PATCH] faet: minor change --- .../team6/controller/FrontpageController.java | 309 +++++++++--------- 1 file changed, 160 insertions(+), 149 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 709fd65..e5ad7d5 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/FrontpageController.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/FrontpageController.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Random; + import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; @@ -26,161 +27,171 @@ * to the charity page and the donation page for the featured charity */ 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. - */ - @FXML - public void initialize() { - try { - DatabaseManager db = new DatabaseManager(); - CharityRegistry charities = db.getCharitiesFromDB(); - DonationRegistry donations = db.getDonationFromDB(); - - allCharities = new ArrayList<>(charities.getAllCharities()); - displayCharities(allCharities); - - int charitiesSize = charities.getAllCharities().size(); - Random random = new Random(); - 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(charitiesSize)); - Total_Donations.setText( - Double.toString( - donations.getAllDonations().stream().mapToDouble(Donation::getAmount).sum())); - PreApproved_Percentage.setText( - String.format( - "%.2f", - charities.getAllCharities().stream().filter(Charity::getPreApproved).count() - * 100.0 - / charitiesSize) - + "%"); - } catch (Exception e) { - e.printStackTrace(); + @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. + */ + @FXML + public void initialize() { + try { + DatabaseManager db = new DatabaseManager(); + CharityRegistry charities = db.getCharitiesFromDB(); + DonationRegistry donations = db.getDonationFromDB(); + + allCharities = new ArrayList<>(charities.getAllCharities()); + displayCharities(allCharities); + + int charitiesSize = charities.getAllCharities().size(); + Random random = new Random(); + 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(charitiesSize)); + Total_Donations.setText( + Double.toString( + donations.getAllDonations().stream().mapToDouble(Donation::getAmount).sum())); + PreApproved_Percentage.setText( + String.format( + "%.2f", + charities.getAllCharities().stream().filter(Charity::getPreApproved).count() + * 100.0 + / 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 - * - * @param event - */ - public void switchToCharityPage(ActionEvent event) { - LoaderScene.LoadScene("CharityPage", event, featuredCharity, null); - } - - /** - * This method is used to switch to the donation page for the selected charity. - * - * @param event - */ - public void switchToDonationPage(ActionEvent event) { - LoaderScene.LoadScene("DonationPage", event, featuredCharity, null); - } - - @FXML - public void handleFrontSearch(ActionEvent event) { - String query = frontSearchField.getText().trim(); - - if (query.isEmpty()) { - return; + + + /** + * This method is used to switch to the charity page for the selected charity + * + * @param event + */ + public void switchToCharityPage(ActionEvent event) { + LoaderScene.LoadScene("CharityPage", event, featuredCharity, null); } - LoaderScene.LoadScene("availableOrganization", event, null, query); - } + /** + * This method is used to switch to the donation page for the selected charity. + * + * @param event + */ + public void switchToDonationPage(ActionEvent event) { + LoaderScene.LoadScene("DonationPage", event, featuredCharity, null); + } - private List getFilteredCharities() { - if (!verifiedFilter.isSelected() - && !childrenFilter.isSelected() - && !healthFilter.isSelected() - && !emergencyAidFilter.isSelected()) { - return allCharities; + @FXML + public void handleCategoryFilterChange(ActionEvent event) { + displayCharities(getFilteredCharities()); } - List filteredCharities = new ArrayList<>(); - for (Charity charity : allCharities) { - if (matchesSelectedFilters(charity)) { - filteredCharities.add(charity); - } + @FXML + public void handleFrontSearch(ActionEvent event) { + String query = frontSearchField.getText().trim(); + + if (query.isEmpty()) { + return; + } + + LoaderScene.LoadScene("availableOrganization", event, null, query); } - 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); - } + + 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); + } + } } - } }