-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from Group-5/feat/donation
Feat/donation
- Loading branch information
Showing
6 changed files
with
483 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package edu.group5.app.model; | ||
|
|
||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Abstract base class for repositories that store their data | ||
| * in a database-related structure. | ||
| * | ||
| * <p> | ||
| * Extends {@link Repository} and specifies that the content | ||
| * is stored as a {@link Map}. | ||
| * </p> | ||
| */ | ||
| public abstract class DBRepository<K, V> extends Repository<K, V> { | ||
| /** | ||
| * Constructs a DBRepository with the given content. | ||
| * | ||
| * @param content the HashMap used to store repository entities | ||
| */ | ||
| protected DBRepository(Map<K, V> content) { | ||
| super(content); | ||
| } | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,53 @@ | ||
| package edu.group5.app.model.donation; | ||
|
|
||
| public class Donation { | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.sql.Timestamp; | ||
|
|
||
| /** | ||
| * Represents a verified donation made by a user to an organization. | ||
| * @param donationId - the unique ID of this donation | ||
| * @param userId - the ID of the user making the donation | ||
| * @param organizationId - the ID of the organization receiving the donation | ||
| * @param amount - the donation amount | ||
| * @param date - the timestamp when the donation was made | ||
| * @param paymentMethod - the payment method used | ||
| */ | ||
| public record Donation( | ||
| int donationId, | ||
| int userId, | ||
| int organizationId, | ||
| BigDecimal amount, | ||
| Timestamp date, | ||
| String paymentMethod) { | ||
|
|
||
| /** | ||
| * Constructor with throws. | ||
| * | ||
| * @param donationId - throws if donationID is negative or 0. | ||
| * @param userId - throws if userID is negative or 0. | ||
| * @param organizationId - throws if organizationID is negative or 0. | ||
| * @param amount - throws if amount is negative or null. | ||
| * @param date - throws if date is null. | ||
| * @param paymentMethod - throws if payment is null or blank. | ||
| */ | ||
| public Donation { | ||
| if (donationId <= 0) { | ||
| throw new IllegalArgumentException("Donation ID must be positive"); | ||
| } | ||
| if (userId <= 0) { | ||
| throw new IllegalArgumentException("User ID must be positive"); | ||
| } | ||
| if (organizationId <= 0) { | ||
| throw new IllegalArgumentException("Organization ID must be positive"); | ||
| } | ||
| if (amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) { | ||
| throw new IllegalArgumentException("Amount must be positive and not null"); | ||
| } | ||
| if (date == null) { | ||
| throw new IllegalArgumentException("Date must not be null"); | ||
| } | ||
| if (paymentMethod == null || paymentMethod.isBlank()) { | ||
| throw new IllegalArgumentException("Payment method must not be empty"); | ||
| } | ||
| } | ||
| } |
119 changes: 119 additions & 0 deletions
119
src/main/java/edu/group5/app/model/donation/DonationRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package edu.group5.app.model.donation; | ||
|
|
||
| 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){ | ||
| if (content == null) { | ||
| throw new IllegalArgumentException("Content cannot be null"); | ||
| } | ||
| 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 (donation == null) { | ||
| throw new IllegalArgumentException("Donation cannot be null"); | ||
| } | ||
| 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) { | ||
| if (orgNumber <= 0) { | ||
| throw new IllegalArgumentException("Organization number must be positive"); | ||
| } | ||
| return content.entrySet() | ||
| .stream() | ||
| .filter(entry -> entry.getValue().organizationId() == orgNumber) | ||
| .collect(Collectors.toMap( | ||
| Map.Entry::getKey, | ||
| Map.Entry::getValue, | ||
| (e1, e2) -> e1, | ||
| LinkedHashMap::new | ||
| )); | ||
| } | ||
| } |
Oops, something went wrong.