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