我有一个场景,我必须检查模板是否已存在于数据库中。如果存在,我必须根据现有模板返回一条消息。
我有几个模板:电子邮件、信件、短信。如果它们全部存在,我必须返回“所有模板都已存在。”
如果仅存在电子邮件模板,我必须返回仅存在电子邮件模板,信函和短信模板也是如此。
代码:
for (EventVO eventVO: eventModuleList) {
List <EmailTemplateMaster> emailTemplateList = communicationDAO
.checkEmailTemplateExist(eventVO.getEventCode());
if (CollectionUtils.isNotEmpty(emailTemplateList)) {
emailTemplateExist = true;
}
List <LetterTemplateMaster> letterTemplateList = communicationDAO
.checkLetterTemplateExist(eventVO.getEventCode());
if (CollectionUtils.isNotEmpty(letterTemplateList)) {
letterTemplateExist = true;
}
List <SmsTemplateMaster> smsTemplateList = communicationDAO
.checkSmsTemplateExist(eventVO.getEventCode());
if (CollectionUtils.isNotEmpty(smsTemplateList)) {
smsTemplateExist = true;
}
if (emailTemplateExist && letterTemplateExist && smsTemplateExist) {
templateExist = CommunicationConstants.ALL_TEMPLATE_EXIST;
}
if (emailTemplateExist || !letterTemplateExist && !smsTemplateExist) {
templateExist = CommunicationConstants.EMAIL_TEMPLATE_EXIST;
}
if (!emailTemplateExist && letterTemplateExist && !smsTemplateExist) {
templateExist = CommunicationConstants.LETTER_TEMPLATE_EXIST;
}
if (!emailTemplateExist && !letterTemplateExist && smsTemplateExist) {
templateExist = CommunicationConstants.SMS_TEMPLATE_EXIST;
}
}
我能知道检查退出模板的 boolean 值是否存在的最简单方法吗?
基于退出,我必须发送相应的消息。
public static final String ALL_TEMPLATE_EXIST = "Email, Letter and Sms Template already exist for the selected event.";
public static final String EMAIL_TEMPLATE_EXIST = "Email Template already exist for the selected event.";
public static final String SMS_TEMPLATE_EXIST = "Sms Template already exist for the selected event.";
public static final String LETTER_TEMPLATE_EXIST = "Email Letter Template already exist for the selected event.";
public static final String EMAIL_SMS_TEMPLATE_EXIST = "Email and Sms Template already exist for the selected event.";
public static final String EMAIL_LETTER_TEMPLATE_EXIST = "Email and Letter Template already exist for the selected event.";
public static final String SMS_LETTER_TEMPLATE_EXIST = "Sms and Letter Template already exist for the selected event.";
最佳答案
您可以在 CommunicationConstants
类中“准备” boolean 值和消息之间的映射:
public class CommunicationConstants
{
private static final Map<List<Boolean>, String> CONSTANTS = new HashMap<>();
static
{
CONSTANTS.put(Arrays.asList(true, true, true), "Email, Letter and Sms Template already exist for the selected event.");
CONSTANTS.put(Arrays.asList(true, true, false), "Email and Sms Template already exist for the selected event.");
CONSTANTS.put(Arrays.asList(true, false, true), "Email and Letter Template already exist for the selected event.");
CONSTANTS.put(Arrays.asList(false, true, true), "Sms and Letter Template already exist for the selected event.");
CONSTANTS.put(Arrays.asList(true, false, false), "Email Template already exist for the selected event.");
CONSTANTS.put(Arrays.asList(false, true, false), "Sms Template already exist for the selected event.");
CONSTANTS.put(Arrays.asList(false, false, true), "Letter Template already exist for the selected event.");
}
public static String getMessage(boolean emailExists, boolean smsExists, boolean letterExists)
{
return CONSTANTS.get(Arrays.asList(emailExists, smsExists, letterExists));
}
}
检索它的代码将是一行代码:
System.out.println(CommunicationConstants.getMessage(true, false, true));
关于java - 检查java中的多个 boolean 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51943978/