Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 66 additions & 17 deletions src/main/java/com/odde/tdd/BudgetPlan.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,66 @@
package com.odde.tdd;

import java.time.LocalDate;
import java.util.List;

public class BudgetPlan {
private final BudgetRepo repo;

public BudgetPlan(BudgetRepo repo) {
this.repo = repo;
}

public double query(LocalDate start, LocalDate end) {
List<Budget> budgets = repo.findAll();
return 0;
}
}
package com.odde.tdd;

import java.time.*;
import java.util.List;

public class BudgetPlan {
private final BudgetRepo repo;

public BudgetPlan(BudgetRepo repo) {
this.repo = repo;
}

public double query(LocalDate start, LocalDate end) {
List<Budget> budgets = repo.findAll();
double result = 0;
int startYear = start.getYear();
int endYear = end.getYear();
int startMonth = start.getMonth().getValue();
int endMonth = end.getMonth().getValue();

if(startYear == endYear){

if(startMonth == endMonth){
double durationDays = end.toEpochDay() - start.toEpochDay() + 1;//Duration.between(start, end).toDays();
double lengthOfMonth = start.lengthOfMonth();
result += getAmount(budgets, YearMonth.of(start.getYear(), start.getMonth()))*durationDays/lengthOfMonth;
return result;
}else{

for(int i = startMonth; i<= endMonth;i++){
if(i == startMonth){
long lengthOfMonth = start.lengthOfMonth();
result += getAmount(budgets, YearMonth.of(startYear, start.getMonth())) * (lengthOfMonth - start.getDayOfMonth() +1) / lengthOfMonth;
}else if(i == endMonth){
result += getAmount(budgets, YearMonth.of(endYear, end.getMonth())) * end.getDayOfMonth() / end.lengthOfMonth();
}else{
result += getAmount(budgets, YearMonth.of(endYear, Month.of(i)));
}
}
}
}else{

for(int i = startYear; i<= endYear; i++){
if(i==startYear){
result += query(start, LocalDate.of(startYear,12,31));
}else if(i == endYear){
result += query(LocalDate.of(endYear,1,1), end);
}else{
result += query(LocalDate.of(i,1,1), LocalDate.of(i,12,31));
}
}

}

return result;
}

private long getAmount(List<Budget> list, YearMonth date){
for(Budget bd: list){
if(bd.getMonth().equals(date)){
return bd.getAmount();
}
}
return 0;
}
}
163 changes: 144 additions & 19 deletions src/test/java/com/odde/tdd/BudgetPlanTest.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,145 @@
package com.odde.tdd;

import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.util.Collections;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class BudgetPlanTest {
@Test
void no_budget() {
BudgetRepo repo = mock(BudgetRepo.class);
when(repo.findAll()).thenReturn(Collections.emptyList());
BudgetPlan plan = new BudgetPlan(repo);
assertEquals(0, plan.query(LocalDate.of(2023, 4, 20), LocalDate.of(2023, 6, 4)));
}
package com.odde.tdd;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class BudgetPlanTest {

// @BeforeAll
// public void initBudget(){
//
// }

@Test
void no_budget() {
BudgetRepo repo = mock(BudgetRepo.class);
when(repo.findAll()).thenReturn(Collections.emptyList());
BudgetPlan plan = new BudgetPlan(repo);
assertEquals(0, plan.query(LocalDate.of(2023, 4, 20), LocalDate.of(2023, 6, 4)));
}

@Test
public void budgetDaysIn1Month(){
BudgetRepo repo = mock(BudgetRepo.class);
when(repo.findAll()).thenReturn(
Arrays.asList(new Budget(YearMonth.of(2023,5), 3100),
new Budget(YearMonth.of(2023,6), 300),
new Budget(YearMonth.of(2023,7), 310),
new Budget(YearMonth.of(2023,8), 3100))
);
BudgetPlan plan = new BudgetPlan(repo);
LocalDate start = LocalDate.of(2023,5,2);
LocalDate end = LocalDate.of(2023,5,10);
assertEquals(900, plan.query(start, end));
}

@Test
public void budgetDaysIn2Month(){
BudgetRepo repo = mock(BudgetRepo.class);
when(repo.findAll()).thenReturn(
Arrays.asList(new Budget(YearMonth.of(2023,5), 3100),
new Budget(YearMonth.of(2023,6), 300),
new Budget(YearMonth.of(2023,7), 310),
new Budget(YearMonth.of(2023,8), 3100))
);
BudgetPlan plan = new BudgetPlan(repo);
LocalDate start = LocalDate.of(2023,5,2);
LocalDate end = LocalDate.of(2023,6,2);
assertEquals(3020, plan.query(start, end));
}

/**
* 3000+300+20
*/
@Test
public void budgetDaysIn3Month(){
BudgetRepo repo = mock(BudgetRepo.class);
when(repo.findAll()).thenReturn(
Arrays.asList(new Budget(YearMonth.of(2023,5), 3100),
new Budget(YearMonth.of(2023,6), 300),
new Budget(YearMonth.of(2023,7), 310),
new Budget(YearMonth.of(2023,8), 3100))
);
BudgetPlan plan = new BudgetPlan(repo);
LocalDate start = LocalDate.of(2023,5,2);
LocalDate end = LocalDate.of(2023,7,2);
assertEquals(3320, plan.query(start, end));
}

/**
* 3000+300+310+200
*/
@Test
public void budgetDaysIn4Month(){
BudgetRepo repo = mock(BudgetRepo.class);
when(repo.findAll()).thenReturn(
Arrays.asList(new Budget(YearMonth.of(2023,5), 3100),
new Budget(YearMonth.of(2023,6), 300),
new Budget(YearMonth.of(2023,7), 310),
new Budget(YearMonth.of(2023,8), 3100))
);
BudgetPlan plan = new BudgetPlan(repo);
LocalDate start = LocalDate.of(2023,5,2);
LocalDate end = LocalDate.of(2023,8,2);
assertEquals(3810, plan.query(start, end));
}

/**
* 300+20
*/
@Test
public void budgetDaysIn2Year(){
BudgetRepo repo = mock(BudgetRepo.class);
when(repo.findAll()).thenReturn(
Arrays.asList(new Budget(YearMonth.of(2023,11), 3000),
new Budget(YearMonth.of(2023,12), 310),
new Budget(YearMonth.of(2024,1), 310),
new Budget(YearMonth.of(2024,2), 3000)
)
);
BudgetPlan plan = new BudgetPlan(repo);
LocalDate start = LocalDate.of(2023,12,2);
LocalDate end = LocalDate.of(2024,1,2);
assertEquals(320, plan.query(start, end));
}

@Test
public void budgetDaysIn3Year(){
BudgetRepo repo = mock(BudgetRepo.class);
when(repo.findAll()).thenReturn(
Arrays.asList(
new Budget(YearMonth.of(2022,12), 3100),
new Budget(YearMonth.of(2023,1), 1000),
new Budget(YearMonth.of(2023,2), 1000),
new Budget(YearMonth.of(2023,3), 1000),
new Budget(YearMonth.of(2023,4), 1000),
new Budget(YearMonth.of(2023,5), 1000),
new Budget(YearMonth.of(2023,6), 1000),
new Budget(YearMonth.of(2023,7), 1000),
new Budget(YearMonth.of(2023,8), 1000),
new Budget(YearMonth.of(2023,9), 1000),
new Budget(YearMonth.of(2023,10), 1000),
new Budget(YearMonth.of(2023,11), 1000),
new Budget(YearMonth.of(2023,12), 1000),
new Budget(YearMonth.of(2024,1), 310),
new Budget(YearMonth.of(2024,2), 3000)
)
);
BudgetPlan plan = new BudgetPlan(repo);
LocalDate start = LocalDate.of(2022,12,2);
LocalDate end = LocalDate.of(2024,1,2);
assertEquals(15020, plan.query(start, end));
//3000+12*1000+20
}

}