In: Computer Science
boolean isEligibile (int, char, char) using Drjava
isEligibile will return true when the customer is eligible to book
a vacation package, and false otherwise. It takes as input 3 pieces
of information, first an int representing the customer’s age.
Second, a char representing the country where the rental is being
made (‘c’ for Canada, ‘u’ for United States, ‘f’ for France). The
third input is a char representing if the customer has a valid
Canadian passport ‘y’ for Yes, ‘n’ for No.
A customer is eligible to book a package under the following
conditions:
If the customer is in Canada, they must be over 21.
If the customer is in the United States, they must be over
18.
If the customer is in France, they must be over 25.
If the customer does not have a valid passport, they are not
eligible to book a package in France or the United
States.
The code for the function "isEligible()" is as follows:
public static boolean isEligible(int age, char country, char passport) {
if( ('n' == passport && 'f' == country) || ('n' == passport && 'u' == country) ) {
return false;
}
else if( (country == 'c' && age > 21) || (country == 'u' && age > 18) || (country == 'f' && age > 25) ) {
return true;
} else {
return false;
}
}
In order to test the functionality of the above function, we need to have the Tester Class. So the code for the Tester class which contain the main class is:
class EligibleTest {
public static boolean isEligible(int age, char country, char passport) {
if( ('n' == passport && 'f' == country) || ('n' == passport && 'u' == country) ) {
return false;
}
else if( (country == 'c' && age > 21) || (country == 'u' && age > 18) || (country == 'f' && age > 25) ) {
return true;
} else {
return false;
}
}
public static void main(String args[]) {
// Testing Code
// when the customers have the passport with him/her
//If the customer is in Canada, they must be over 21.
System.out.println(isEligible(22, 'c', 'y'));
//If the customer is in the United States, they must be over 18.
System.out.println(isEligible(19, 'u', 'y'));
//If the customer is in France, they must be over 25.
System.out.println(isEligible(26, 'f', 'y'));
// when the customers does not have the passport with him/her
//If the customer does not have a valid passport, they are not eligible to book a package in France
System.out.println(isEligible(22, 'u', 'n'));
//If the customer does not have a valid passport, they are not eligible to book a package in the United States.
System.out.println(isEligible(27, 'f', 'n'));
// If the customer does not have a valid passport, but he is applicable age wise than the function can return true
System.out.println(isEligible(22, 'c', 'n'));
}
}
The output is:
If you have any doubts let me know in the comments section. Thank you