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.data.statics;
18  
19  import io.github.tonywasher.joceanus.moneywise.exc.MoneyWiseDataException;
20  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
21  import io.github.tonywasher.joceanus.oceanus.resource.OceanusBundleId;
22  
23  /**
24   * Enumeration of LoanCategory Type Classes.
25   */
26  public enum MoneyWiseLoanCategoryClass
27          implements MoneyWiseCategoryInterface {
28      /**
29       * CreditCard.
30       * <p>
31       * This is a Credit Card account, which is a specialised form of a {@link #LOAN} account. It
32       * simply differs in reporting treatment in that overall spend is tracked.
33       */
34      CREDITCARD(1, 1),
35  
36      /**
37       * PrivateLoan.
38       * <p>
39       * This is a PrivateLoan account, which is a specialised form of a {@link #LOAN} account. It
40       * represents a loan to/from the client from/to an individual represented by a
41       * {@link MoneyWisePayeeClass#INDIVIDUAL} account.
42       */
43      PRIVATELOAN(2, 2),
44  
45      /**
46       * Generic Loan.
47       * <p>
48       * This is a Loan account which represents a loan to/from the client from/to a
49       * {@link MoneyWisePayeeClass#INSTITUTION} account.
50       */
51      LOAN(3, 3),
52  
53      /**
54       * Parent category.
55       * <p>
56       * This is used for the total of all categories and is used purely for reporting purposes.
57       */
58      PARENT(4, 0);
59  
60      /**
61       * The String name.
62       */
63      private String theName;
64  
65      /**
66       * Class Id.
67       */
68      private final int theId;
69  
70      /**
71       * Class Order.
72       */
73      private final int theOrder;
74  
75      /**
76       * Constructor.
77       *
78       * @param uId    the Id
79       * @param uOrder the default order.
80       */
81      MoneyWiseLoanCategoryClass(final int uId,
82                                 final int uOrder) {
83          theId = uId;
84          theOrder = uOrder;
85      }
86  
87      @Override
88      public int getClassId() {
89          return theId;
90      }
91  
92      @Override
93      public int getOrder() {
94          return theOrder;
95      }
96  
97      @Override
98      public String toString() {
99          /* If we have not yet loaded the name */
100         if (theName == null) {
101             /* Load the name */
102             theName = bundleIdForCategoryClass(this).getValue();
103         }
104 
105         /* return the name */
106         return theName;
107     }
108 
109     /**
110      * get value from id.
111      *
112      * @param id the id value
113      * @return the corresponding enum object
114      * @throws OceanusException on error
115      */
116     public static MoneyWiseLoanCategoryClass fromId(final int id) throws OceanusException {
117         for (MoneyWiseLoanCategoryClass myClass : values()) {
118             if (myClass.getClassId() == id) {
119                 return myClass;
120             }
121         }
122         throw new MoneyWiseDataException("Invalid ClassId for " + MoneyWiseStaticDataType.LOANTYPE.toString() + ":" + id);
123     }
124 
125     /**
126      * Determine whether the LoanCategoryType is a child, and needs a parent.
127      *
128      * @return <code>true</code> if the account category type is a child, <code>false</code>
129      * otherwise.
130      */
131     public boolean isChild() {
132         return switch (this) {
133             case LOAN, PRIVATELOAN, CREDITCARD -> true;
134             default -> false;
135         };
136     }
137 
138     /**
139      * Determine whether the LoanCategoryType can provide cashBack.
140      *
141      * @return <code>true</code> if the loan category type can provide cashBack, <code>false</code>
142      * otherwise.
143      */
144     public boolean canCashBack() {
145         return this == MoneyWiseLoanCategoryClass.CREDITCARD;
146     }
147 
148     /**
149      * Determine whether the LoanCategoryType is a parent category.
150      *
151      * @return <code>true</code> if the loan category type is a parent category, <code>false</code>
152      * otherwise.
153      */
154     public boolean isParentCategory() {
155         return this == PARENT;
156     }
157 
158     @Override
159     public boolean isTotals() {
160         return false;
161     }
162 
163     /**
164      * Obtain the resource bundleId for the category class.
165      *
166      * @param pClass the category class
167      * @return the resource bundleId
168      */
169     private static OceanusBundleId bundleIdForCategoryClass(final MoneyWiseLoanCategoryClass pClass) {
170         /* Create the map and return it */
171         return switch (pClass) {
172             case CREDITCARD -> MoneyWiseStaticResource.LOANTYPE_CREDIT;
173             case PRIVATELOAN -> MoneyWiseStaticResource.LOANTYPE_PRIVATE;
174             case LOAN -> MoneyWiseStaticResource.LOANTYPE_LOAN;
175             case PARENT -> MoneyWiseStaticResource.CATEGORYTYPE_PARENT;
176             default -> throw new IllegalArgumentException();
177         };
178     }
179 }