java 根据输入的数量修改消息

pu82cl6c  于 2023-06-04  发布在  Java
关注(0)|答案(1)|浏览(281)

我有下面的方法,它接受一个由逗号分隔的cardodes字符串(也可以是一个单一的汽车代码)和holdReason,但我的问题是关于cardodes的。
例如,carcodes可以如下所示:"ABC1234, TYZ8765"

public PostReturn scan(String carcode, String holdReason) {
    PostReturn pr = new PostReturn();
    if (Utils.nullOrBlank(carcode)) {
        // cannot lookup
        pr.setErrorMsg("No carcode to lookup");
        return pr;
    }

    // trim it first/remove white spaces
    String[] carcodes = carcode.trim().split("\\s*,\\s*");
    for (String carId: carcodes) {
        Kitcarcode kb = Kitcarcode.parsecarcode(carId);
        if (kb == null) {
            // invalid carId
            pr.setErrorMsg(carId + " was invalid - could not be parsed");
            return pr;
        }
        // lookup the kit
        CarHandler cph = CarHandler.findByKitcarcode(kb);
        // mark it as on hold
        cph.setAttribute(CarHandler.PREPROCESSING_HOLD_DATE, Utils.getDateAsMDYYYY(new Date()));
        cph.setAttribute(CarHandler.PREPROCESSING_HOLD_REASON, holdReason);
        cph.storeChanges();
        carProcessing(kb, cph);
        // confirm msg
        pr.setSuccessMsg(kb + " is marked as on carprocessing hold for reason: " + holdReason);
    }
    return pr;
}

我主要关注这行代码:pr.setSuccessMsg(kb + " is marked as on carprocessing hold for reason: " + holdReason);
如果有多个汽车代码,用逗号分隔,例如"ABC1234,TYZ8765"
由上面的代码行打印的消息是TYZ8765 is marked as on carprocessing hold for reason xyz。因为有两个carcodes,可以像下面这样打印吗?
ABC1234 and TYZ8765 are marked as on carprocessing hod for reason xyz

8hhllhi2

8hhllhi21#

看起来,打印的消息只包含最后一个 carcode 的原因是因为它所在的 * for循环 * 在每次迭代期间都会简单地覆盖上一个 pr.setSuccessMsg 调用。
由于 prholdReasonfor-loop 中没有更改,因此您可以将其放置在 for-loop 之后。
此外,在 for-loop 中,您可以使用 kb 值填充 String 值。
我们将使用 * for循环 * 来递增 count 值。
这将用于确定我们的 strings 值是否被1、2或更多 kb 值填充-这将确定我们的 strings 值是否应包含逗号或 “和”
在 * for循环 * 退出后,我们可以进一步使用 count 值来确定我们应该使用 “is” 还是 “and”
然后将 strings 值用作发送到 setSuccessMsg 方法的消息。

PostReturn pr = new PostReturn();
if (Utils.nullOrBlank(carcode)) {
    // cannot lookup
    pr.setErrorMsg("No carcode to lookup");
    return pr;
}
// trim it first/remove white spaces
String[] carcodes = carcode.trim().split("\\s*,\\s*");
StringBuilder strings = new StringBuilder();
int count = 0, length = carcodes.length;
for (String carId : carcodes) {
    Kitcarcode kb = Kitcarcode.parsecarcode(carId);
    if (kb == null) {
        // invalid carId
        pr.setErrorMsg(carId + " was invalid - could not be parsed");
        return pr;
    }
    // lookup the kit
    CarHandler cph = CarHandler.findByKitcarcode(kb);
    // mark it as on hold
    cph.setAttribute(CarHandler.PREPROCESSING_HOLD_DATE, Utils.getDateAsMDYYYY(new Date()));
    cph.setAttribute(CarHandler.PREPROCESSING_HOLD_REASON, holdReason);
    cph.storeChanges();
    carProcessing(kb, cph);
    if (count != 0) {
        if (length > 2) strings.append(", ");
        if (count == length - 1) {
            if (count == 1) strings.append(' ');
            strings.append("and ");
        }
    }
    strings.append(kb);
    count++;
}
strings.append(count == 1 ? " is " : " are ");
strings.append("marked as on carprocessing hold for reason: ");
strings.append(holdReason);
// confirm msg
pr.setSuccessMsg(strings.toString());
return pr;

例如,打印 strings 值将产生类似的输出。

ABC123, DEF456, and GHI789 are marked as on carprocessing hold for reason: 
ABC123 and DEF456 are marked as on carprocessing hold for reason: 
ABC123 is marked as on carprocessing hold for reason:

相关问题