2 * Copyright © 2018 Damyan Ivanov.
3 * This file is part of Mobile-Ledger.
4 * Mobile-Ledger is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * Mobile-Ledger is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger;
20 import android.database.sqlite.SQLiteDatabase;
22 import java.util.ArrayList;
23 import java.util.Iterator;
24 import java.util.List;
26 class LedgerTransaction {
29 private String description;
30 private List<LedgerTransactionItem> items;
32 LedgerTransaction(String id, String date, String description) {
35 this.description = description;
36 this.items = new ArrayList<>();
38 LedgerTransaction(String date, String description) {
39 this(null, date, description);
41 void add_item(LedgerTransactionItem item) {
45 public String getDate() {
49 public void setDate(String date) {
53 public String getDescription() {
57 public void setDescription(String description) {
58 this.description = description;
61 Iterator<LedgerTransactionItem> getItemsIterator() {
62 return new Iterator<LedgerTransactionItem>() {
63 private int pointer = 0;
65 public boolean hasNext() {
66 return pointer < items.size();
70 public LedgerTransactionItem next() {
71 return hasNext() ? items.get(pointer++) : null;
75 public String getId() {
79 void insertInto(SQLiteDatabase db) {
80 db.execSQL("INSERT INTO transactions(id, date, " + "description) values(?, ?, ?)",
81 new String[]{id, date, description});
83 for(LedgerTransactionItem item : items) {
84 db.execSQL("INSERT INTO transaction_accounts(transaction_id, account_name, amount, "
85 + "currency) values(?, ?, ?, ?)", new Object[]{id, item.getAccountName(),
86 item.getAmount(), item.getCurrency()});