I want to validate below condition but something is going wrong with my if condition and returning invalid results. My validations are:
Either productId or productAltID can have value or both can be null
If both productId and productAltID are null, then productSellDate and productReturnDate must have value.
If productSellDate and productReturnDate are null, then productId or productAltID should have value.
Please find my code below, I'm getting incorrect result not sure what I'm messing up here:
public class validate {
public static void main(String[] args) {
String productId = null;
String productAltID = "adfafadsf";
Date productSellDate = null;
Date productReturnDate = new Date();
if (productId != null || productAltID != null || productSellDate !=null && productReturnDate != null) {
System.out.println("validation success");
} else {
System.out.println("validation failed");
}
}
}
// Valid Scenarios
Combination 1: Valid Scenario
String productId = null;
String productAltID = null;
Date productSellDate = new Date();
Date productReturnDate = new Date();
Combination 2: Valid Scenario
String productId = null;
String productAltID = "3432fefsf";
Date productSellDate = new Date();
Date productReturnDate = new Date();
Combination 3: Valid Scenario
String productId = "sdf3234234324";
String productAltID = "3432fefsf";
Date productSellDate = null;
Date productReturnDate = null;
Combination 4: Valid Scenario
String productId = null;
String productAltID = "3432fefsf";
Date productSellDate = null;
Date productReturnDate = null;