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;
22 import net.ktnx.mobileledger.db.TransactionAccount;
23 import net.ktnx.mobileledger.utils.Misc;
25 import java.util.Locale;
27 public class LedgerTransactionAccount {
28 private String accountName;
29 private String shortAccountName;
31 private boolean amountSet = false;
32 private String currency;
33 private String comment;
34 private boolean amountValid = true;
36 public LedgerTransactionAccount(String accountName, float amount, String currency,
38 this.setAccountName(accountName);
40 this.amountSet = true;
41 this.amountValid = true;
42 this.currency = Misc.emptyIsNull(currency);
43 this.comment = Misc.emptyIsNull(comment);
45 public LedgerTransactionAccount(String accountName) {
46 this.accountName = accountName;
48 public LedgerTransactionAccount(String accountName, String currency) {
49 this.accountName = accountName;
50 this.currency = Misc.emptyIsNull(currency);
52 public LedgerTransactionAccount(LedgerTransactionAccount origin) {
54 setAccountName(origin.getAccountName());
55 setComment(origin.getComment());
56 if (origin.isAmountSet())
57 setAmount(origin.getAmount());
58 amountValid = origin.amountValid;
59 currency = origin.getCurrency();
61 public LedgerTransactionAccount(TransactionAccount dbo) {
62 this(dbo.getAccountName(), dbo.getAmount(), Misc.emptyIsNull(dbo.getCurrency()),
63 Misc.emptyIsNull(dbo.getComment()));
68 public String getComment() {
71 public void setComment(String comment) {
72 this.comment = comment;
74 public String getAccountName() {
77 public void setAccountName(String accountName) {
78 this.accountName = accountName;
79 shortAccountName = accountName.replaceAll("(?<=^|:)(.)[^:]+(?=:)", "$1");
81 public String getShortAccountName() {
82 return shortAccountName;
84 public float getAmount() {
86 throw new IllegalStateException("Account amount is not set");
90 public void setAmount(float account_amount) {
91 this.amount = account_amount;
92 this.amountSet = true;
93 this.amountValid = true;
95 public void resetAmount() {
96 this.amountSet = false;
97 this.amountValid = true;
99 public void invalidateAmount() {
100 this.amountValid = false;
102 public boolean isAmountSet() {
105 public boolean isAmountValid() { return amountValid; }
106 public String getCurrency() {
109 public void setCurrency(String currency) {
110 this.currency = Misc.emptyIsNull(currency);
113 public String toString() {
117 StringBuilder sb = new StringBuilder();
118 if (currency != null) {
122 sb.append(String.format(Locale.US, "%,1.2f", amount));
124 return sb.toString();
126 public TransactionAccount toDBO() {
127 TransactionAccount dbo = new TransactionAccount();
128 dbo.setAccountName(accountName);
130 dbo.setAmount(amount);
131 dbo.setComment(comment);
132 dbo.setCurrency(Misc.nullIsEmpty(currency));