2 * Copyright © 2019 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.utils.Misc;
24 import java.util.Locale;
26 public class LedgerTransactionAccount {
27 private String accountName;
28 private String shortAccountName;
30 private boolean amountSet = false;
31 private String currency;
32 private String comment;
33 private boolean amountValid = true;
34 public LedgerTransactionAccount(String accountName, float amount, String currency,
36 this.setAccountName(accountName);
38 this.amountSet = true;
39 this.amountValid = true;
40 this.currency = Misc.emptyIsNull(currency);
41 this.comment = Misc.emptyIsNull(comment);
43 public LedgerTransactionAccount(String accountName) {
44 this.accountName = accountName;
46 public LedgerTransactionAccount(String accountName, String currency) {
47 this.accountName = accountName;
48 this.currency = Misc.emptyIsNull(currency);
50 public LedgerTransactionAccount(LedgerTransactionAccount origin) {
52 setAccountName(origin.getAccountName());
53 setComment(origin.getComment());
54 if (origin.isAmountSet())
55 setAmount(origin.getAmount());
56 amountValid = origin.amountValid;
57 currency = origin.getCurrency();
59 public String getComment() {
62 public void setComment(String comment) {
63 this.comment = comment;
65 public String getAccountName() {
68 public void setAccountName(String accountName) {
69 this.accountName = accountName;
70 shortAccountName = accountName.replaceAll("(?<=^|:)(.)[^:]+(?=:)", "$1");
72 public String getShortAccountName() {
73 return shortAccountName;
75 public float getAmount() {
77 throw new IllegalStateException("Account amount is not set");
81 public void setAmount(float account_amount) {
82 this.amount = account_amount;
83 this.amountSet = true;
84 this.amountValid = true;
86 public void resetAmount() {
87 this.amountSet = false;
88 this.amountValid = true;
90 public void invalidateAmount() {
91 this.amountValid = false;
93 public boolean isAmountSet() {
96 public boolean isAmountValid() { return amountValid; }
97 public String getCurrency() {
100 public void setCurrency(String currency) {
101 this.currency = Misc.emptyIsNull(currency);
104 public String toString() {
108 StringBuilder sb = new StringBuilder();
109 if (currency != null) {
113 sb.append(String.format(Locale.US, "%,1.2f", amount));
115 return sb.toString();