-
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.
Perf: Update model classes and write Customer class
- Loading branch information
Fredrik Marjoni
committed
Mar 2, 2026
1 parent
e4b38a5
commit 0db6023
Showing
4 changed files
with
116 additions
and
35 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/main/java/edu/group5/app/model/donation/DonationService.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,41 @@ | ||
| package edu.group5.app.service; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.sql.Timestamp; | ||
| import edu.group5.app.model.donation.Donation; | ||
| import edu.group5.app.model.organization.Organization; | ||
| import edu.group5.app.model.organization.OrganizationRepository; | ||
| import edu.group5.app.model.user.Customer; | ||
| import edu.group5.app.model.donation.DonationRepository; | ||
|
|
||
| public class DonationService { | ||
|
|
||
| private final DonationRepository donationRepository; | ||
| private final OrganizationRepository organizationRepository; | ||
|
|
||
| public DonationService(DonationRepository donationRepository, | ||
| OrganizationRepository organizationRepository) { | ||
| 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; | ||
| } | ||
| } |
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,49 @@ | ||
| package edu.group5.app.model.user; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import edu.group5.app.model.organization.Organization; | ||
|
|
||
| /** | ||
| * Customer class represents a customer in the system. | ||
| * It extends the User class and includes additional functionality specific to customers, | ||
| * such as managing their preferences for organizations. | ||
| * Each customer has a map of preferences that associates organization numbers with the corresponding Organization objects. | ||
| */ | ||
| public class Customer extends User { | ||
| private List<Integer> preferences; | ||
|
|
||
| /** | ||
| * Constructs a Customer object. | ||
| * Calls the {@link User} superclass constructor to initialize common user fields | ||
| * and initializes the preferences list. | ||
| * @param userId the unique identifier for the user, must be a positive integer | ||
| * @param firstName the first name of the customer | ||
| * @param lastName the last name of the customer | ||
| * @param email the email address of the customer | ||
| * @param passwordHash the hashed password of the customer | ||
| * @throws IllegalArgumentException if any required field is invalid | ||
| */ | ||
| public Customer(int userId, | ||
| String firstName, | ||
| String lastName, | ||
| String email, | ||
| String passwordHash) { | ||
|
|
||
| super(userId, "Customer", firstName, lastName, email, passwordHash); | ||
| this.preferences = new ArrayList<>(); | ||
| } | ||
|
|
||
| public List<Integer> getPreferences() { | ||
| return preferences; | ||
| } | ||
|
|
||
| public void addPreference(int orgNumber) { | ||
| preferences.add(orgNumber); | ||
| } | ||
|
|
||
| public void removePreference(int orgNumber) { | ||
| preferences.remove(Integer.valueOf(orgNumber)); | ||
| } | ||
| } |
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