Skip to content

Commit

Permalink
feat: added categorisation on the front page
Browse files Browse the repository at this point in the history
  • Loading branch information
cathrkri committed Mar 30, 2026
1 parent 940fcaa commit 240943f
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Charity> 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
*
Expand Down Expand Up @@ -120,4 +117,70 @@ public void handleFrontSearch(ActionEvent event) {

LoaderScene.LoadScene("availableOrganization", event, null, query);
}

private List<Charity> getFilteredCharities() {
if (!verifiedFilter.isSelected()
&& !childrenFilter.isSelected()
&& !healthFilter.isSelected()
&& !emergencyAidFilter.isSelected()) {
return allCharities;
}

List<Charity> 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<Charity> 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);
}
}
}
}
8 changes: 4 additions & 4 deletions helpmehelpapplication/src/main/resources/fxml/frontPage.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@
<Insets left="20.0" />
</VBox.margin>
</Label>
<CheckBox mnemonicParsing="false" text="Verified by IK">
<CheckBox fx:id="verifiedFilter" onAction="#handleCategoryFilterChange" mnemonicParsing="false" text="Verified by IK">
<font>
<Font size="12.0" />
</font>
Expand All @@ -249,7 +249,7 @@
<Insets top="10.0" />
</padding>
</CheckBox>
<CheckBox mnemonicParsing="false" text="Children">
<CheckBox fx:id="childrenFilter" onAction="#handleCategoryFilterChange" mnemonicParsing="false" text="Children">
<font>
<Font size="12.0" />
</font>
Expand All @@ -260,7 +260,7 @@
<Insets left="20.0" />
</VBox.margin>
</CheckBox>
<CheckBox mnemonicParsing="false" text="Health">
<CheckBox fx:id="healthFilter" onAction="#handleCategoryFilterChange" mnemonicParsing="false" text="Health">
<font>
<Font size="12.0" />
</font>
Expand All @@ -271,7 +271,7 @@
<Insets left="20.0" />
</VBox.margin>
</CheckBox>
<CheckBox mnemonicParsing="false" text="Emergency aid">
<CheckBox fx:id="emergencyAidFilter" onAction="#handleCategoryFilterChange" mnemonicParsing="false" text="Emergency aid">
<font>
<Font size="12.0" />
</font>
Expand Down

0 comments on commit 240943f

Please sign in to comment.