Skip to content

Commit

Permalink
faet: minor change
Browse files Browse the repository at this point in the history
  • Loading branch information
cathrkri committed Mar 30, 2026
1 parent 240943f commit 0987cd0
Showing 1 changed file with 160 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<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.
*/
@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<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.
*/
@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<Charity> getFilteredCharities() {
if (!verifiedFilter.isSelected()
&& !childrenFilter.isSelected()
&& !healthFilter.isSelected()
&& !emergencyAidFilter.isSelected()) {
return allCharities;
@FXML
public void handleCategoryFilterChange(ActionEvent event) {
displayCharities(getFilteredCharities());
}

List<Charity> 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<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);
}

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);
}
}
}
}
}

0 comments on commit 0987cd0

Please sign in to comment.