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