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