]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/LedgerTransactionAccount.java
some renames to better reflect the function
[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 Mobile-Ledger.
4  * Mobile-Ledger 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  * Mobile-Ledger 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 Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.model;
19
20 import android.support.annotation.NonNull;
21
22 public class LedgerTransactionAccount {
23     private String accountName;
24     private float amount;
25     private boolean amountSet;
26     private String currency;
27
28     public LedgerTransactionAccount(String accountName, float amount) {
29         this(accountName, amount, null);
30     }
31     public LedgerTransactionAccount(String accountName, float amount, String currency) {
32         this.accountName = accountName;
33         this.amount = amount;
34         this.amountSet = true;
35         this.currency = currency;
36     }
37
38     public LedgerTransactionAccount(String accountName) {
39         this.accountName = accountName;
40     }
41
42     public String getAccountName() {
43         return accountName;
44     }
45     public String getShortAccountName() {
46         String result = accountName;
47         result = result.replaceAll("(?<=^|:)(.)[^:]+(?=:)", "$1");
48         return result;
49     }
50
51     public void setAccountName(String accountName) {
52         this.accountName = accountName;
53     }
54
55     public float getAmount() {
56         if (!amountSet) throw new IllegalStateException("Account amount is not set");
57
58         return amount;
59     }
60
61     public void setAmount(float account_amount) {
62         this.amount = account_amount;
63         this.amountSet = true;
64     }
65
66     public void resetAmount() {
67         this.amountSet = false;
68     }
69
70     public boolean isAmountSet() {
71         return amountSet;
72     }
73     public String getCurrency() {
74         return currency;
75     }
76     @NonNull
77     public String toString() {
78         if (!amountSet) return "";
79
80         StringBuilder sb = new StringBuilder();
81         if (currency != null) {
82             sb.append(currency);
83             sb.append(' ');
84         }
85         sb.append(String.format("%,1.2f", amount));
86
87         return sb.toString();
88     }
89 }