diff --git a/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/Constants.java b/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/Constants.java index 26a832d833..30d6b343e7 100644 --- a/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/Constants.java +++ b/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/Constants.java @@ -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"; } + diff --git a/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/control/PersistentExpenseManager.java b/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/control/PersistentExpenseManager.java new file mode 100644 index 0000000000..467f8b8c47 --- /dev/null +++ b/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/control/PersistentExpenseManager.java @@ -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); + } +} diff --git a/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/data/impl/PersistentAccountDAO.java b/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/data/impl/PersistentAccountDAO.java new file mode 100644 index 0000000000..59327bf41a --- /dev/null +++ b/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/data/impl/PersistentAccountDAO.java @@ -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 getAccountNumbersList() { + @SuppressLint("Recycle") Cursor results = db.rawQuery("Select accountNo from Account",null); + List result = new ArrayList<>(); + results.moveToFirst(); + while(!results.isAfterLast()) + { + result.add(results.getString(0)); + results.moveToNext(); + } + return result; + } + + @Override + public List getAccountsList() { + @SuppressLint("Recycle") Cursor results = db.rawQuery("Select * from Account;",null); + List 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+"';"); + } +} \ No newline at end of file diff --git a/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/data/impl/PersistentTransactionDAO.java b/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/data/impl/PersistentTransactionDAO.java new file mode 100644 index 0000000000..0cfe60e0c9 --- /dev/null +++ b/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/data/impl/PersistentTransactionDAO.java @@ -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 getAllTransactionLogs() { + @SuppressLint("Recycle") Cursor results = db.rawQuery("Select * from Transactions",null); + results.moveToFirst(); + List result = new ArrayList(); + 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 getPaginatedTransactionLogs(int limit) { + @SuppressLint("Recycle") Cursor results = db.rawQuery("Select * from Transactions ORDER BY date_value LIMIT "+limit, + null); + results.moveToFirst(); + List result = new ArrayList(); + 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; + } +} diff --git a/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/ui/MainActivity.java b/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/ui/MainActivity.java index 46c5f78299..287814f257 100644 --- a/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/ui/MainActivity.java +++ b/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/ui/MainActivity.java @@ -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; @@ -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 ***/ }