]> git.ktnx.net Git - mobile-ledger.git/blob - model/LedgerTransactionAccount.java
bump androidx.constraintlayout library version
[mobile-ledger.git] / model / LedgerTransactionAccount.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 androidx.annotation.NonNull;
21
22 import net.ktnx.mobileledger.db.TransactionAccount;
23 import net.ktnx.mobileledger.utils.Misc;
24
25 import java.util.Locale;
26
27 public class LedgerTransactionAccount {
28     private String accountName;
29     private String shortAccountName;
30     private float amount;
31     private boolean amountSet = false;
32     private String currency;
33     private String comment;
34     private boolean amountValid = true;
35     private long dbId;
36     public LedgerTransactionAccount(String accountName, float amount, String currency,
37                                     String comment) {
38         this.setAccountName(accountName);
39         this.amount = amount;
40         this.amountSet = true;
41         this.amountValid = true;
42         this.currency = Misc.emptyIsNull(currency);
43         this.comment = Misc.emptyIsNull(comment);
44     }
45     public LedgerTransactionAccount(String accountName) {
46         this.accountName = accountName;
47     }
48     public LedgerTransactionAccount(String accountName, String currency) {
49         this.accountName = accountName;
50         this.currency = Misc.emptyIsNull(currency);
51     }
52     public LedgerTransactionAccount(LedgerTransactionAccount origin) {
53         // copy constructor
54         setAccountName(origin.getAccountName());
55         setComment(origin.getComment());
56         if (origin.isAmountSet())
57             setAmount(origin.getAmount());
58         amountValid = origin.amountValid;
59         currency = origin.getCurrency();
60     }
61     public LedgerTransactionAccount(TransactionAccount dbo) {
62         this(dbo.getAccountName(), dbo.getAmount(), Misc.emptyIsNull(dbo.getCurrency()),
63                 Misc.emptyIsNull(dbo.getComment()));
64         amountSet = true;
65         amountValid = true;
66         dbId = dbo.getId();
67     }
68     public String getComment() {
69         return comment;
70     }
71     public void setComment(String comment) {
72         this.comment = comment;
73     }
74     public String getAccountName() {
75         return accountName;
76     }
77     public void setAccountName(String accountName) {
78         this.accountName = accountName;
79         shortAccountName = accountName.replaceAll("(?<=^|:)(.)[^:]+(?=:)", "$1");
80     }
81     public String getShortAccountName() {
82         return shortAccountName;
83     }
84     public float getAmount() {
85         if (!amountSet)
86             throw new IllegalStateException("Account amount is not set");
87
88         return amount;
89     }
90     public void setAmount(float account_amount) {
91         this.amount = account_amount;
92         this.amountSet = true;
93         this.amountValid = true;
94     }
95     public void resetAmount() {
96         this.amountSet = false;
97         this.amountValid = true;
98     }
99     public void invalidateAmount() {
100         this.amountValid = false;
101     }
102     public boolean isAmountSet() {
103         return amountSet;
104     }
105     public boolean isAmountValid() { return amountValid; }
106     public String getCurrency() {
107         return currency;
108     }
109     public void setCurrency(String currency) {
110         this.currency = Misc.emptyIsNull(currency);
111     }
112     @NonNull
113     public String toString() {
114         if (!amountSet)
115             return "";
116
117         StringBuilder sb = new StringBuilder();
118         if (currency != null) {
119             sb.append(currency);
120             sb.append(' ');
121         }
122         sb.append(String.format(Locale.US, "%,1.2f", amount));
123
124         return sb.toString();
125     }
126     public TransactionAccount toDBO() {
127         TransactionAccount dbo = new TransactionAccount();
128         dbo.setAccountName(accountName);
129         if (amountSet)
130             dbo.setAmount(amount);
131         dbo.setComment(comment);
132         dbo.setCurrency(Misc.nullIsEmpty(currency));
133         dbo.setId(dbId);
134
135         return dbo;
136     }
137 }