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

Commit 64f4a04

Browse files
author
Evgenii Kanivets
committed
#6[1h]. Add TransferController to keep track on transfers.
1 parent 694d3d0 commit 64f4a04

File tree

5 files changed

+169
-9
lines changed

5 files changed

+169
-9
lines changed

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import com.blogspot.e_kanivets.moneytracker.R;
1010
import com.blogspot.e_kanivets.moneytracker.activity.base.BaseActivity;
1111
import com.blogspot.e_kanivets.moneytracker.controller.AccountController;
12+
import com.blogspot.e_kanivets.moneytracker.controller.TransferController;
1213
import com.blogspot.e_kanivets.moneytracker.helper.DbHelper;
1314
import com.blogspot.e_kanivets.moneytracker.model.Account;
15+
import com.blogspot.e_kanivets.moneytracker.model.Transfer;
1416

1517
import java.util.ArrayList;
1618
import java.util.List;
@@ -21,7 +23,9 @@ public class TransferActivity extends BaseActivity {
2123
@SuppressWarnings("unused")
2224
private static final String TAG = "TransferActivity";
2325

24-
private AccountController accountController;
26+
private TransferController transferController;
27+
28+
private List<Account> accountList;
2529

2630
@Bind(R.id.spinner_from)
2731
AppCompatSpinner spinnerFrom;
@@ -39,7 +43,12 @@ protected int getContentViewId() {
3943

4044
@Override
4145
protected boolean initData() {
42-
accountController = new AccountController(new DbHelper(TransferActivity.this));
46+
DbHelper dbHelper = new DbHelper(TransferActivity.this);
47+
48+
AccountController accountController = new AccountController(dbHelper);
49+
transferController = new TransferController(dbHelper, accountController);
50+
51+
accountList = accountController.getAccounts();
4352

4453
return super.initData();
4554
}
@@ -48,8 +57,6 @@ protected boolean initData() {
4857
protected void initViews() {
4958
super.initViews();
5059

51-
List<Account> accountList = accountController.getAccounts();
52-
5360
List<String> accounts = new ArrayList<>();
5461
for (Account account : accountList) {
5562
accounts.add(account.getTitle());
@@ -88,6 +95,25 @@ public boolean onOptionsItemSelected(MenuItem item) {
8895
}
8996

9097
private void doTransfer() {
98+
Account fromAccount = accountList.get(spinnerFrom.getSelectedItemPosition());
99+
Account toAccount = accountList.get(spinnerTo.getSelectedItemPosition());
100+
101+
int fromAmount = -1;
102+
try {
103+
fromAmount = Integer.parseInt(etFromAmount.getText().toString());
104+
} catch (NumberFormatException e) {
105+
e.printStackTrace();
106+
}
107+
108+
int toAmount = -1;
109+
try {
110+
toAmount = Integer.parseInt(etToAmount.getText().toString());
111+
} catch (NumberFormatException e) {
112+
e.printStackTrace();
113+
}
91114

115+
Transfer transfer = new Transfer(System.currentTimeMillis(), fromAccount.getId(),
116+
toAccount.getId(), fromAmount, toAmount);
117+
transferController.create(transfer);
92118
}
93119
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/AccountController.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.blogspot.e_kanivets.moneytracker.helper.DbHelper;
1010
import com.blogspot.e_kanivets.moneytracker.model.Account;
1111
import com.blogspot.e_kanivets.moneytracker.model.Record;
12+
import com.blogspot.e_kanivets.moneytracker.model.Transfer;
1213

1314
import java.util.ArrayList;
1415
import java.util.List;
@@ -62,14 +63,14 @@ public List<Account> getAccounts() {
6263
}
6364

6465
@Nullable
65-
public Account read(int id) {
66+
public Account read(long id) {
6667
Account account = null;
6768

6869
SQLiteDatabase db = dbHelper.getReadableDatabase();
6970

7071
// Read accounts table from db
7172
Cursor cursor = db.query(DbHelper.TABLE_ACCOUNTS, null, "id=?",
72-
new String[]{Integer.toString(id)}, null, null, null);
73+
new String[]{Long.toString(id)}, null, null, null);
7374

7475
if (cursor.moveToFirst()) {
7576
// Get indexes of columns
@@ -94,7 +95,7 @@ public Account read(int id) {
9495
return account;
9596
}
9697

97-
public void updateAccountById(Account account) {
98+
public void update(Account account) {
9899
SQLiteDatabase db = dbHelper.getWritableDatabase();
99100

100101
ContentValues contentValues = new ContentValues();
@@ -148,7 +149,7 @@ public boolean recordAdded(@Nullable Record record) {
148149

149150
Log.d(TAG, "recordAdded: " + account);
150151

151-
updateAccountById(account);
152+
update(account);
152153

153154
return true;
154155
}
@@ -172,7 +173,7 @@ public boolean recordDeleted(@Nullable Record record) {
172173
break;
173174
}
174175

175-
updateAccountById(account);
176+
update(account);
176177

177178
return true;
178179
}
@@ -183,4 +184,21 @@ public boolean recordUpdated(@Nullable Record oldRecord, @Nullable Record newRec
183184

184185
return recordDeleted(oldRecord) && recordAdded(newRecord);
185186
}
187+
188+
public boolean transferDone(@Nullable Transfer transfer) {
189+
if(transfer == null) return false;
190+
191+
Account fromAccount = read(transfer.getFromAccountId());
192+
Account toAccount = read(transfer.getToAccountId());
193+
194+
if (fromAccount == null || toAccount == null) return false;
195+
196+
fromAccount.take(transfer.getFromAmount());
197+
toAccount.put(transfer.getToAmount());
198+
199+
update(fromAccount);
200+
update(toAccount);
201+
202+
return true;
203+
}
186204
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.blogspot.e_kanivets.moneytracker.controller;
2+
3+
import android.content.ContentValues;
4+
import android.database.sqlite.SQLiteDatabase;
5+
import android.support.annotation.Nullable;
6+
import android.util.Log;
7+
8+
import com.blogspot.e_kanivets.moneytracker.helper.DbHelper;
9+
import com.blogspot.e_kanivets.moneytracker.model.Transfer;
10+
11+
/**
12+
* Controller class to encapsulate transfer handling logic.
13+
* Created by evgenii_kanivets on 2/10/16.
14+
*/
15+
public class TransferController {
16+
@SuppressWarnings("unused")
17+
private static final String TAG = "TransferController";
18+
19+
private DbHelper dbHelper;
20+
private AccountController accountController;
21+
22+
public TransferController(DbHelper dbHelper, AccountController accountController) {
23+
this.dbHelper = dbHelper;
24+
this.accountController = accountController;
25+
}
26+
27+
public boolean create(@Nullable Transfer transfer) {
28+
if (transfer == null) return false;
29+
30+
SQLiteDatabase db = dbHelper.getWritableDatabase();
31+
32+
ContentValues contentValues = new ContentValues();
33+
contentValues.put(DbHelper.TIME_COLUMN, transfer.getTime());
34+
contentValues.put(DbHelper.FROM_ACCOUNT_ID_COLUMN, transfer.getFromAccountId());
35+
contentValues.put(DbHelper.TO_ACCOUND_ID_COLUMN, transfer.getToAccountId());
36+
contentValues.put(DbHelper.FROM_AMOUNT_COLUMN, transfer.getFromAmount());
37+
contentValues.put(DbHelper.TO_AMOUNT_COLUMN, transfer.getToAmount());
38+
39+
long id = db.insert(DbHelper.TABLE_TRANSFERS, null, contentValues);
40+
Log.d(TAG, "created transfer with id = " + id);
41+
42+
db.close();
43+
44+
accountController.transferDone(transfer);
45+
46+
return true;
47+
}
48+
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/fragment/AccountsFragment.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
134134
update();
135135
break;
136136

137+
case REQUEST_TRANSFER:
138+
update();
139+
break;
140+
137141
default:
138142
break;
139143
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.blogspot.e_kanivets.moneytracker.model;
2+
3+
/**
4+
* Entity class to represent transfer operation between accounts.
5+
* Created on 2/10/16.
6+
*
7+
* @author Evgenii Kanivets
8+
*
9+
* + ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
10+
+ TIME_COLUMN + " INTEGER,"
11+
+ FROM_ACCOUNT_ID_COLUMN + " INTEGER,"
12+
+ TO_ACCOUND_ID_COLUMN + " INTEGER,"
13+
+ FROM_AMOUNT_COLUMN + " INTEGER,"
14+
+ TO_AMOUNT_COLUMN + " INTEGER);");
15+
*/
16+
public class Transfer {
17+
private long id;
18+
private long time;
19+
private long fromAccountId;
20+
private long toAccountId;
21+
private int fromAmount;
22+
private int toAmount;
23+
24+
public Transfer(long id, long time, long fromAccountId, long toAccountId, int fromAmount, int toAmount) {
25+
this.id = id;
26+
this.time = time;
27+
this.fromAccountId = fromAccountId;
28+
this.toAccountId = toAccountId;
29+
this.fromAmount = fromAmount;
30+
this.toAmount = toAmount;
31+
}
32+
33+
public Transfer(long time, long fromAccountId, long toAccountId, int fromAmount, int toAmount) {
34+
this.time = time;
35+
this.fromAccountId = fromAccountId;
36+
this.toAccountId = toAccountId;
37+
this.fromAmount = fromAmount;
38+
this.toAmount = toAmount;
39+
}
40+
41+
public long getId() {
42+
return id;
43+
}
44+
45+
public long getTime() {
46+
return time;
47+
}
48+
49+
public long getFromAccountId() {
50+
return fromAccountId;
51+
}
52+
53+
public long getToAccountId() {
54+
return toAccountId;
55+
}
56+
57+
public int getFromAmount() {
58+
return fromAmount;
59+
}
60+
61+
public int getToAmount() {
62+
return toAmount;
63+
}
64+
}

0 commit comments

Comments
 (0)