Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
BE-#94: Working on calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Flugschnitzel committed May 20, 2024
1 parent 6cf2237 commit a7001be
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.dhbw.get2gether.backend.widget.model.expensesplit;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class Dept {
private String userId;
private double deptAmount;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Builder(toBuilder = true)
@Getter
Expand Down Expand Up @@ -40,5 +42,47 @@ public boolean replaceEntry(ExpenseEntry oldEntry, ExpenseEntry newEntry) {
entries.set(index, newEntry);
return true;
}

public List<Dept> calculateDeptsForUserId(String userId){
List<Dept> depts = new ArrayList<>();
for(ExpenseEntry expenseEntry : entries){
if(expenseEntry.getCreatorId().equals(userId)){
// Add every involved user except the buyer to List of dept
for(UserWithPercentage userWithPercentage: expenseEntry.getInvolvedUsers()){
if(!Objects.equals(userWithPercentage.getUserId(), userId)){
depts = addDept(depts, userWithPercentage.getUserId(), expenseEntry.getPrice()*userWithPercentage.getPercentage());
}
}
} else {
Optional<UserWithPercentage> userWithPercentage = expenseEntry.getInvolvedUsers().stream()
.findFirst().filter(user -> user.getUserId().equals(userId));
if(userWithPercentage.isPresent()){
// Add the Dept of the user to the Dept List
// In this case the user for whom we calculate all depts is a involved user and not the payer
depts = addDept(depts, expenseEntry.getCreatorId(), expenseEntry.getPrice()*userWithPercentage.get().getPercentage()*-1.0);
}
}
}
return depts;
}

// This method extracts the logic if the deptor is already in the list or not
private List<Dept> addDept(List<Dept> depts, String debtorId, double deptAmount){
Optional<Dept> optionalDept = depts.stream().findFirst()
.filter(dept -> dept.getUserId().equals(debtorId));
// todo somehow in second entry with second user this optional is empty but should not be

if(optionalDept.isPresent()){
// Calculate new deptAmount if already exists
Dept newDept = new Dept(debtorId, optionalDept.get().getDeptAmount()+deptAmount);
int index = depts.indexOf(optionalDept.get());
depts.set(index, newDept);

} else {
// Add new dept if not exists already
depts.add(new Dept(debtorId, deptAmount));
}
return depts;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,60 @@ void shouldAddEntry() {
assertThat(returnedWidget.getEntries().get(0).getId()).isNotBlank();
}

@Test
@WithMockOAuth2User
void shouldCalculateDeptsCorrectly(){
// given
UserWithPercentage mainUser = UserWithPercentage.builder()
.userId("test")
.percentage(0.25)
.build();
UserWithPercentage user2 = UserWithPercentage.builder()
.userId("user2")
.percentage(0.25)
.build();
UserWithPercentage user3 = UserWithPercentage.builder()
.userId("user3")
.percentage(0.25)
.build();
UserWithPercentage user4 = UserWithPercentage.builder()
.userId("user4")
.percentage(0.25)
.build();
ExpenseEntry entry1 = ExpenseEntry.builder()
.id("1")
.price(10)
.description("Bier")
.creatorId("test")
.involvedUsers(List.of(mainUser, user2, user3, user4))
.build();
ExpenseEntry entry2 = ExpenseEntry.builder()
.id("2")
.price(20)
.description("Bier2")
.creatorId("test")
.involvedUsers(List.of(mainUser, user2, user3, user4))
.build();

ExpenseSplitWidget widget = ExpenseSplitWidget.builder()
.id("wi-123")
.entries(List.of(entry1, entry2))
.build();

// when
List<Dept> depts = widget.calculateDeptsForUserId(mainUser.getUserId());

// then
assertThat(depts.get(0).getUserId()).isEqualTo(user2.getUserId());
assertThat(depts.get(0).getDeptAmount()).isEqualTo(user2.getPercentage()*entry1.getPrice()+user2.getPercentage()*entry2.getPrice());
assertThat(depts.get(1).getUserId()).isEqualTo(user3.getUserId());
assertThat(depts.get(1).getDeptAmount()).isEqualTo(user3.getPercentage()*entry1.getPrice()+user3.getPercentage()*entry2.getPrice());
assertThat(depts.get(2).getUserId()).isEqualTo(user4.getUserId());
assertThat(depts.get(2).getDeptAmount()).isEqualTo(user4.getPercentage()*entry1.getPrice()+user4.getPercentage()*entry2.getPrice());
assertThat(depts.size()==3).isTrue();
}


// @Test
// @WithMockGuestUser
// void shouldNotAddEntryIfUserIsGuest() {
Expand Down

0 comments on commit a7001be

Please sign in to comment.