diff --git a/src/main/java/edu/group5/app/model/donation/DonationService.java b/src/main/java/edu/group5/app/model/donation/DonationService.java index ecd13da..6f12f91 100644 --- a/src/main/java/edu/group5/app/model/donation/DonationService.java +++ b/src/main/java/edu/group5/app/model/donation/DonationService.java @@ -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; + } } \ No newline at end of file diff --git a/src/main/java/edu/group5/app/model/user/Customer.java b/src/main/java/edu/group5/app/model/user/Customer.java index 4f52b19..6ae6109 100644 --- a/src/main/java/edu/group5/app/model/user/Customer.java +++ b/src/main/java/edu/group5/app/model/user/Customer.java @@ -36,17 +36,35 @@ public List 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)); } -} \ No newline at end of file +}