基于输入数量修改消息。

huangapple go评论70阅读模式
英文:

modify the message based on the number of inputs

问题

我主要关注这行代码:
pr.setSuccessMsg(kb + " is marked as on carprocessing hold for reason: " + holdReason);

如果有多个汽车代码,用逗号分隔,例如 ABC1234,TYZ8765

上述代码行打印的消息是 TYZ8765 is marked as on carprocessing hold for reason xyz。由于有两个汽车代码,是否可以像下面这样打印?

ABC1234 and TYZ8765 are marked as on carprocessing hod for reason xyz

英文:

I have the following method which accepts a string of carcodes separated by a comma (it can be a single car code as well) and holdReason but my question is about carcodes.

For example, the carcodes can be like the following: "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;
}

I'm mainly concerned about this line of code:
pr.setSuccessMsg(kb + " is marked as on carprocessing hold for reason: " + holdReason);

If there are multiple car codes, separated by comma, for example "ABC1234,TYZ8765"

the message printed by the above line of code is TYZ8765 is marked as on carprocessing hold for reason xyz. Since there are two carcodes , is it possible to print it like the following?

ABC1234 and TYZ8765 are marked as on carprocessing hod for reason xyz

答案1

得分: 1

看起来打印的消息之所以只包含最后一个 carcode 是因为它在其中的 for循环 中,在每次迭代期间仅会覆盖前一个 pr.setSuccessMsg 调用。

由于 prholdReasonfor循环 内部没有更改,您可以将它放在 for循环 之后。

此外,在 for循环 内部,您可以填充一个 String 值,其中包含 kb 的值。

我们将利用 for循环 来递增一个 count 值。
这将用于确定我们的 strings 值是否被填充了1、2或更多个 kb 值,这将决定我们的 strings 值是否应该包含逗号或 "and"

for循环 退出后,我们还可以进一步使用 count 值来确定我们是否应该使用 "is" 还是 "and"

然后,strings 值用作发送给 setSuccessMsg 方法的消息。

PostReturn pr = new PostReturn();
if (Utils.nullOrBlank(carcode)) {
    // 无法查找
    pr.setErrorMsg("No carcode to lookup");
    return pr;
}
// 首先修剪它/删除空格
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) {
        // 无效的 carId
        pr.setErrorMsg(carId + " was invalid - could not be parsed");
        return pr;
    }
    // 查找套件
    CarHandler cph = CarHandler.findByKitcarcode(kb);
    // 标记为等待
    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);
// 确认消息
pr.setSuccessMsg(strings.toString());
return pr;

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

ABC123, DEF456, and GHI789 被标记为在等待 carprocessing 的原因下
ABC123 和 DEF456 被标记为在等待 carprocessing 的原因下
ABC123 被标记为在等待 carprocessing 的原因下
英文:

It appears that the reason the message being printed only contains the last carcode is because the for-loop it's within, will simply over-write the previous pr.setSuccessMsg call during each iteration.

Since pr and holdReason are not changed within the for-loop, you can place it after the for-loop.

Additionally, from within the for-loop, you can populate a String value with the kb values.

We'll utilize the for-loop to increment a count value.
This will be used to determine if our strings value is being populated with 1, 2, or more kb values—which will determine if our strings value should contain commas or an "and".

We can further use the count value, after the for-loop is exited, to determine if we should be using "is", or "and".

The strings value is then used as the message sent to the setSuccessMsg method.

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;

As an example, printing the strings value would yield a similar output.

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: 

huangapple
  • 本文由 发表于 2023年6月1日 04:44:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377143.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定