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
62 changes: 60 additions & 2 deletions src/main/java/Consumer.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,67 @@
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Consumer {


private LocalDate currDate;

Consumer(){
}

Consumer(LocalDate currDate){
this.currDate = currDate;
}

public InvoiceStatistics consumeInvoices(List<Invoice> invoices) {
// TODO: Implement the invoice processing logic
return null;
Map<Integer, Integer> invoicesCountByOrgMap = getInvoicesCountByOrg(invoices);
Map<Integer, Integer> overDueInvoicesCountByOrg = getOverDueInvoicesCountByOrg(invoices,currDate);
Map<Integer, Map<Integer, Integer>> invoicesCountByOrgMapByMonth = getInvoicesCountByOrgByMonth(invoices);
InvoiceStatistics invoiceStatistics = new InvoiceStatistics(invoicesCountByOrgMap,overDueInvoicesCountByOrg,invoicesCountByOrgMapByMonth);
return invoiceStatistics;
}

public Map<Integer, Integer> getOverDueInvoicesCountByOrg(List<Invoice> invoices, LocalDate currDate) {

if(null==currDate) {
currDate = LocalDate.now();
}

Map<Integer,Integer> result = new HashMap<>();
for (Invoice invoice : invoices) {
if( "unpaid".equalsIgnoreCase(invoice.getStatus()) && invoice.getDueDate().isAfter(currDate)){
int currCount = result.getOrDefault(invoice.getOrganisationId(),0);
result.put(invoice.getOrganisationId(),currCount+1);
}
}
return result;
}

public Map<Integer, Integer> getInvoicesCountByOrg(List<Invoice> invoices) {
Map<Integer,Integer> result = new HashMap<>();
for (Invoice invoice : invoices) {
int currCount = result.getOrDefault(invoice.getOrganisationId(),0);
result.put(invoice.getOrganisationId(),currCount+1);
}
return result;
}


public Map<Integer, Map<Integer,Integer>> getInvoicesCountByOrgByMonth(List<Invoice> invoices) {
Map<Integer,Map<Integer,Integer>> result = new HashMap<>();
for (Invoice invoice : invoices) {
if("paid".equalsIgnoreCase(invoice.getStatus())) {
Map<Integer,Integer> countByMonth = result.getOrDefault(invoice.getOrganisationId(),new HashMap<>());
int invCount = countByMonth.getOrDefault(invoice.getRaisedDate().getMonth().getValue(),0);
countByMonth.put(invoice.getRaisedDate().getMonth().getValue(),invCount+1);
result.put(invoice.getOrganisationId(),countByMonth);
}

}
return result;
}


}
26 changes: 24 additions & 2 deletions src/main/java/InvoiceStatistics.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
import java.util.Map;

public class InvoiceStatistics {
public InvoiceStatistics() {

private final Map<Integer, Integer> invoicesCountByOrgMap;
private final Map<Integer, Integer> overDueInvoicesCountByOrg;
private final Map<Integer, Map<Integer, Integer>> invoicesCountByOrgMapByMonth;

public InvoiceStatistics(Map<Integer, Integer> invoicesCountByOrgMap, Map<Integer, Integer> overDueInvoicesCountByOrg, Map<Integer, Map<Integer, Integer>> invoicesCountByOrgMapByMonth) {
this.invoicesCountByOrgMap = invoicesCountByOrgMap;
this.overDueInvoicesCountByOrg = overDueInvoicesCountByOrg;
this.invoicesCountByOrgMapByMonth = invoicesCountByOrgMapByMonth;
}
}

public Map<Integer, Integer> getInvoicesCountByOrgMap() {
return invoicesCountByOrgMap;
}

public Map<Integer, Integer> getOverDueInvoicesCountByOrg() {
return overDueInvoicesCountByOrg;
}

public Map<Integer, Map<Integer, Integer>> getInvoicesCountByOrgMapByMonth() {
return invoicesCountByOrgMapByMonth;
}
}
67 changes: 64 additions & 3 deletions src/test/java/ConsumerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@
import static org.junit.jupiter.api.Assertions.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.io.IOException;
import java.util.Map;

class ConsumerTest {

private Consumer consumer;
private CsvInvoiceReader csvReader;
private List<Invoice> testInvoices;
private InvoiceStatistics stats;

@BeforeEach
void setUp() throws IOException {
consumer = new Consumer();
csvReader = new CsvInvoiceReader();
LocalDate testCurrDate = LocalDate.parse("2019-10-14", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
consumer = new Consumer(testCurrDate);
CsvInvoiceReader csvReader = new CsvInvoiceReader();
testInvoices = csvReader.readInvoices("invoices.csv");
stats = consumer.consumeInvoices(testInvoices);
}

@Test
Expand All @@ -31,4 +36,60 @@ void testEmptyList() {
// For now, it will fail because the method returns null
assertNotNull(result);
}


@Test
@DisplayName("test record count match")
void testRecordCount(){
assertEquals(11,testInvoices.size());
}

@Test
@DisplayName("Total number of invoices per organisation should match")
void testInvoicesPerOrgCount(){
assertNotNull(stats);
Map<Integer,Integer> invoiceCountByOrg = stats.getInvoicesCountByOrgMap();

Map<Integer,Integer> expectedResultMap=new HashMap<>();
expectedResultMap.put(1000,5);
expectedResultMap.put(2000,6);
assertEquals(expectedResultMap,invoiceCountByOrg);
}

@Test
@DisplayName("Total number of Over due invoices per organisation should match")
void testOverDueInvoicesCountByOrg(){

assertNotNull(stats);
Map<Integer,Integer> overDueInvoiceCountByOrg = stats.getOverDueInvoicesCountByOrg();

Map<Integer,Integer> expectedResultMap=new HashMap<>();
expectedResultMap.put(1000,2);
expectedResultMap.put(2000,2);
assertEquals(expectedResultMap,overDueInvoiceCountByOrg);
}

@Test
@DisplayName("Total number of invoices per organisation per month should match")
void testInvoicesCountByOrgMapByMonth(){

assertNotNull(stats);
Map<Integer, Map<Integer, Integer>> invoiceCountByOrgByMonth = stats.getInvoicesCountByOrgMapByMonth();

Map<Integer,Map<Integer, Integer>> expectedResultMap=new HashMap<>();
HashMap<Integer, Integer> account1MonthlyMap = new HashMap<>();;
account1MonthlyMap.put(10,3);

expectedResultMap.put(1000, account1MonthlyMap);

HashMap<Integer, Integer> account2MonthlyMap = new HashMap<>();;
account2MonthlyMap.put(9,4);

expectedResultMap.put(2000,account2MonthlyMap);


assertEquals(expectedResultMap,invoiceCountByOrgByMonth);
}


}