Java – create util class

I created a currency fomatter class I hope it is a util class that can be used by other applications

public class CurrencyUtil{
  public BigDecimal currencyUtil(RenderRequest renderRequest,RenderResponse renderResponse)
        throws IOException,PortletException {
        BigDecimal amount = new BigDecimal("123456789.99");  //Instead I want the amount to be set by the application.      
        ThemeDisplay themeDisplay = 
             (ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
        Locale locale = themeDisplay.getLocale();

        NumberFormat canadaFrench = NumberFormat.getCurrencyInstance(Locale.CANADA_FRENCH);
        NumberFormat canadaEnglish = NumberFormat.getCurrencyInstance(Locale.CANADA);
        BigDecimal amount = new BigDecimal("123456789.99");
        DecimalFormatSymbols symbols = ((DecimalFormat) canadaFrench).getDecimalFormatSymbols();
        symbols.setGroupingSeparator('.');
        ((DecimalFormat) canadaFrench).setDecimalFormatSymbols(symbols);

    System.out.println(canadaFrench.format(amount));
        System.out.println(canadaEnglish.format(amount));     
    //Need to have a return type which would return the formats
     return amount;
    }
}

Let other applications call this util class

import com.mypackage.CurrencyUtil;
 ...
 public int handleCurrency(RenderRequest request,RenderResponse response) {
String billAmount = "123456.99";
    CurrencyUtil CU = new currencyUtil();
//Need to call that util class and set this billAmount to BigDecimal amount in util class.
//Then it should return both the formats or the format I call in the application.
   System.out.println(canadaEnglish.format(billAmount);  //something like this
}

What changes have I made?

Solution

You need to create an object of the currencyutil class

CurrencyUtil CU = new CurrencyUtil();
//To call a method,BigDecimal value=CU.currencyUtil(request,response);

I suggest that the currentutil method should be static, which requires three parameters

public class CurrencyUtil
 {
  public static BigDecimal currencyUtil(
                RenderRequest renderRequest,RenderResponse renderResponse,String amountStr) throws IOException,PortletException 
    {
       BigDecimal amount = new BigDecimal(amountStr); 
       ...
    }
 }

You can call,

BigDecimal value=CurrencyUtil.currencyUtil(request,response,billAmount);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>