Skip to content

Commit

Permalink
feat: implement DonationRepository with sorting and filtering methods
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheaGjerde committed Mar 2, 2026
1 parent d608890 commit 3361b26
Showing 1 changed file with 107 additions and 1 deletion.
108 changes: 107 additions & 1 deletion src/main/java/edu/group5/app/model/donation/DonationRepository.java
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>
* Extends {@link DBRepository} and manages Donation entities.
* </p>
*/
public class DonationRepository extends DBRepository<Integer, Donation> {
private final HashMap<Integer, Donation> 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<Integer, Donation> content){
super(content);
this.content = content;
}

/**
* Adds a new donation to the repository
* <p>
* 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.
* </p>
*
* @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).
*
* <p>
* The returned map preserves the sorted order.
* </p>
*
* @return a new {@link HashMap} containing the donations sorted by date
*/
public HashMap<Integer, Donation> 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).
*
* <p>
* The returned map preserves the sorted order from lowest to highest amount.
* </p>
*
* @return a new {@link HashMap} containing the donations sorted by amount.
*/
public HashMap<Integer, Donation> 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<Integer, Donation> 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
));
}
}

0 comments on commit 3361b26

Please sign in to comment.