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