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