View Javadoc
1   /*
2    * MoneyWise: Finance Application
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.moneywise.quicken.file;
18  
19  import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
20  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseTransCategory;
21  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseTransCategoryClass;
22  import io.github.tonywasher.joceanus.moneywise.quicken.definitions.MoneyWiseQCategoryLineType;
23  import io.github.tonywasher.joceanus.moneywise.quicken.file.MoneyWiseQIFLine.MoneyWiseQIFStringLine;
24  
25  import java.util.List;
26  
27  /**
28   * Class representing a QIF Category record.
29   */
30  public class MoneyWiseQIFEventCategory
31          extends MoneyWiseQIFRecord<MoneyWiseQCategoryLineType>
32          implements Comparable<MoneyWiseQIFEventCategory> {
33      /**
34       * Item type.
35       */
36      protected static final String QIF_ITEM = "Cat";
37  
38      /**
39       * The Category Name.
40       */
41      private final String theName;
42  
43      /**
44       * The Category Description.
45       */
46      private final String theDesc;
47  
48      /**
49       * The Category Type.
50       */
51      private final boolean isIncome;
52  
53      /**
54       * Constructor.
55       *
56       * @param pFile     the QIF File
57       * @param pCategory the Event Category
58       */
59      public MoneyWiseQIFEventCategory(final MoneyWiseQIFFile pFile,
60                                       final MoneyWiseTransCategory pCategory) {
61          /* Call super-constructor */
62          super(pFile, MoneyWiseQCategoryLineType.class);
63  
64          /* Store data */
65          theName = pCategory.getName();
66          theDesc = pCategory.getDesc();
67  
68          /* Determine whether this is an income category */
69          final MoneyWiseTransCategoryClass myClass = pCategory.getCategoryTypeClass();
70          isIncome = myClass.isIncome();
71  
72          /* Build lines */
73          addLine(new MoneyWiseQIFCategoryNameLine(theName));
74          if (theDesc != null) {
75              addLine(new MoneyWiseQIFCategoryDescLine(theDesc));
76          }
77          if (isIncome) {
78              addLine(new MoneyWiseQIFCategoryIncomeLine());
79          } else {
80              addLine(new MoneyWiseQIFCategoryExpenseLine());
81          }
82      }
83  
84      /**
85       * Constructor.
86       *
87       * @param pFile  the QIF File
88       * @param pLines the data lines
89       */
90      protected MoneyWiseQIFEventCategory(final MoneyWiseQIFFile pFile,
91                                          final List<String> pLines) {
92          /* Call super-constructor */
93          super(pFile, MoneyWiseQCategoryLineType.class);
94  
95          /* Determine details */
96          String myName = null;
97          String myDesc = null;
98          boolean bIsIncome = false;
99  
100         /* Loop through the lines */
101         for (String myLine : pLines) {
102             /* Determine the category */
103             final MoneyWiseQCategoryLineType myType = MoneyWiseQCategoryLineType.parseLine(myLine);
104             if (myType != null) {
105                 /* Access data */
106                 final String myData = myLine.substring(myType.getSymbol().length());
107 
108                 /* Switch on line type */
109                 switch (myType) {
110                     case NAME:
111                         addLine(new MoneyWiseQIFCategoryNameLine(myData));
112                         myName = myData;
113                         break;
114                     case DESCRIPTION:
115                         addLine(new MoneyWiseQIFCategoryDescLine(myData));
116                         myDesc = myData;
117                         break;
118                     case INCOME:
119                         addLine(new MoneyWiseQIFCategoryIncomeLine());
120                         bIsIncome = true;
121                         break;
122                     case EXPENSE:
123                         addLine(new MoneyWiseQIFCategoryExpenseLine());
124                         bIsIncome = false;
125                         break;
126                     case TAX:
127                     default:
128                         break;
129                 }
130             }
131         }
132 
133         /* Build details */
134         theName = myName;
135         theDesc = myDesc;
136         isIncome = bIsIncome;
137     }
138 
139     @Override
140     public String toString() {
141         return getName();
142     }
143 
144     /**
145      * Obtain the Name.
146      *
147      * @return the Name
148      */
149     public String getName() {
150         return theName;
151     }
152 
153     /**
154      * Obtain the Description.
155      *
156      * @return the description
157      */
158     public String getDesc() {
159         return theDesc;
160     }
161 
162     /**
163      * Is the Category an income category.
164      *
165      * @return true/false
166      */
167     public boolean isIncome() {
168         return isIncome;
169     }
170 
171     /**
172      * Is the Category an expense category.
173      *
174      * @return true/false
175      */
176     public boolean isExpense() {
177         return !isIncome;
178     }
179 
180     @Override
181     public int compareTo(final MoneyWiseQIFEventCategory pThat) {
182         return theName.compareTo(pThat.getName());
183     }
184 
185     /**
186      * The Category Name line.
187      */
188     public static class MoneyWiseQIFCategoryNameLine
189             extends MoneyWiseQIFStringLine<MoneyWiseQCategoryLineType> {
190         /**
191          * Constructor.
192          *
193          * @param pName the Name
194          */
195         protected MoneyWiseQIFCategoryNameLine(final String pName) {
196             /* Call super-constructor */
197             super(pName);
198         }
199 
200         @Override
201         public MoneyWiseQCategoryLineType getLineType() {
202             return MoneyWiseQCategoryLineType.NAME;
203         }
204 
205         /**
206          * Obtain name.
207          *
208          * @return the name
209          */
210         public String getName() {
211             return getValue();
212         }
213     }
214 
215     /**
216      * The Category Description line.
217      */
218     public static class MoneyWiseQIFCategoryDescLine
219             extends MoneyWiseQIFStringLine<MoneyWiseQCategoryLineType> {
220         /**
221          * Constructor.
222          *
223          * @param pDesc the Description
224          */
225         protected MoneyWiseQIFCategoryDescLine(final String pDesc) {
226             /* Call super-constructor */
227             super(pDesc);
228         }
229 
230         @Override
231         public MoneyWiseQCategoryLineType getLineType() {
232             return MoneyWiseQCategoryLineType.DESCRIPTION;
233         }
234 
235         /**
236          * Obtain description.
237          *
238          * @return the description
239          */
240         public String getDescription() {
241             return getValue();
242         }
243     }
244 
245     /**
246      * The Category Income line.
247      */
248     public static class MoneyWiseQIFCategoryIncomeLine
249             extends MoneyWiseQIFLine<MoneyWiseQCategoryLineType> {
250         /**
251          * Constructor.
252          */
253         protected MoneyWiseQIFCategoryIncomeLine() {
254         }
255 
256         @Override
257         public MoneyWiseQCategoryLineType getLineType() {
258             return MoneyWiseQCategoryLineType.INCOME;
259         }
260 
261         @Override
262         public String toString() {
263             return getLineType().getSymbol();
264         }
265 
266         @Override
267         protected void formatData(final OceanusDataFormatter pFormatter,
268                                   final StringBuilder pBuilder) {
269             /* No data */
270         }
271     }
272 
273     /**
274      * The Category Expense line.
275      */
276     public static class MoneyWiseQIFCategoryExpenseLine
277             extends MoneyWiseQIFLine<MoneyWiseQCategoryLineType> {
278         /**
279          * Constructor.
280          */
281         protected MoneyWiseQIFCategoryExpenseLine() {
282         }
283 
284         @Override
285         public MoneyWiseQCategoryLineType getLineType() {
286             return MoneyWiseQCategoryLineType.EXPENSE;
287         }
288 
289         @Override
290         public String toString() {
291             return getLineType().getSymbol();
292         }
293 
294         @Override
295         protected void formatData(final OceanusDataFormatter pFormatter,
296                                   final StringBuilder pBuilder) {
297             /* No data */
298         }
299     }
300 
301     /**
302      * The Category Tax line.
303      */
304     public static class MoneyWiseQIFCategoryTaxLine
305             extends MoneyWiseQIFLine<MoneyWiseQCategoryLineType> {
306         /**
307          * Constructor.
308          */
309         protected MoneyWiseQIFCategoryTaxLine() {
310         }
311 
312         @Override
313         public MoneyWiseQCategoryLineType getLineType() {
314             return MoneyWiseQCategoryLineType.TAX;
315         }
316 
317         @Override
318         public String toString() {
319             return getLineType().getSymbol();
320         }
321 
322         @Override
323         protected void formatData(final OceanusDataFormatter pFormatter,
324                                   final StringBuilder pBuilder) {
325             /* No data */
326         }
327     }
328 }