]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/LedgerAmount.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / LedgerAmount.java
1 /*
2  * Copyright © 2021 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe 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.
8  *
9  * MoLe 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.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.model;
19
20 import android.annotation.SuppressLint;
21
22 import androidx.annotation.NonNull;
23
24 import net.ktnx.mobileledger.dao.AccountValueDAO;
25 import net.ktnx.mobileledger.db.Account;
26 import net.ktnx.mobileledger.db.AccountValue;
27 import net.ktnx.mobileledger.db.DB;
28
29 public class LedgerAmount {
30     private final String currency;
31     private final float amount;
32     private long dbId;
33
34     public LedgerAmount(float amount, @NonNull String currency) {
35         this.currency = currency;
36         this.amount = amount;
37     }
38     public LedgerAmount(float amount) {
39         this.amount = amount;
40         this.currency = null;
41     }
42     static public LedgerAmount fromDBO(AccountValue dbo) {
43         final LedgerAmount ledgerAmount = new LedgerAmount(dbo.getValue(), dbo.getCurrency());
44         ledgerAmount.dbId = dbo.getId();
45         return ledgerAmount;
46     }
47     public AccountValue toDBO(Account account) {
48         final AccountValueDAO dao = DB.get()
49                                       .getAccountValueDAO();
50         AccountValue obj = new AccountValue();
51         obj.setId(dbId);
52         obj.setAccountId(account.getId());
53
54         obj.setCurrency(currency);
55         obj.setValue(amount);
56
57         return obj;
58     }
59     @SuppressLint("DefaultLocale")
60     @NonNull
61     public String toString() {
62         if (currency == null)
63             return String.format("%,1.2f", amount);
64         else
65             return String.format("%s %,1.2f", currency, amount);
66     }
67     public void propagateToAccount(@NonNull LedgerAccount acc) {
68         if (currency != null)
69             acc.addAmount(amount, currency);
70         else
71             acc.addAmount(amount);
72     }
73     public String getCurrency() {
74         return currency;
75     }
76     public float getAmount() {
77         return amount;
78     }
79 }