Skip to content

Commit

Permalink
Merge pull request #44 from Group-5/feat/donation
Browse files Browse the repository at this point in the history
Feat/donation
  • Loading branch information
emilfa authored Mar 5, 2026
2 parents ba2d6ad + e15478f commit e401d27
Show file tree
Hide file tree
Showing 6 changed files with 483 additions and 5 deletions.
25 changes: 25 additions & 0 deletions src/main/java/edu/group5/app/model/DBRepository.java
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);
}
}
8 changes: 6 additions & 2 deletions src/main/java/edu/group5/app/model/Repository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import java.util.Map;

/**
* Represents a repository
* Represents a repository.
*/
public abstract class Repository<K, V> {
protected final Map<K, V> content;

/**
* Constructs a new Repository with the specified content.
*
* @param content the underlying data structure used to store entities
*/
public Repository(Map<K, V> content) {
this.content = content;
}
Expand Down
52 changes: 50 additions & 2 deletions src/main/java/edu/group5/app/model/donation/Donation.java
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 src/main/java/edu/group5/app/model/donation/DonationRepository.java
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
));
}
}
Loading

0 comments on commit e401d27

Please sign in to comment.