July 20, 2018

Custom annotation Tax VAT GST calculation

Java Program for Custom annotation VAT calculation



This is a sample java program for custom annotation , showing vat calculation.

MainClass



public class Main {
public static void main(String[] args) throws SecurityException, NoSuchFieldException, NoSuchMethodException {
Insurance axa = new Insurance();
axa.setMaketPrice(200);
PriceModification priceModification = new PriceModification();
axa = (Insurance) priceModification.calculateTax(axa);
System.out.println(" supplier price : "+axa.getMaketPrice());
}
}


Price Modification class - Using reflection , based on the annotated value the tax and gst is calculated here



/**
 *
 */
package com.belazy.tax;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author gosling
 *
 */
public class PriceModification {
public Object calculateTax(Insurance axa) {
try {
Class refClass = axa.getClass();
Annotation taxAnno;
Method[] methods = refClass.getMethods();
for (Method thisMethod : methods) {
String methodName = thisMethod.getName();
if (thisMethod.getAnnotations().length > 0
&& methodName.startsWith("get")) {
for (Annotation annotation : thisMethod.getAnnotations()) {
if (annotation instanceof Tax) {
taxAnno = thisMethod.getAnnotation(Tax.class);
Tax tax = (Tax) taxAnno;
System.out.println(" *** "
+ thisMethod.invoke(axa, new Object[] {}));
double price = (Double) thisMethod.invoke(axa,
new Object[] {});
double supplierPrice = 0;
String setMethodName = null;
switch (tax.mode()) {
case PERCENTAGE:
double taxPrice = price * tax.amount() / 100;
supplierPrice = price + taxPrice;
setMethodName = methodName
.replace("get", "set");
Method ss1Method = refClass.getMethod(
setMethodName, double.class);
ss1Method.invoke(axa,
new Object[] { supplierPrice });
break;
case AMOUNT:
supplierPrice = price + tax.amount();
setMethodName = methodName
.replace("get", "set");
Method ss1Method1 = refClass.getMethod(
setMethodName, double.class);
ss1Method1.invoke(axa,
new Object[] { supplierPrice });
break;
default:
break;
}
}
}
}
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return axa;
}

}


Insurance.java - A Model POJO class



/**
 *
 */
package com.belazy.tax;

/**
 * @author asif rajguru
 *
 */
public class Insurance {
private String name;
private double supplierPrice;
private double maketPrice;
private double basePrice;
private double profit;
private double commision;

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}

/**
* @return the supplierPrice
*/
public double getSupplierPrice() {
return supplierPrice;
}

/**
* @param supplierPrice the supplierPrice to set
*/

public void setSupplierPrice(double supplierPrice) {
this.supplierPrice = supplierPrice;
}

/**
* @return the maketPrice
*/
@Tax(name="VAT",amount =5, mode=TaxMode.PERCENTAGE, type=TaxTypes.INCLUSIVE)
public double getMaketPrice() {
return maketPrice;
}

/**
* @param maketPrice the maketPrice to set
*/
public void setMaketPrice(double maketPrice) {
this.maketPrice = maketPrice;
}

/**
* @return the basePrice
*/
@Tax(name="VAT",amount =5, mode=TaxMode.PERCENTAGE, type=TaxTypes.INCLUSIVE)
public double getBasePrice() {
return basePrice;
}

/**
* @param basePrice the basePrice to set
*/
public void setBasePrice(double basePrice) {
this.basePrice = basePrice;
}

/**
* @return the profit
*/
@Tax(name="VAT",amount =100, mode=TaxMode.AMOUNT, type=TaxTypes.EXCLUSIVE)
public double getProfit() {
return profit;
}

/**
* @param profit the profit to set
*/
public void setProfit(double profit) {
this.profit = profit;
}

/**
* @return the commision
*/
@Tax(name="VAT",amount =1, mode=TaxMode.PERCENTAGE, type=TaxTypes.EXCLUSIVE)
public double getCommision() {
return commision;
}

/**
* @param commision the commision to set
*/
public void setCommision(double commision) {
this.commision = commision;
}

}

TaxType Enum 


/**
 * 
 */
package com.belazy.tax;

/**
 * @author jovil
 *
 */
public enum TaxTypes {
INCLUSIVE,EXCLUSIVE;

}


TaxMode Enum


/**
 * 
 */
package com.belazy.tax;

/**
 * @author jobit
 *
 */
public enum TaxMode {

AMOUNT,PERCENTAGE;
}

Tax annotation class - Custom annotation class


/**
 * 
 */
package com.belazy.tax;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author alan jhon
 * 
 */
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD })
public @interface Tax {
public String name();
public double amount();
public TaxMode mode() default TaxMode.PERCENTAGE;
public TaxTypes type() default TaxTypes.INCLUSIVE;
}


Output


input market price is 200
market price is : 210






No comments:

Post a Comment

Your feedback may help others !!!

Facebook comments