]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java
4cf08f9ebf01b114ffd47a5a181ab6ebafafcc77
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / LedgerAccount.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 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
27 public class LedgerAccount {
28     private String name;
29     private String shortName;
30     private int level;
31     private String parentName;
32     private boolean hidden;
33     private boolean hiddenToBe;
34     private List<LedgerAmount> amounts;
35     static Pattern higher_account = Pattern.compile("^[^:]+:");
36
37     public LedgerAccount(String name) {
38         this.setName(name);
39         hidden = false;
40     }
41
42     public boolean isHidden() {
43         return hidden;
44     }
45
46     public void setHidden(boolean hidden) {
47         this.hidden = hidden;
48     }
49
50     public LedgerAccount(String name, float amount) {
51         this.setName(name);
52         this.hidden = false;
53         this.amounts = new ArrayList<LedgerAmount>();
54         this.addAmount(amount);
55     }
56
57     public void setName(String name) {
58         this.name = name;
59         stripName();
60     }
61
62     private void stripName() {
63         level = 0;
64         shortName = name;
65         StringBuilder parentBuilder = new StringBuilder();
66         while (true) {
67             Matcher m = higher_account.matcher(shortName);
68             if (m.find()) {
69                 level++;
70                 parentBuilder.append(m.group(0));
71                 shortName = m.replaceFirst("");
72             }
73             else break;
74         }
75         if (parentBuilder.length() > 0)
76             parentName = parentBuilder.substring(0, parentBuilder.length() - 1);
77         else parentName = null;
78     }
79
80     public String getName() {
81         return name;
82     }
83
84     public void addAmount(float amount, String currency) {
85         if (amounts == null ) amounts = new ArrayList<>();
86         amounts.add(new LedgerAmount(amount, currency));
87     }
88     public void addAmount(float amount) {
89         this.addAmount(amount, null);
90     }
91
92     public String getAmountsString() {
93         if ((amounts == null) || amounts.isEmpty()) return "";
94
95         StringBuilder builder = new StringBuilder();
96         for( LedgerAmount amount : amounts ) {
97             String amt = amount.toString();
98             if (builder.length() > 0) builder.append('\n');
99             builder.append(amt);
100         }
101
102         return builder.toString();
103     }
104
105     public int getLevel() {
106         return level;
107     }
108
109     @NonNull
110     public String getShortName() {
111         return shortName;
112     }
113
114     public String getParentName() {
115         return parentName;
116     }
117     public void togglehidden() {
118         hidden = !hidden;
119     }
120
121     public boolean isHiddenToBe() {
122         return hiddenToBe;
123     }
124     public void setHiddenToBe(boolean hiddenToBe) {
125         this.hiddenToBe = hiddenToBe;
126     }
127     public void toggleHiddenToBe() {
128         setHiddenToBe(!hiddenToBe);
129     }
130 }