Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
#5[30m]. Add currency to Account entity.
  • Loading branch information
Evgenii Kanivets committed Feb 3, 2016
commit 1a828ff71a2aa18a04bfb2baecfdd374face9720
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.SimpleAdapter;

import com.blogspot.e_kanivets.moneytracker.R;
import com.blogspot.e_kanivets.moneytracker.activity.base.BaseActivity;
Expand Down Expand Up @@ -74,8 +73,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
private void addAccount() {
String title = etTitle.getText().toString().trim();
int initSum = Integer.parseInt(etInitSum.getText().toString().trim());
String currency = (String) spinner.getSelectedItem();

new AccountController(new DbHelper(AddAccountActivity.this)).addAccount(title, initSum);
new AccountController(new DbHelper(AddAccountActivity.this))
.addAccount(title, initSum, currency);
}

public static List<String> getAllCurrencies() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public View getView(final int position, View convertView, ViewGroup parent) {
Account account = accounts.get(position);

viewHolder.tvTitle.setText(account.getTitle());
viewHolder.tvCurSum.setText(Integer.toString(account.getCurSum()));
viewHolder.tvCurSum.setText(Integer.toString(account.getCurSum())
+ " " + account.getCurrency());

return convertView;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ public List<Account> getAccounts() {
int idColIndex = cursor.getColumnIndex(DbHelper.ID_COLUMN);
int titleColIndex = cursor.getColumnIndex(DbHelper.TITLE_COLUMN);
int curSumColIndex = cursor.getColumnIndex(DbHelper.CUR_SUM_COLUMN);
int currencyColIndex = cursor.getColumnIndex(DbHelper.CURRENCY_COLUMN);

do {
// Read a account from DB
Account account = new Account(cursor.getInt(idColIndex),
cursor.getString(titleColIndex),
cursor.getInt(curSumColIndex));
cursor.getInt(curSumColIndex),
cursor.getString(currencyColIndex));

if (account.getTitle().equals(DbHelper.DEFAULT_ACCOUNT)) continue;

//Add account to list
accountList.add(account);
Expand All @@ -58,17 +62,20 @@ public void updateAccountById(int id, int diff) {
SQLiteDatabase db = dbHelper.getWritableDatabase();

// Read account from db
Cursor cursor = db.query(DbHelper.TABLE_ACCOUNTS, null, "id=?", new String[]{Integer.valueOf(id).toString()}, null, null, null);
Cursor cursor = db.query(DbHelper.TABLE_ACCOUNTS, null, "id=?",
new String[]{Integer.valueOf(id).toString()}, null, null, null);
Account account = null;
if (cursor.moveToFirst()) {
// Get indexes of columns
int idColIndex = cursor.getColumnIndex(DbHelper.ID_COLUMN);
int titleColIndex = cursor.getColumnIndex(DbHelper.TITLE_COLUMN);
int curSumColIndex = cursor.getColumnIndex(DbHelper.CUR_SUM_COLUMN);
int currencyColIndex = cursor.getColumnIndex(DbHelper.CURRENCY_COLUMN);

account = new Account(cursor.getInt(idColIndex),
cursor.getString(titleColIndex),
cursor.getInt(curSumColIndex));
cursor.getInt(curSumColIndex),
cursor.getString(currencyColIndex));
}

cursor.close();
Expand All @@ -77,25 +84,26 @@ public void updateAccountById(int id, int diff) {
ContentValues contentValues = new ContentValues();
contentValues.put(DbHelper.CUR_SUM_COLUMN, account.getCurSum() + diff);

db.update(DbHelper.TABLE_ACCOUNTS, contentValues, "id=?", new String[]{Integer.valueOf(id).toString()});
db.update(DbHelper.TABLE_ACCOUNTS, contentValues, "id=?",
new String[]{Integer.valueOf(id).toString()});
}
}

public void deleteAccount(Account account) {
// Delete the account from DB
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete(DbHelper.TABLE_ACCOUNTS, "id=?",
new String[]{Integer.toString(account.getId())});
db.delete(DbHelper.TABLE_ACCOUNTS, "id=?", new String[]{Integer.toString(account.getId())});
db.close();
}

public void addAccount(String title, int curSum) {
public void addAccount(String title, int curSum, String currency) {
//Add account to DB
SQLiteDatabase db = dbHelper.getWritableDatabase();

ContentValues contentValues = new ContentValues();
contentValues.put(DbHelper.TITLE_COLUMN, title);
contentValues.put(DbHelper.CUR_SUM_COLUMN, curSum);
contentValues.put(DbHelper.CURRENCY_COLUMN, currency);

db.insert(DbHelper.TABLE_ACCOUNTS, null, contentValues);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class DbHelper extends SQLiteOpenHelper {

/* DB_VERSION = 2 */
public static final String TABLE_ACCOUNTS = "accounts";
public static final String CURRENCY_COLUMN = "currency";

public static final String ACCOUNT_ID_COLUMN = "account_id";
public static final String CUR_SUM_COLUMN = "cur_sum";
Expand Down Expand Up @@ -107,7 +108,8 @@ private void createDbVersion2(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_ACCOUNTS + "("
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ TITLE_COLUMN + " TEXT,"
+ CUR_SUM_COLUMN + " INTEGER);");
+ CUR_SUM_COLUMN + " INTEGER,"
+ CURRENCY_COLUMN + " TEXT );");

/* Insert default account for all records from DB_VERSION = 1 */
ContentValues contentValues = new ContentValues();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ public class Account {
private int id;
private String title;
private int curSum;
private String currency;

public Account(int id, String title, int curSum) {
public Account(int id, String title, int curSum, String currency) {
this.id = id;
this.title = title;
this.curSum = curSum;
this.currency = currency;
}

public int getId() {
Expand All @@ -35,6 +37,10 @@ public void setCurSum(int curSum) {
this.curSum = curSum;
}

public String getCurrency() {
return currency;
}

@SuppressWarnings("SimplifiableIfStatement")
@Override
public boolean equals(Object o) {
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/layout/activity_add_record.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center_vertical|left"
android:paddingLeft="10dp"
android:text="@string/account" />
android:text="@string/account"
android:layout_weight="3" />

<Spinner
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner_account"
android:layout_marginRight="10dp"
Expand Down