]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/LedgerTransactionAccount.java
migrate to AndroidX
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / LedgerTransactionAccount.java
1 /*
2  * Copyright © 2018 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;
27     private String currency;
28
29     public LedgerTransactionAccount(String accountName, float amount) {
30         this(accountName, amount, null);
31     }
32     public LedgerTransactionAccount(String accountName, float amount, String currency) {
33         this.setAccountName(accountName);
34         this.amount = amount;
35         this.amountSet = true;
36         this.currency = currency;
37     }
38
39     public LedgerTransactionAccount(String accountName) {
40         this.accountName = accountName;
41     }
42
43     public String getAccountName() {
44         return accountName;
45     }
46     public String getShortAccountName() {
47         return shortAccountName;
48     }
49     public void setAccountName(String accountName) {
50         this.accountName = accountName;
51         shortAccountName = accountName.replaceAll("(?<=^|:)(.)[^:]+(?=:)", "$1");
52     }
53
54     public float getAmount() {
55         if (!amountSet) throw new IllegalStateException("Account amount is not set");
56
57         return amount;
58     }
59
60     public void setAmount(float account_amount) {
61         this.amount = account_amount;
62         this.amountSet = true;
63     }
64
65     public void resetAmount() {
66         this.amountSet = false;
67     }
68
69     public boolean isAmountSet() {
70         return amountSet;
71     }
72     public String getCurrency() {
73         return currency;
74     }
75     @NonNull
76     public String toString() {
77         if (!amountSet) return "";
78
79         StringBuilder sb = new StringBuilder();
80         if (currency != null) {
81             sb.append(currency);
82             sb.append(' ');
83         }
84         sb.append(String.format("%,1.2f", amount));
85
86         return sb.toString();
87     }
88 }