diff --git a/src/main/java/edu/group5/app/model/donation/DonationRepository.java b/src/main/java/edu/group5/app/model/donation/DonationRepository.java index f179b01..596ea60 100644 --- a/src/main/java/edu/group5/app/model/donation/DonationRepository.java +++ b/src/main/java/edu/group5/app/model/donation/DonationRepository.java @@ -1,4 +1,110 @@ package edu.group5.app.model.donation; -public class DonationRepository { +import edu.group5.app.model.DBRepository; + +import java.util.Comparator; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * Repository class for Donation. + * + *

+ * Extends {@link DBRepository} and manages Donation entities. + *

+ */ +public class DonationRepository extends DBRepository { + private final HashMap content; + + /** + * Constructs DonationRepository using Hashmap, + * and extends the content from DBRepository. + * @param content the underlying map used to store donations, + * where the key represents the donation ID + */ + public DonationRepository(HashMap content){ + super(content); + this.content = content; + } + + /** + * Adds a new donation to the repository + *

+ * The donation is stored using its {@code donationId} as the key. + * If a donation with the same ID already exists, the donation + * will not be added. + *

+ * + * @param donation the donation to add + * @return {@code true} if the donation was successfully added, and + * {@code false} if a donation with the same ID already exists + */ + public boolean addDonation(Donation donation) { + if(content.containsKey(donation.donationId())){ + return false; + } + content.put(donation.donationId(), donation); + return true; + } + + /** + * Returns all donations sorted by date (ascending). + * + *

+ * The returned map preserves the sorted order. + *

+ * + * @return a new {@link HashMap} containing the donations sorted by date + */ + public HashMap sortByDate(){ + return content.entrySet().stream() + .sorted(Map.Entry.comparingByValue( + Comparator.comparing(Donation::date))) + .collect(Collectors.toMap( + Map.Entry::getKey, + Map.Entry::getValue, + (e1, e2) -> e1, + LinkedHashMap::new)); + } + + /** + * Returns all donations sorted by amount (ascending). + * + *

+ * The returned map preserves the sorted order from lowest to highest amount. + *

+ * + * @return a new {@link HashMap} containing the donations sorted by amount. + */ + public HashMap sortByAmount() { + return content.entrySet().stream() + .sorted(Map.Entry.comparingByValue( + Comparator.comparing(Donation::amount))) + .collect(Collectors.toMap( + Map.Entry::getKey, + Map.Entry::getValue, + (e1, e2) -> e1, + LinkedHashMap::new + )); + } + + /** + * Returns all donations associated with a specific organization. + * + * @param orgNumber the organization ID to filter by + * @return a map containing all donations that belong to the given organization + */ + public HashMap filterByOrganization(int orgNumber) { + return content.entrySet() + .stream() + .filter(entry -> entry.getValue().organizationId() == orgNumber) + .collect(Collectors.toMap( + Map.Entry::getKey, + Map.Entry::getValue, + (e1, e2) -> e1, + LinkedHashMap::new + )); + } }