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

Commit caf0a80

Browse files
author
Evgenii Kanivets
committed
#80[30m]. Refactor double precision code.
1 parent 879427d commit caf0a80

File tree

7 files changed

+45
-46
lines changed

7 files changed

+45
-46
lines changed

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/account/AddAccountActivity.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,7 @@ private void addAccount() {
8181
double initSum = Double.parseDouble(etInitSum.getText().toString().trim());
8282
String currency = (String) spinner.getSelectedItem();
8383

84-
int intInitSum = (int) initSum;
85-
// Strange calculation because of double type precision issue
86-
int decInitSum = (int) Math.round(initSum * 100 - intInitSum * 100);
87-
88-
Account account = new Account(title, intInitSum, currency, decInitSum);
89-
84+
Account account = new Account(title, initSum, currency);
9085
accountController.create(account);
9186
}
9287
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/account/TransferActivity.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,8 @@ private void doTransfer() {
108108
e.printStackTrace();
109109
}
110110

111-
int intFromAmount = (int) fromAmount;
112-
// Strange calculation because of double type precision issue
113-
int decFromAmount = (int) Math.round(fromAmount * 100 - intFromAmount * 100);
114-
115-
int intToAmount = (int) toAmount;
116-
// Strange calculation because of double type precision issue
117-
int decToAmount = (int) Math.round(toAmount * 100 - intToAmount * 100);
118-
119111
Transfer transfer = new Transfer(System.currentTimeMillis(), fromAccount.getId(),
120-
toAccount.getId(), intFromAmount, intToAmount, decFromAmount, decToAmount);
112+
toAccount.getId(), fromAmount, toAmount);
121113
transferController.create(transfer);
122114
}
123115
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/record/AddRecordActivity.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -243,31 +243,24 @@ private boolean prepareRecord() {
243243
private boolean doRecord(String title, String category, double price, @Nullable Account account) {
244244
if (account == null) return false;
245245

246-
int intPrice = (int) price;
247-
// Strange calculation because of double type precision issue
248-
int decPrice = (int) Math.round(price * 100 - intPrice * 100);
249-
250246
if (mode == Mode.MODE_ADD) {
251247
switch (type) {
252248
case Record.TYPE_EXPENSE:
253249
recordController.create(new Record(new Date().getTime(), Record.TYPE_EXPENSE,
254-
title, new Category(category), intPrice, account, account.getCurrency(),
255-
decPrice));
250+
title, new Category(category), price, account, account.getCurrency()));
256251
return true;
257252

258253
case Record.TYPE_INCOME:
259254
recordController.create(new Record(new Date().getTime(), Record.TYPE_INCOME,
260-
title, new Category(category), intPrice, account, account.getCurrency(),
261-
decPrice));
255+
title, new Category(category), price, account, account.getCurrency()));
262256
return true;
263257

264258
default:
265259
return false;
266260
}
267261
} else if (mode == Mode.MODE_EDIT) {
268262
Record updatedRecord = new Record(record.getId(), record.getTime(), record.getType(),
269-
title, new Category(category), intPrice, account, account.getCurrency(),
270-
decPrice);
263+
title, new Category(category), price, account, account.getCurrency());
271264
recordController.update(updatedRecord);
272265

273266
return true;
@@ -277,4 +270,4 @@ title, new Category(category), intPrice, account, account.getCurrency(),
277270
}
278271

279272
public enum Mode {MODE_ADD, MODE_EDIT}
280-
}
273+
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/entity/base/BaseEntity.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,13 @@ protected boolean equals(String str1, String str2) {
1818
if (str1 == null) return str2 == null;
1919
else return str1.equals(str2);
2020
}
21-
}
21+
22+
protected int getInteger(double value) {
23+
return (int) value;
24+
}
25+
26+
protected int getDecimal(double value) {
27+
// Strange calculation because of double type precision issue
28+
return (int) Math.round(value * 100 - getInteger(value) * 100);
29+
}
30+
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/entity/data/Account.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ public Account(long id, String title, int curSum, String currency, int decimals)
2525
this.decimals = decimals;
2626
}
2727

28-
public Account(String title, int curSum, String currency, int decimals) {
28+
public Account(String title, double curSum, String currency) {
2929
this.id = -1;
3030
this.title = title;
31-
this.curSum = curSum;
3231
this.currency = currency;
33-
this.decimals = decimals;
32+
this.curSum = getInteger(curSum);
33+
this.decimals = getDecimal(curSum);
3434
}
3535

3636
protected Account(Parcel in) {
@@ -76,17 +76,15 @@ public String getCurrency() {
7676
public void put(double amount) {
7777
double sum = curSum + decimals / 100.0;
7878
sum += amount;
79-
curSum = (int) sum;
80-
// Strange calculation because of double type precision issue
81-
decimals = (int) Math.round(sum * 100 - curSum * 100);
79+
curSum = getInteger(sum);
80+
decimals = getDecimal(sum);
8281
}
8382

8483
public void take(double amount) {
8584
double sum = curSum + decimals / 100.0;
8685
sum -= amount;
87-
curSum = (int) sum;
88-
// Strange calculation because of double type precision issue
89-
decimals = (int) Math.round(sum * 100 - curSum * 100);
86+
curSum = getInteger(sum);
87+
decimals = getDecimal(sum);
9088
}
9189

9290
@SuppressWarnings("SimplifiableIfStatement")

app/src/main/java/com/blogspot/e_kanivets/moneytracker/entity/data/Record.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,30 @@ public Record(long id, long time, int type, String title, Category category, int
5050
this.decimals = decimals;
5151
}
5252

53-
public Record(long time, int type, String title, Category category, int price, Account account,
54-
String currency, int decimals) {
53+
public Record(long id, long time, int type, String title, Category category, double price,
54+
Account account, String currency) {
55+
this.id = id;
56+
this.time = time;
57+
this.type = type;
58+
this.title = title;
59+
this.category = category;
60+
this.account = account;
61+
this.currency = currency;
62+
this.price = getInteger(price);
63+
this.decimals = getDecimal(price);
64+
}
65+
66+
public Record(long time, int type, String title, Category category, double price, Account account,
67+
String currency) {
5568
this.id = -1;
5669
this.time = time;
5770
this.type = type;
5871
this.title = title;
5972
this.category = category;
60-
this.price = price;
6173
this.account = account;
6274
this.currency = currency;
63-
this.decimals = decimals;
75+
this.price = getInteger(price);
76+
this.decimals = getDecimal(price);
6477
}
6578

6679
protected Record(Parcel in) {

app/src/main/java/com/blogspot/e_kanivets/moneytracker/entity/data/Transfer.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,14 @@ public Transfer(long id, long time, long fromAccountId, long toAccountId, int fr
3232
this.toAmount = toAmount;
3333
}
3434

35-
public Transfer(long time, long fromAccountId, long toAccountId, int fromAmount,
36-
int toAmount, int fromDecimals, int toDecimals) {
35+
public Transfer(long time, long fromAccountId, long toAccountId, double fromAmount, double toAmount) {
3736
this.time = time;
3837
this.fromAccountId = fromAccountId;
3938
this.toAccountId = toAccountId;
40-
this.fromAmount = fromAmount;
41-
this.toAmount = toAmount;
42-
this.fromDecimals = fromDecimals;
43-
this.toDecimals = toDecimals;
39+
this.fromAmount = getInteger(fromAmount);
40+
this.fromDecimals = getDecimal(fromAmount);
41+
this.toAmount = getInteger(toAmount);
42+
this.toDecimals = getDecimal(toAmount);
4443
}
4544

4645
protected Transfer(Parcel in) {

0 commit comments

Comments
 (0)