Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

temp #25

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

temp #25

Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@

package lk.ac.mrt.cse.dbs.simpleexpensemanager;

import android.annotation.SuppressLint;
import android.content.Context;

/**
*
*/
public class Constants {
@SuppressLint("StaticFieldLeak")
public static Context CONTEXT;
public static final String EXPENSE_MANAGER = "expense-manager";
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package lk.ac.mrt.cse.dbs.simpleexpensemanager.control;

import lk.ac.mrt.cse.dbs.simpleexpensemanager.data.AccountDAO;
import lk.ac.mrt.cse.dbs.simpleexpensemanager.data.TransactionDAO;
import lk.ac.mrt.cse.dbs.simpleexpensemanager.data.impl.PersistentAccountDAO;
import lk.ac.mrt.cse.dbs.simpleexpensemanager.data.impl.PersistentTransactionDAO;
import lk.ac.mrt.cse.dbs.simpleexpensemanager.data.model.Account;

public class PersistentExpenseManager extends ExpenseManager {
public PersistentExpenseManager() {
setup();
}

@Override
public void setup() {
AccountDAO sqliteMemoryAccountDAO = new PersistentAccountDAO();
setAccountsDAO(sqliteMemoryAccountDAO);

TransactionDAO sqliteTransactionDAO = new PersistentTransactionDAO();
setTransactionsDAO(sqliteTransactionDAO);

// dummy data
Account dummyAcct1 = new Account("12345eA", "Yoda Bank", "Anakin Skywalker", 10000.0);
Account dummyAcct2 = new Account("78945Z", "Clone BC", "Obi-Wan Kenobi", 80000.0);
getAccountsDAO().addAccount(dummyAcct1);
getAccountsDAO().addAccount(dummyAcct2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package lk.ac.mrt.cse.dbs.simpleexpensemanager.data.impl;

import java.util.ArrayList;
import java.util.List;

import lk.ac.mrt.cse.dbs.simpleexpensemanager.Constants;
import lk.ac.mrt.cse.dbs.simpleexpensemanager.data.AccountDAO;
import lk.ac.mrt.cse.dbs.simpleexpensemanager.data.exception.InvalidAccountException;
import lk.ac.mrt.cse.dbs.simpleexpensemanager.data.model.Account;
import lk.ac.mrt.cse.dbs.simpleexpensemanager.data.model.ExpenseType;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.*;

public class PersistentAccountDAO implements AccountDAO {
SQLiteDatabase db;
java.io.File filename = Constants.CONTEXT.getFilesDir();
public PersistentAccountDAO()
{
db = SQLiteDatabase.openOrCreateDatabase(filename.getAbsolutePath() + "/200592R.sqlite", null);
db.execSQL("CREATE TABLE IF NOT EXISTS Account(accountNo VARCHAR(50),bankName VARCHAR(50)," +
"accountHolderName VARCHAR(50), balance NUMERIC(10,2));");
}

@Override
public List<String> getAccountNumbersList() {
@SuppressLint("Recycle") Cursor results = db.rawQuery("Select accountNo from Account",null);
List<String> result = new ArrayList<>();
results.moveToFirst();
while(!results.isAfterLast())
{
result.add(results.getString(0));
results.moveToNext();
}
return result;
}

@Override
public List<Account> getAccountsList() {
@SuppressLint("Recycle") Cursor results = db.rawQuery("Select * from Account;",null);
List<Account> result = new ArrayList<>();
results.moveToFirst();

while(!results.isAfterLast())
{

result.add( new Account(results.getString(0),results.getString(1),results.getString(2),
Double.parseDouble(results.getString(3) ) ));

results.moveToNext();
}
return result;
}

@Override
public Account getAccount(String accountNo) throws InvalidAccountException {
@SuppressLint("Recycle") Cursor resultSet = db.rawQuery("Select * from Account where accountNo='"
+ accountNo+"';", null);

resultSet.moveToFirst();

if (resultSet.isAfterLast()) {
throw new InvalidAccountException("Account No:" + accountNo + " is not valid!");
}
return new Account(resultSet.getString(0), resultSet.getString(1), resultSet.getString(2),
Double.parseDouble(resultSet.getString(3)));
}

@Override
public void addAccount(Account account) {
db.execSQL("INSERT INTO Account VALUES('"+account.getAccountNo()+"','"+account.getBankName()+
"','"+account.getAccountHolderName()+"','"+account.getBalance()+"');");
}

@Override
public void removeAccount(String accountNo) throws InvalidAccountException {
db.execSQL("DELETE FROM Account WHERE accountNo='"+accountNo+"';");
}

@Override
public void updateBalance(String accountNo, ExpenseType expenseType, double amount) throws InvalidAccountException {
Account account = getAccount(accountNo);

double balance = account.getBalance();
if (ExpenseType.INCOME == expenseType) {
balance += amount;
} else
balance-=amount;
db.execSQL("UPDATE Account SET balance='"+balance+"' WHERE accountNo='"+accountNo+"';");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package lk.ac.mrt.cse.dbs.simpleexpensemanager.data.impl;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import lk.ac.mrt.cse.dbs.simpleexpensemanager.Constants;
import lk.ac.mrt.cse.dbs.simpleexpensemanager.data.TransactionDAO;
import lk.ac.mrt.cse.dbs.simpleexpensemanager.data.model.ExpenseType;
import lk.ac.mrt.cse.dbs.simpleexpensemanager.data.model.Transaction;

public class PersistentTransactionDAO implements TransactionDAO {

SQLiteDatabase db;
java.io.File filename = Constants.CONTEXT.getFilesDir();
public PersistentTransactionDAO()
{
db = SQLiteDatabase.openOrCreateDatabase(filename.getAbsolutePath() + "/200592R.sqlite", null);
db.execSQL("CREATE TABLE IF NOT EXISTS Transactions(accountNo VARCHAR(50),expenseType VARCHAR(50)," +
"amount NUMERIC(10,2), date_value Date);");
}


@Override
public void logTransaction(Date date, String accountNo, ExpenseType expenseType, double amount) {
db.execSQL("INSERT INTO Transactions VALUES('"+accountNo+"','"+((expenseType==ExpenseType.INCOME)?"INCOME":"EXPENSE")
+"','"+amount+"','"+date.toString()+"');");
}

@Override
public List<Transaction> getAllTransactionLogs() {
@SuppressLint("Recycle") Cursor results = db.rawQuery("Select * from Transactions",null);
results.moveToFirst();
List<Transaction> result = new ArrayList<Transaction>();
while(!results.isAfterLast())
{
result.add( new Transaction(new Date(results.getString(3)),results.getString(0),
((results.getString(1)=="INCOME")?ExpenseType.INCOME:ExpenseType.EXPENSE),
Double.parseDouble(results.getString(2) ) ));

results.moveToNext();
}
return result;
}

@Override
public List<Transaction> getPaginatedTransactionLogs(int limit) {
@SuppressLint("Recycle") Cursor results = db.rawQuery("Select * from Transactions ORDER BY date_value LIMIT "+limit,
null);
results.moveToFirst();
List<Transaction> result = new ArrayList<Transaction>();
while(!results.isAfterLast())
{
result.add( new Transaction(new Date(results.getString(3)),results.getString(0),
((results.getString(1)=="INCOME")?ExpenseType.INCOME:ExpenseType.EXPENSE),
Double.parseDouble(results.getString(2) ) ));

results.moveToNext();
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import lk.ac.mrt.cse.dbs.simpleexpensemanager.R;
import lk.ac.mrt.cse.dbs.simpleexpensemanager.control.ExpenseManager;
import lk.ac.mrt.cse.dbs.simpleexpensemanager.control.InMemoryDemoExpenseManager;
import lk.ac.mrt.cse.dbs.simpleexpensemanager.control.PersistentExpenseManager;

public class MainActivity extends AppCompatActivity {
private ExpenseManager expenseManager;
Expand Down Expand Up @@ -64,8 +64,12 @@ protected void onCreate(Bundle savedInstanceState) {
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);

/*** Begin generating dummy data for In-Memory implementation ***/
expenseManager = new InMemoryDemoExpenseManager();
// /*** Begin generating dummy data for In-Memory implementation ***/
// expenseManager = new InMemoryDemoExpenseManager();
// /*** END ***/

/*** Setup the persistent storage implementation ***/
expenseManager = new PersistentExpenseManager();
/*** END ***/
}

Expand Down