-1

我有一个常量文件abcder.constants,如下所示

public static final String ABC_abbject_EOD = "DDD_Report";
public static final String CDE_abbject_INTRADAY = "FFD_Report";

现在我有下面的方法,如下所示

public void Gen(String[] toAddress, String[] ccAddress, String abbject,
                String message, String defIdentifier, Date date)

现在在这个方法中可能有两种情况,要么defIdentifier是,null要么abbject参数有值

  1. 因此,如果defIdentifier为 null 则abbject具有来自 a 的值,abcderconstant因此在这种情况下我必须做一些事情。这取决于它可以是ABC_abbject_EODCDE_abbject_INTRADAY
  2. 如果defIdentifier不为空,abbject则为空,那么在这种情况下,我必须执行其他操作

所以,我开发了如下所示的代码,请告知这是正确的方法

if (defIdentifier != null && abbject== null)
{
    String s = defIdentifier + "-" + formatter.format(now.getTime()) + "." + "doc";
}

if (defIdentifier == null && abbject.equalsIgnoreCase(abcderconstants.ABC_abbject_EOD))
{
    String s = "DDD-Report" + "-" + formatter.format(now.getTime()) + "." + "doc";
}
if (defIdentifier == null && abbject.equalsIgnoreCase(abcderconstants.CDE_abbject_INTRADAY))
{
    String s = "FFD-Report" + "-" + formatter.format(now.getTime()) + "." + "doc";
}
4

2 回答 2

1
StringBuilder sb = new StringBuilder();
if (defIdentifier != null && abbject == null) {
    sb.append(defIdentifier);
} else if (defIdentifier == null && abbject != null ) {

    if(abbject.equalsIgnoreCase(abcderconstants.ABC_abbject_EOD)) {
         sb.append("DDD-Report");
    } else if (abbject.equalsIgnoreCase(abcderconstants.CDE_abbject_INTRADAY)) {
         sb.append("FFD-Report");
    } else {
         // throw invalid abbject type exception?
    }

} else {
    // both defIdentifier and abbject are either null or not null. Illegal args?
}

sb.append("-" + formatter.format(now.getTime()) + "." + "doc");
String s = sb.toString();
于 2013-07-16T08:04:26.960 回答
0

放在String s = ...if 语句之外。abbject检查是否为空可能是个好主意。请尝试使用更好的变量名。

String s = null;

if (defIdentifier != null && abbject == null) {
    s = defIdentifier + "-" + formatter.format(now.getTime()) + "." + "doc";
} 
else if (defIdentifier == null && abbject != null && abbject.equalsIgnoreCase(abcderconstants.ABC_abbject_EOD)) {
    s = "DDD-Report" + "-" + formatter.format(now.getTime()) + "." + "doc";
}
else if (defIdentifier == null && abbject != null && abbject.equalsIgnoreCase(abcderconstants.CDE_abbject_INTRADAY)) {
    s = "FFD-Report" + "-" + formatter.format(now.getTime()) + "." + "doc";
}
于 2013-07-16T05:39:53.230 回答