Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Commit 498e8fb

Browse files
author
Evgenii Kanivets
committed
#23[1h]. Implement some methods of Report and test them.
1 parent d530379 commit 498e8fb

File tree

4 files changed

+279
-5
lines changed

4 files changed

+279
-5
lines changed

app/src/main/java/com/blogspot/e_kanivets/moneytracker/model/Period.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,13 @@ public static Period yearPeriod() {
176176

177177
return new Period(first, last);
178178
}
179+
180+
@Override
181+
public boolean equals(Object o) {
182+
if (o instanceof Period) {
183+
Period period = (Period) o;
184+
return this.first.equals(period.getFirst())
185+
&& this.last.equals(period.getLast());
186+
} else return false;
187+
}
179188
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/model/Record.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.blogspot.e_kanivets.moneytracker.model;
22

3+
import android.support.annotation.NonNull;
4+
35
import com.blogspot.e_kanivets.moneytracker.DbHelper;
46
import com.blogspot.e_kanivets.moneytracker.MtApp;
57
import com.blogspot.e_kanivets.moneytracker.controller.CategoryController;
@@ -54,6 +56,18 @@ public Record(long time, int type, String title, String category, int price, lon
5456
this.currency = currency;
5557
}
5658

59+
public Record(@NonNull Record record) {
60+
this.id = record.getId();
61+
this.time = record.getTime();
62+
this.type = record.getType();
63+
this.title = record.getTitle();
64+
this.categoryId = record.getCategoryId();
65+
this.category = record.getCategory();
66+
this.price = record.getPrice();
67+
this.accountId = record.getAccountId();
68+
this.currency = record.getCurrency();
69+
}
70+
5771
public int getType() {
5872
return type;
5973
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/report/Report.java

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.support.annotation.NonNull;
44

5+
import com.blogspot.e_kanivets.moneytracker.model.ExchangeRate;
56
import com.blogspot.e_kanivets.moneytracker.model.Period;
67
import com.blogspot.e_kanivets.moneytracker.model.Record;
78
import com.blogspot.e_kanivets.moneytracker.report.base.IExchangeRateProvider;
@@ -23,17 +24,20 @@ public class Report implements IReport {
2324

2425
private String currency;
2526
private Period period;
26-
private List<Record> recordList;
2727
private IExchangeRateProvider rateProvider;
2828

29+
private double totalIncome;
30+
private double totalExpense;
31+
2932
public Report(String currency, Period period, List<Record> recordList, IExchangeRateProvider rateProvider) {
3033
if (currency == null || period == null || recordList == null || rateProvider == null)
3134
throw new NullPointerException("Params can't be null");
3235

3336
this.currency = currency;
3437
this.period = period;
35-
this.recordList = recordList;
3638
this.rateProvider = rateProvider;
39+
40+
makeReport(recordList);
3741
}
3842

3943
@NonNull
@@ -50,22 +54,66 @@ public Period getPeriod() {
5054

5155
@Override
5256
public double getTotal() {
53-
return 0;
57+
return getTotalExpense() + getTotalIncome();
5458
}
5559

5660
@Override
5761
public double getTotalIncome() {
58-
return 0;
62+
return totalIncome;
5963
}
6064

6165
@Override
6266
public double getTotalExpense() {
63-
return 0;
67+
return totalExpense;
6468
}
6569

6670
@NonNull
6771
@Override
6872
public List<CategoryRecord> getSummary() {
6973
return new ArrayList<>();
7074
}
75+
76+
private void makeReport(List<Record> recordList) {
77+
totalIncome = 0;
78+
totalExpense = 0;
79+
80+
List<Record> convertedRecordList = convertRecordList(recordList);
81+
82+
for (Record record : convertedRecordList) {
83+
switch (record.getType()) {
84+
case Record.TYPE_INCOME:
85+
totalIncome += record.getPrice();
86+
break;
87+
88+
case Record.TYPE_EXPENSE:
89+
totalExpense -= record.getPrice();
90+
break;
91+
92+
default:
93+
break;
94+
}
95+
}
96+
}
97+
98+
@NonNull
99+
private List<Record> convertRecordList(List<Record> recordList) {
100+
List<Record> convertedRecordList = new ArrayList<>();
101+
102+
for (Record record : recordList) {
103+
int convertedPrice = record.getPrice();
104+
105+
if (!currency.equals(record.getCurrency())) {
106+
ExchangeRate exchangeRate = rateProvider.getRate(record);
107+
if (exchangeRate == null) throw new NullPointerException("No exchange rate found");
108+
convertedPrice *= exchangeRate.getAmount();
109+
}
110+
111+
Record convertedRecord = new Record(record);
112+
convertedRecord.setPrice(convertedPrice);
113+
114+
convertedRecordList.add(convertedRecord);
115+
}
116+
117+
return convertedRecordList;
118+
}
71119
}
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
package com.blogspot.e_kanivets.moneytracker.report;
2+
3+
import android.support.annotation.Nullable;
4+
5+
import com.blogspot.e_kanivets.moneytracker.model.ExchangeRate;
6+
import com.blogspot.e_kanivets.moneytracker.model.Period;
7+
import com.blogspot.e_kanivets.moneytracker.model.Record;
8+
import com.blogspot.e_kanivets.moneytracker.report.base.IExchangeRateProvider;
9+
import com.blogspot.e_kanivets.moneytracker.report.base.IReport;
10+
11+
import org.junit.After;
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
15+
import java.util.ArrayList;
16+
import java.util.Date;
17+
import java.util.List;
18+
19+
import static org.junit.Assert.*;
20+
21+
/**
22+
* JUnit4 test case.
23+
* Created on 2/25/16.
24+
*
25+
* @author Evgenii Kanivets
26+
*/
27+
public class ReportTest {
28+
private String currency;
29+
private IExchangeRateProvider rateProvider;
30+
31+
@Before
32+
public void setUp() throws Exception {
33+
currency = "UAH";
34+
rateProvider = new TestProvider();
35+
}
36+
37+
@After
38+
public void tearDown() throws Exception {
39+
currency = null;
40+
rateProvider = null;
41+
}
42+
43+
@Test
44+
public void testForNulls() throws Exception {
45+
IReport report;
46+
47+
Period period = new Period(new Date(1), new Date());
48+
List<Record> recordList = new ArrayList<>();
49+
50+
try {
51+
report = new Report(null, period, recordList, rateProvider);
52+
} catch (NullPointerException e) {
53+
report = null;
54+
}
55+
56+
assertNull(report);
57+
58+
try {
59+
report = new Report(currency, null, recordList, rateProvider);
60+
} catch (NullPointerException e) {
61+
report = null;
62+
}
63+
64+
assertNull(report);
65+
66+
try {
67+
report = new Report(currency, period, null, rateProvider);
68+
} catch (NullPointerException e) {
69+
report = null;
70+
}
71+
72+
assertNull(report);
73+
74+
try {
75+
report = new Report(currency, period, recordList, null);
76+
} catch (NullPointerException e) {
77+
report = null;
78+
}
79+
80+
assertNull(report);
81+
82+
try {
83+
report = new Report(null, null, null, null);
84+
} catch (NullPointerException e) {
85+
report = null;
86+
}
87+
88+
assertNull(report);
89+
90+
try {
91+
report = new Report(currency, period, recordList, rateProvider);
92+
} catch (NullPointerException e) {
93+
report = null;
94+
}
95+
96+
assertNotNull(report);
97+
}
98+
99+
@Test
100+
public void testGetCurrency() throws Exception {
101+
Period period = new Period(new Date(1), new Date());
102+
List<Record> recordList = new ArrayList<>();
103+
104+
IReport report = new Report(currency, period, recordList, rateProvider);
105+
106+
assertEquals(currency, report.getCurrency());
107+
108+
currency = "KHI";
109+
report = new Report(currency, period, recordList, rateProvider);
110+
111+
assertEquals(currency, report.getCurrency());
112+
}
113+
114+
@Test
115+
public void testGetPeriod() throws Exception {
116+
Period period = new Period(new Date(1), new Date());
117+
List<Record> recordList = new ArrayList<>();
118+
119+
IReport report = new Report(currency, period, recordList, rateProvider);
120+
121+
assertEquals(period, report.getPeriod());
122+
123+
period = new Period(new Date(3), new Date(100));
124+
report = new Report(currency, period, recordList, rateProvider);
125+
126+
assertEquals(period, report.getPeriod());
127+
}
128+
129+
@Test
130+
public void testGetTotal() throws Exception {
131+
Period period = new Period(new Date(1), new Date());
132+
133+
List<Record> recordList = new ArrayList<>();
134+
recordList.add(new Record(0, Record.TYPE_INCOME, "1", "1", 10, 1, "USD"));
135+
recordList.add(new Record(1, Record.TYPE_EXPENSE, "2", "1", 2, 2, "UAH"));
136+
recordList.add(new Record(2, Record.TYPE_INCOME, "3", "1", 5, 1, "UAH"));
137+
recordList.add(new Record(3, Record.TYPE_EXPENSE, "4", "1", 10, 2, "USD"));
138+
139+
IReport report = new Report(currency, period, recordList, rateProvider);
140+
141+
double expectedTotal = 10 * 4 - 2 + 5 - 10 * 4;
142+
assertEquals(expectedTotal, report.getTotal(), 0.0000000001);
143+
}
144+
145+
@Test
146+
public void testGetTotalIncome() throws Exception {
147+
Period period = new Period(new Date(1), new Date());
148+
149+
List<Record> recordList = new ArrayList<>();
150+
recordList.add(new Record(0, Record.TYPE_INCOME, "1", "1", 10, 1, "USD"));
151+
recordList.add(new Record(1, Record.TYPE_EXPENSE, "2", "1", 2, 2, "UAH"));
152+
recordList.add(new Record(2, Record.TYPE_INCOME, "3", "1", 5, 1, "UAH"));
153+
recordList.add(new Record(3, Record.TYPE_EXPENSE, "4", "1", 10, 2, "USD"));
154+
155+
IReport report = new Report(currency, period, recordList, rateProvider);
156+
157+
double expectedTotal = 10 * 4 + 5;
158+
assertEquals(expectedTotal, report.getTotalIncome(), 0.0000000001);
159+
}
160+
161+
@Test
162+
public void testGetTotalExpense() throws Exception {
163+
Period period = new Period(new Date(1), new Date());
164+
165+
List<Record> recordList = new ArrayList<>();
166+
recordList.add(new Record(0, Record.TYPE_INCOME, "1", "1", 10, 1, "USD"));
167+
recordList.add(new Record(1, Record.TYPE_EXPENSE, "2", "1", 2, 2, "UAH"));
168+
recordList.add(new Record(2, Record.TYPE_INCOME, "3", "1", 5, 1, "UAH"));
169+
recordList.add(new Record(3, Record.TYPE_EXPENSE, "4", "1", 10, 2, "USD"));
170+
171+
IReport report = new Report(currency, period, recordList, rateProvider);
172+
173+
double expectedTotal = -2 - 10 * 4;
174+
assertEquals(expectedTotal, report.getTotalExpense(), 0.0000000001);
175+
}
176+
177+
@Test
178+
public void testGetSummary() throws Exception {
179+
180+
}
181+
182+
private static class TestProvider implements IExchangeRateProvider {
183+
184+
@Nullable
185+
@Override
186+
public ExchangeRate getRate(@Nullable Record record) {
187+
if (record == null) return null;
188+
189+
String fromCurrency = record.getCurrency();
190+
switch (fromCurrency) {
191+
case "USD":
192+
return new ExchangeRate(1, "USD", "UAH", 4);
193+
194+
case "AFN":
195+
return new ExchangeRate(0, "AFN", "UAH", 3);
196+
197+
default:
198+
return null;
199+
200+
}
201+
}
202+
}
203+
}

0 commit comments

Comments
 (0)