]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/LedgerTransactionAccount.java
allow removal of the last profile
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / LedgerTransactionAccount.java
1 /*
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.
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.utils.Misc;
23
24 import java.util.Locale;
25
26 public class LedgerTransactionAccount {
27     private String accountName;
28     private String shortAccountName;
29     private float amount;
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,
35                                     String comment) {
36         this.setAccountName(accountName);
37         this.amount = amount;
38         this.amountSet = true;
39         this.amountValid = true;
40         this.currency = Misc.emptyIsNull(currency);
41         this.comment = Misc.emptyIsNull(comment);
42     }
43     public LedgerTransactionAccount(String accountName) {
44         this.accountName = accountName;
45     }
46     public LedgerTransactionAccount(String accountName, String currency) {
47         this.accountName = accountName;
48         this.currency = Misc.emptyIsNull(currency);
49     }
50     public LedgerTransactionAccount(LedgerTransactionAccount origin) {
51         // copy constructor
52         setAccountName(origin.getAccountName());
53         setComment(origin.getComment());
54         if (origin.isAmountSet())
55             setAmount(origin.getAmount());
56         amountValid = origin.amountValid;
57         currency = origin.getCurrency();
58     }
59     public String getComment() {
60         return comment;
61     }
62     public void setComment(String comment) {
63         this.comment = comment;
64     }
65     public String getAccountName() {
66         return accountName;
67     }
68     public void setAccountName(String accountName) {
69         this.accountName = accountName;
70         shortAccountName = accountName.replaceAll("(?<=^|:)(.)[^:]+(?=:)", "$1");
71     }
72     public String getShortAccountName() {
73         return shortAccountName;
74     }
75     public float getAmount() {
76         if (!amountSet)
77             throw new IllegalStateException("Account amount is not set");
78
79         return amount;
80     }
81     public void setAmount(float account_amount) {
82         this.amount = account_amount;
83         this.amountSet = true;
84         this.amountValid = true;
85     }
86     public void resetAmount() {
87         this.amountSet = false;
88         this.amountValid = true;
89     }
90     public void invalidateAmount() {
91         this.amountValid = false;
92     }
93     public boolean isAmountSet() {
94         return amountSet;
95     }
96     public boolean isAmountValid() { return amountValid; }
97     public String getCurrency() {
98         return currency;
99     }
100     public void setCurrency(String currency) {
101         this.currency = Misc.emptyIsNull(currency);
102     }
103     @NonNull
104     public String toString() {
105         if (!amountSet)
106             return "";
107
108         StringBuilder sb = new StringBuilder();
109         if (currency != null) {
110             sb.append(currency);
111             sb.append(' ');
112         }
113         sb.append(String.format(Locale.US, "%,1.2f", amount));
114
115         return sb.toString();
116     }
117 }