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.
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.
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/>.
18 package net.ktnx.mobileledger.model;
20 import android.support.annotation.NonNull;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
27 public class LedgerAccount {
29 private String shortName;
31 private String parentName;
32 private boolean hidden;
33 private boolean hiddenToBe;
34 private List<LedgerAmount> amounts;
35 static Pattern reHigherAccount = Pattern.compile("^[^:]+:");
37 public LedgerAccount(String name) {
42 public boolean isHidden() {
46 public void setHidden(boolean hidden) {
50 public LedgerAccount(String name, float amount) {
53 this.amounts = new ArrayList<LedgerAmount>();
54 this.addAmount(amount);
57 public void setName(String name) {
62 private void stripName() {
65 StringBuilder parentBuilder = new StringBuilder();
67 Matcher m = reHigherAccount.matcher(shortName);
70 parentBuilder.append(m.group(0));
71 shortName = m.replaceFirst("");
75 if (parentBuilder.length() > 0)
76 parentName = parentBuilder.substring(0, parentBuilder.length() - 1);
77 else parentName = null;
80 public String getName() {
84 public void addAmount(float amount, String currency) {
85 if (amounts == null ) amounts = new ArrayList<>();
86 amounts.add(new LedgerAmount(amount, currency));
88 public void addAmount(float amount) {
89 this.addAmount(amount, null);
92 public String getAmountsString() {
93 if ((amounts == null) || amounts.isEmpty()) return "";
95 StringBuilder builder = new StringBuilder();
96 for( LedgerAmount amount : amounts ) {
97 String amt = amount.toString();
98 if (builder.length() > 0) builder.append('\n');
102 return builder.toString();
105 public int getLevel() {
110 public String getShortName() {
114 public String getParentName() {
117 public void togglehidden() {
121 public boolean isHiddenToBe() {
124 public void setHiddenToBe(boolean hiddenToBe) {
125 this.hiddenToBe = hiddenToBe;
127 public void toggleHiddenToBe() {
128 setHiddenToBe(!hiddenToBe);