Check Company Insures Drivers in Java


A company insures its drivers in the following cases:

  • If the driver is married
  • If the driver is unmarried, male & above 30 years of age
  • If the driver is unmarried, female & above 25 years of age

The program starts by importing the Scanner class to take input from the user. It then prompts the user to enter their age, gender, and marital status. The program reads in these values using nextInt() and next().charAt(0) methods of the Scanner class and stores them in separate variables.

After this, the program uses if-else statements to determine whether the driver should be insured or not. If the driver is married (status == 'M' || status == 'm'), they should be insured regardless of their age and gender. Otherwise, the program checks the age and gender of the driver.

If the driver is unmarried (status == 'U' && gen == 'M' && age > 30 || status == 'u' && gen == 'm' && age > 30), male (gen == 'M'), and over 30 years old (age > 30), they should be insured. Similarly, if the driver is unmarried (status == 'U' && gen == 'F' && age > 25 || status == 'u' && gen == 'f' && age > 25), female (gen == 'F'), and over 25 years old (age > 25), they should be insured.

If the driver does not meet any of these conditions, the program outputs that the driver should not be insured.

Source Code

import java.util.Scanner;
class Company_Insures
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter The Age :");
		int age = input.nextInt();
		System.out.print("Enter the Gender M/F :");
		char gen = input.next().charAt(0);
		System.out.print("Enter the Marital Status U/M :");
		char status = input.next().charAt(0);
		if(status == 'M' || status == 'm')
		{			
			System.out.println("Driver should be Insured.");
		}
		else if(status == 'U' && gen == 'M' && age > 30 || status == 'u' && gen == 'm' && age > 30)
		{			
			System.out.println("Driver should be Insured.");
		}
		else if(status == 'U' && gen == 'F' && age > 25 || status == 'u' && gen == 'f' && age > 25)
		{			
			System.out.println("Driver should be Insured.");
		}
		else
		{			
			System.out.println("Driver should not be Insured.");
		}
	}
}

Output

Enter The Age :24
Enter the Gender M/F :f
Enter the Marital Status U/M :u
Driver should not be Insured.

Example Programs