Skip to content

Commit

Permalink
update: update relationship between Customer and intermideary Donatio…
Browse files Browse the repository at this point in the history
…nService
  • Loading branch information
Fredrik Marjoni committed Mar 3, 2026
1 parent 3335ba2 commit c4b2eac
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 29 deletions.
61 changes: 41 additions & 20 deletions src/main/java/edu/group5/app/model/donation/DonationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,55 @@
import edu.group5.app.model.user.Customer;
import edu.group5.app.model.donation.DonationRepository;

/**
* DonationService class provides functionality for handling donations in the system.
* It interacts with the DonationRepository to manage donation records
* and the OrganizationRepository to validate organization information.
* The donate method allows a customer to make a donation to a specified organization,
* ensuring that the customer, organization, and donation amount are valid before processing the donation.
*/
public class DonationService {

private final DonationRepository donationRepository;
private final OrganizationRepository organizationRepository;

/**
* Constructor for DonationService. Initializes the service with the required repositories.
* @param donationRepository the repository for managing donation records
* @param organizationRepository the repository for managing organization information
* @throws IllegalArgumentException if either repository is null
*/
public DonationService(DonationRepository donationRepository,
OrganizationRepository organizationRepository) {
this.donationRepository = donationRepository;
this.organizationRepository = organizationRepository;
if (donationRepository == null) {
throw new IllegalArgumentException("DonationRepository cannot be null");
}
if (organizationRepository == null) {
throw new IllegalArgumentException("OrganizationRepository cannot be null");
}
this.donationRepository = donationRepository;
this.organizationRepository = organizationRepository;
}

public boolean donate(Customer customer,
int orgNumber,
BigDecimal amount) {
if (customer == null || amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
return false;
}
Organization org = organizationRepository.findByOrgNumber(orgNumber);
if (org == null) {
return false;
}
Donation donation = new Donation(
customer.getUserId(),
org.getOrgNumber(),
amount,
new Timestamp(System.currentTimeMillis())
);
donationRepository.addDonation(donation);
return true;
/**
* Processes a donation from a customer to a specified organization with a given amount.
* Validates the customer, organization number, and donation amount before creating a donation record.
* @param customer the customer making the donation
* @param orgNumber the organization number to which the donation is made
* @param amount the amount of the donation
* @return true if the donation is successfully processed, false otherwise
*/
public boolean donate(Customer customer, int orgNumber, BigDecimal amount) {
if (customer == null || amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
return false;
}
Organization org = organizationRepository.findByOrgNumber(orgNumber);
if (org == null) {
return false;
}
Donation donation = new Donation(customer.getUserId(), org.getOrgNumber(),
amount, new Timestamp(System.currentTimeMillis()));
donationRepository.addDonation(donation);
return true;
}
}
36 changes: 27 additions & 9 deletions src/main/java/edu/group5/app/model/user/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,35 @@ public List<Integer> getPreferences() {
return preferences;
}

public void addPreference(int orgNumber) {
if (preferences.contains(orgNumber)) {
throw new IllegalArgumentException("Organization number already in preferences");
public boolean addPreference(int orgNumber) {
try {
if (orgNumber <= 0) {
throw new IllegalArgumentException("Organization number must be a positive integer");
}
if (preferences.contains(orgNumber)) {
throw new IllegalStateException("Organization number already in preferences");
}
preferences.add(orgNumber);
return true;
} catch (Exception e) {
System.out.println("Add preference failed: " + e.getMessage());
return false;
}
preferences.add(orgNumber);
}

public void removePreference(int orgNumber) {
if (!preferences.contains(orgNumber)) {
throw new IllegalArgumentException("Organization number not found in preferences");
public boolean removePreference(int orgNumber) {
try {
if (orgNumber <= 0) {
throw new IllegalArgumentException("Organization number must be a positive integer");
}
if (!preferences.contains(orgNumber)) {
throw new IllegalStateException("Organization number not found in preferences");
}
preferences.remove(Integer.valueOf(orgNumber));
return true;
} catch (Exception e) {
System.out.println("Remove preference failed: " + e.getMessage());
return false;
}
preferences.remove(Integer.valueOf(orgNumber));
}
}
}

0 comments on commit c4b2eac

Please sign in to comment.