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

Commit abb535a

Browse files
author
Evgenii Kanivets
committed
#80[30m]. Update EditText fields to work with doubles.
1 parent 7220d00 commit abb535a

File tree

6 files changed

+34
-20
lines changed

6 files changed

+34
-20
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,13 @@ public boolean onOptionsItemSelected(MenuItem item) {
7878

7979
private void addAccount() {
8080
String title = etTitle.getText().toString().trim();
81-
int initSum = Integer.parseInt(etInitSum.getText().toString().trim());
81+
double initSum = Double.parseDouble(etInitSum.getText().toString().trim());
8282
String currency = (String) spinner.getSelectedItem();
8383

84-
Account account = new Account(title, initSum, currency, 0);
84+
int intInitSum = (int) initSum;
85+
int decInitSum = (int) ((initSum - intInitSum) * 100);
86+
87+
Account account = new Account(title, intInitSum, currency, decInitSum);
8588

8689
accountController.create(account);
8790
}

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,27 @@ private void doTransfer() {
9494
Account fromAccount = accountList.get(spinnerFrom.getSelectedItemPosition());
9595
Account toAccount = accountList.get(spinnerTo.getSelectedItemPosition());
9696

97-
int fromAmount = -1;
97+
double fromAmount = -1;
9898
try {
99-
fromAmount = Integer.parseInt(etFromAmount.getText().toString());
99+
fromAmount = Double.parseDouble(etFromAmount.getText().toString());
100100
} catch (NumberFormatException e) {
101101
e.printStackTrace();
102102
}
103103

104-
int toAmount = -1;
104+
double toAmount = -1;
105105
try {
106-
toAmount = Integer.parseInt(etToAmount.getText().toString());
106+
toAmount = Double.parseDouble(etToAmount.getText().toString());
107107
} catch (NumberFormatException e) {
108108
e.printStackTrace();
109109
}
110110

111+
int intFromAmount = (int) fromAmount;
112+
int decFromAmount = (int) ((fromAmount - intFromAmount) * 100);
113+
int intToAmount = (int) toAmount;
114+
int decToAmount = (int) ((toAmount - intToAmount) * 100);
115+
111116
Transfer transfer = new Transfer(System.currentTimeMillis(), fromAccount.getId(),
112-
toAccount.getId(), fromAmount, toAmount, 0, 0);
117+
toAccount.getId(), intFromAmount, intToAmount, decFromAmount, decToAmount);
113118
transferController.create(transfer);
114119
}
115120
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,9 @@ private boolean prepareRecord() {
224224

225225
//Check if price is valid
226226
//noinspection UnusedAssignment
227-
int price = -1;
227+
double price = -1;
228228
try {
229-
price = Integer.parseInt(etPrice.getText().toString());
229+
price = Double.parseDouble(etPrice.getText().toString());
230230
} catch (NumberFormatException e) {
231231
e.printStackTrace();
232232
}
@@ -240,27 +240,33 @@ private boolean prepareRecord() {
240240
} else return false;
241241
}
242242

243-
private boolean doRecord(String title, String category, int price, @Nullable Account account) {
243+
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+
int decPrice = (int) ((price - intPrice) * 100);
248+
246249
if (mode == Mode.MODE_ADD) {
247250
switch (type) {
248251
case Record.TYPE_EXPENSE:
249252
recordController.create(new Record(new Date().getTime(), Record.TYPE_EXPENSE,
250-
title, new Category(category), price, account, account.getCurrency(), 0));
253+
title, new Category(category), intPrice, account, account.getCurrency(),
254+
decPrice));
251255
return true;
252256

253257
case Record.TYPE_INCOME:
254258
recordController.create(new Record(new Date().getTime(), Record.TYPE_INCOME,
255-
title, new Category(category), price, account, account.getCurrency(), 0));
259+
title, new Category(category), intPrice, account, account.getCurrency(),
260+
decPrice));
256261
return true;
257262

258263
default:
259264
return false;
260265
}
261266
} else if (mode == Mode.MODE_EDIT) {
262267
Record updatedRecord = new Record(record.getId(), record.getTime(), record.getType(),
263-
title, new Category(category), price, account, account.getCurrency(), 0);
268+
title, new Category(category), intPrice, account, account.getCurrency(),
269+
decPrice);
264270
recordController.update(updatedRecord);
265271

266272
return true;

app/src/main/res/layout/content_add_account.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
android:layout_width="match_parent"
3434
android:layout_height="wrap_content"
3535
android:hint="@string/initial_sum"
36-
android:inputType="numberSigned"
36+
android:inputType="numberDecimal|numberSigned"
3737
android:maxLines="1"
3838
android:singleLine="true" />
3939

app/src/main/res/layout/content_add_record.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
android:layout_width="match_parent"
3030
android:layout_height="wrap_content"
3131
android:hint="@string/price"
32-
android:inputType="numberSigned"
3332
android:maxLines="1"
34-
android:singleLine="true" />
33+
android:singleLine="true"
34+
android:inputType="numberDecimal" />
3535
</android.support.design.widget.TextInputLayout>
3636

3737
<android.support.design.widget.TextInputLayout

app/src/main/res/layout/content_transfer.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040
android:layout_height="wrap_content"
4141
android:ems="10"
4242
android:hint="@string/amount"
43-
android:inputType="number"
4443
android:maxLines="1"
4544
android:nextFocusDown="@+id/et_to_amount"
46-
android:singleLine="true" />
45+
android:singleLine="true"
46+
android:inputType="numberDecimal" />
4747
</android.support.design.widget.TextInputLayout>
4848
</LinearLayout>
4949

@@ -77,9 +77,9 @@
7777
android:layout_height="wrap_content"
7878
android:ems="10"
7979
android:hint="@string/amount"
80-
android:inputType="number"
8180
android:maxLines="1"
82-
android:singleLine="true" />
81+
android:singleLine="true"
82+
android:inputType="numberDecimal" />
8383
</android.support.design.widget.TextInputLayout>
8484
</LinearLayout>
8585

0 commit comments

Comments
 (0)