From 5742d83e1f55e3a212f516ebc7dbd3c4309a9c1a Mon Sep 17 00:00:00 2001 From: Sathveegan3105 <117098579+Sathveegan3105@users.noreply.github.com> Date: Mon, 12 Dec 2022 17:48:09 +0530 Subject: [PATCH 1/7] Create SQLiteAccDAO.java --- .../data/impl/SQLiteAccDAO.java | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/data/impl/SQLiteAccDAO.java diff --git a/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/data/impl/SQLiteAccDAO.java b/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/data/impl/SQLiteAccDAO.java new file mode 100644 index 0000000000..83b8bf8ff2 --- /dev/null +++ b/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/data/impl/SQLiteAccDAO.java @@ -0,0 +1,92 @@ +package lk.ac.mrt.cse.dbs.simpleexpensemanager.data.impl; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; + +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.database.Cursor; +import android.database.sqlite.*; +import android.os.Environment; +import android.support.annotation.NonNull; + +/** + * Created by Iresha on 05/12/2015. + */ +public class SQLiteAccountDAO implements AccountDAO { + SQLiteDatabase db; + java.io.File filename = Constants.CONTEXT.getFilesDir(); + public SQLiteAccountDAO() + { + db = SQLiteDatabase.openOrCreateDatabase(filename.getAbsolutePath() + "/130437A.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() { + Cursor resultSet = db.rawQuery("Select accountNo from Account",null); + List result = new ArrayList(); + resultSet.moveToFirst(); + while(!resultSet.isAfterLast()) + { + result.add(resultSet.getString(0)); + resultSet.moveToNext(); + } + return result; + } + + @Override + public List getAccountsList() { + Cursor resultSet = db.rawQuery("Select * from Account;",null); + List result = new ArrayList(); + resultSet.moveToFirst(); + while(!resultSet.isAfterLast()) + { + + result.add( new Account(resultSet.getString(0),resultSet.getString(1),resultSet.getString(2), Double.parseDouble(resultSet.getString(3) ) )); + resultSet.moveToNext(); + } + return result; + } + + @Override + public Account getAccount(String accountNo) throws InvalidAccountException { + 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+"';"); + } +} From ac36cd2126310f15db1c1c5fa5490d6b290eaccc Mon Sep 17 00:00:00 2001 From: Sathveegan3105 <117098579+Sathveegan3105@users.noreply.github.com> Date: Mon, 12 Dec 2022 17:51:51 +0530 Subject: [PATCH 2/7] Update SQLiteAccDAO.java --- .../cse/dbs/simpleexpensemanager/data/impl/SQLiteAccDAO.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/data/impl/SQLiteAccDAO.java b/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/data/impl/SQLiteAccDAO.java index 83b8bf8ff2..553f0f446f 100644 --- a/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/data/impl/SQLiteAccDAO.java +++ b/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/data/impl/SQLiteAccDAO.java @@ -19,9 +19,6 @@ import android.os.Environment; import android.support.annotation.NonNull; -/** - * Created by Iresha on 05/12/2015. - */ public class SQLiteAccountDAO implements AccountDAO { SQLiteDatabase db; java.io.File filename = Constants.CONTEXT.getFilesDir(); From bc20f8bd06dfc4476fe8f2a3c29c1ee1cbbb0a0a Mon Sep 17 00:00:00 2001 From: Sathveegan3105 <117098579+Sathveegan3105@users.noreply.github.com> Date: Tue, 13 Dec 2022 00:03:10 +0530 Subject: [PATCH 3/7] Delete SQLiteAccDAO.java --- .../data/impl/SQLiteAccDAO.java | 89 ------------------- 1 file changed, 89 deletions(-) delete mode 100644 app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/data/impl/SQLiteAccDAO.java diff --git a/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/data/impl/SQLiteAccDAO.java b/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/data/impl/SQLiteAccDAO.java deleted file mode 100644 index 553f0f446f..0000000000 --- a/app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/data/impl/SQLiteAccDAO.java +++ /dev/null @@ -1,89 +0,0 @@ -package lk.ac.mrt.cse.dbs.simpleexpensemanager.data.impl; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.ListIterator; -import java.util.Map; - -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.database.Cursor; -import android.database.sqlite.*; -import android.os.Environment; -import android.support.annotation.NonNull; - -public class SQLiteAccountDAO implements AccountDAO { - SQLiteDatabase db; - java.io.File filename = Constants.CONTEXT.getFilesDir(); - public SQLiteAccountDAO() - { - db = SQLiteDatabase.openOrCreateDatabase(filename.getAbsolutePath() + "/130437A.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() { - Cursor resultSet = db.rawQuery("Select accountNo from Account",null); - List result = new ArrayList(); - resultSet.moveToFirst(); - while(!resultSet.isAfterLast()) - { - result.add(resultSet.getString(0)); - resultSet.moveToNext(); - } - return result; - } - - @Override - public List getAccountsList() { - Cursor resultSet = db.rawQuery("Select * from Account;",null); - List result = new ArrayList(); - resultSet.moveToFirst(); - while(!resultSet.isAfterLast()) - { - - result.add( new Account(resultSet.getString(0),resultSet.getString(1),resultSet.getString(2), Double.parseDouble(resultSet.getString(3) ) )); - resultSet.moveToNext(); - } - return result; - } - - @Override - public Account getAccount(String accountNo) throws InvalidAccountException { - 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+"';"); - } -} From b729461bdabed56dd41addc75459f8b47986f5f0 Mon Sep 17 00:00:00 2001 From: Sathveegan3105 <117098579+Sathveegan3105@users.noreply.github.com> Date: Tue, 13 Dec 2022 04:41:24 +0530 Subject: [PATCH 4/7] Add files via upload --- .../control/PersistentExpenseManager.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/control/PersistentExpenseManager.java 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); + } +} From 048f4d5ece8a00821b12aed8ca8f2ad00de6432e Mon Sep 17 00:00:00 2001 From: Sathveegan3105 <117098579+Sathveegan3105@users.noreply.github.com> Date: Tue, 13 Dec 2022 04:42:23 +0530 Subject: [PATCH 5/7] Add files via upload --- .../data/impl/PersistentAccountDAO.java | 92 +++++++++++++++++++ .../data/impl/PersistentTransactionDAO.java | 66 +++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/data/impl/PersistentAccountDAO.java create mode 100644 app/src/main/java/lk/ac/mrt/cse/dbs/simpleexpensemanager/data/impl/PersistentTransactionDAO.java 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; + } +} From 080af91a0b1bcbf8c656fb3695e1c14a8f593f69 Mon Sep 17 00:00:00 2001 From: Sathveegan3105 <117098579+Sathveegan3105@users.noreply.github.com> Date: Tue, 13 Dec 2022 04:44:23 +0530 Subject: [PATCH 6/7] Update MainActivity.java --- .../cse/dbs/simpleexpensemanager/ui/MainActivity.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) 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 ***/ } From 35fe01e363fb4e63b5d467716d2a8ce80740e35a Mon Sep 17 00:00:00 2001 From: Sathveegan3105 <117098579+Sathveegan3105@users.noreply.github.com> Date: Tue, 13 Dec 2022 04:44:54 +0530 Subject: [PATCH 7/7] Update Constants.java --- .../lk/ac/mrt/cse/dbs/simpleexpensemanager/Constants.java | 6 ++++++ 1 file changed, 6 insertions(+) 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"; } +