【问题标题】:Creating a Util class创建一个 Util 类
【发布时间】:2012-01-31 17:32:19
【问题描述】:

我创建了一个货币格式化程序类。我希望这是一个 util 类,并且可以被其他应用程序使用。 现在我只取一个字符串,而不是我希望它由导入我的currencyUtil.jar的应用程序设置@

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;
    }
}

让调用这个 util 类的其他应用成为

 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
}

我做了什么改变?

【问题讨论】:

  • 如果可以的话,我会让实用程序类成为无状态的,并且您不需要创建实例,并且它的所有方法都是静态的。

标签: java format return currency


【解决方案1】:

你需要创建一个CurrencyUtil类的对象。

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

我建议currenyUtil方法应该是static,它需要三个参数。

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

你可以这样称呼它,

 BigDecimal value=CurrencyUtil.currencyUtil(request,response,billAmount);

【讨论】:

  • 如果我想将返回类型更改为 ArrayList 那么我的 BigDecimal 数量需要如何更改其类型转换。 public static ArrayList<String> currencyUtilArray(ArrayList<String> amountStr ) { BigDecimal amount = new BigDecimal(ArrayList<String> amountStr); //how would I read amount here, where the parameter is not string but arraylist<String> return amountStr; }
【解决方案2】:

根据 Joshua Bloch 的 Effective Java, 第 4 条:使用私有构造函数强制不可实例化:

public final class CurrencyUtil {

    // A private constructor
    private CurrencyUtil() {
        throw new AssertionError();
    }

    //Here goes your methods
}

【讨论】:

    猜你喜欢
    • 2020-04-13
    • 1970-01-01
    • 2021-08-08
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多