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.
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.
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/>.
18 package net.ktnx.mobileledger.model;
20 import androidx.annotation.NonNull;
21 import androidx.annotation.Nullable;
23 import net.ktnx.mobileledger.db.TransactionAccount;
24 import net.ktnx.mobileledger.utils.Misc;
26 import java.util.Locale;
28 public class LedgerTransactionAccount {
29 private String accountName;
30 private String shortAccountName;
32 private boolean amountSet = false;
34 private String currency;
35 private String comment;
36 private boolean amountValid = true;
38 public LedgerTransactionAccount(String accountName, float amount, String currency,
40 this.setAccountName(accountName);
42 this.amountSet = true;
43 this.amountValid = true;
44 this.currency = Misc.emptyIsNull(currency);
45 this.comment = Misc.emptyIsNull(comment);
47 public LedgerTransactionAccount(String accountName) {
48 this.accountName = accountName;
50 public LedgerTransactionAccount(String accountName, String currency) {
51 this.accountName = accountName;
52 this.currency = Misc.emptyIsNull(currency);
54 public LedgerTransactionAccount(LedgerTransactionAccount origin) {
56 setAccountName(origin.getAccountName());
57 setComment(origin.getComment());
58 if (origin.isAmountSet())
59 setAmount(origin.getAmount());
60 amountValid = origin.amountValid;
61 currency = origin.getCurrency();
63 public LedgerTransactionAccount(TransactionAccount dbo) {
64 this(dbo.getAccountName(), dbo.getAmount(), Misc.emptyIsNull(dbo.getCurrency()),
65 Misc.emptyIsNull(dbo.getComment()));
70 public String getComment() {
73 public void setComment(String comment) {
74 this.comment = comment;
76 public String getAccountName() {
79 public void setAccountName(String accountName) {
80 this.accountName = accountName;
81 shortAccountName = accountName.replaceAll("(?<=^|:)(.)[^:]+(?=:)", "$1");
83 public String getShortAccountName() {
84 return shortAccountName;
86 public float getAmount() {
88 throw new IllegalStateException("Account amount is not set");
92 public void setAmount(float account_amount) {
93 this.amount = account_amount;
94 this.amountSet = true;
95 this.amountValid = true;
97 public void resetAmount() {
98 this.amountSet = false;
99 this.amountValid = true;
101 public void invalidateAmount() {
102 this.amountValid = false;
104 public boolean isAmountSet() {
107 public boolean isAmountValid() { return amountValid; }
109 public String getCurrency() {
112 public void setCurrency(String currency) {
113 this.currency = Misc.emptyIsNull(currency);
116 public String toString() {
120 StringBuilder sb = new StringBuilder();
121 if (currency != null) {
125 sb.append(String.format(Locale.US, "%,1.2f", amount));
127 return sb.toString();
129 public TransactionAccount toDBO() {
130 TransactionAccount dbo = new TransactionAccount();
131 dbo.setAccountName(accountName);
133 dbo.setAmount(amount);
134 dbo.setComment(comment);
135 dbo.setCurrency(Misc.nullIsEmpty(currency));