Class and Object Programs

Java Program to store he policy record and search , insert etc.


import java.util.Scanner;

class PolicyInsurance

{

int policyno;

String name;

int age;

int policyDuration;

String interval;

int amount;

public static boolean login(String usr, String pwd)

{

String username[]={"u1","u2","u3"};

String password[]={"pswd1","pswd2","pswd3"};

boolean s=false;

for(int x=0;x<username.length;x++)

{

if(usr.equals(username[x])==true && pwd.equals(password[x])==true)

{

s=true;

break;

}

}

return s;

}

public static void main()

{

String userid, pwd;

int op,n;

int i;

Scanner sc=new Scanner (System.in);

System.out.println("Enter user Id");

userid=sc.next();

System.out.println("Enter your password");

pwd=sc.next();

if(login(userid, pwd))

{

System.out.println("Enter the number of records");

n=sc.nextInt();

PolicyInsurance[]pi=new PolicyInsurance[n];// creating object for n policy insurance

for(i=0;i<n;i++)

{

pi[i]=new PolicyInsurance();// allocating memory space for object

}

do

{

System.out.println("Enter 1. for accepting the policy holder details");

System.out.println("Enter 2. to search for an input policy number");

System.out.println("Enter 3. to display the records as per the premium amount in descending order");

System.out.println("Enter 4. for displaying the half yearly policies");

System.out.println("Enter 5. for listing the policies that match the given duration");

System.out.println("Enter 6. to exit");

op=sc.nextInt();

switch(op)

{

case 1:

for( i=0;i<n;i++)

{

System.out.println("Enter the details of" + (i+1)+" employee");

System.out.println("Enter the policy number");

pi[i].policyno=sc.nextInt();

System.out.println("Enter the name");

pi[i].name=sc.next();

System.out.println("Enter the age");

pi[i].age=sc.nextInt();

System.out.println("Enter the policy duration");

pi[i].policyDuration=sc.nextInt();

System.out.println("Enter the premium interval");

pi[i].interval=sc.next();

System.out.println("Enter the premium amount");

pi[i].amount=sc.nextInt();

}

break;

case 2:

System.out.println("Enter the policy number you want to search");

int pc;

pc=sc.nextInt();

for(i=0;i<n;i++)

{

if(pi[i].policyno==pc)

{

System.out.print("\t" + pi[i].policyno);

System.out.print("\t" + pi[i].name);

System.out.print("\t" + pi[i].age);

System.out.print("\t" + pi[i].policyDuration);

System.out.print("\t" + pi[i].interval);

System.out.print("\t" + pi[i].amount+ "\n");

}


}

break;

case 3:

int tallest;

PolicyInsurance Temp;


for(int a = 0; a <pi.length - 1; a++)

{

tallest = a; // reset tallest to current index

// start inner loop at next index

for(int b = a + 1; b < pi.length; b++)// sorting in descending order

// check if the premuimAmount at this index is taller than the

// tallest found then check for bellow

if(pi[b].amount > pi[tallest].amount)

tallest = b;

Temp = pi[a];

pi[a] = pi[tallest];

pi[tallest] = Temp;

}

System.out.println("Record After Sorting");

for(i=0;i<n;i++)

{

System.out.print("\n\t" +pi[i].policyno);// \t is for tab

System.out.print("\t" + pi[i].name);// \n is for new line

System.out.print("\t" + pi[i].age);

System.out.print("\t" + pi[i].policyDuration);

System.out.print("\t" + pi[i].interval);

System.out.print("\t" + pi[i].amount);

}

System.out.println("");

break;

case 4:

System.out.println(" List of half yearly payment policy ");

String in="half";

for(i=0;i<n;i++)

{

if(pi[i].interval.equals(in))

{

System.out.print("\t" + pi[i].policyno);

System.out.print("\t" + pi[i].name);

System.out.print("\t" + pi[i].age);

System.out.print("\t" + pi[i].policyDuration);

System.out.print("\t" + pi[i].interval);

System.out.print("\t" + pi[i].amount+ "\n");

}


}

break;

case 5:

System.out.println("Enter the duration you want to display");

int d;

d=sc.nextInt();

for(i=0;i<n;i++)

{

if(pi[i].policyDuration==d)

{

System.out.print("\t" + pi[i].policyno);

System.out.print("\t" + pi[i].name);

System.out.print("\t" + pi[i].age);

System.out.print("\t" + pi[i].policyDuration);

System.out.print("\t" + pi[i].interval);

System.out.print("\t" + pi[i].amount+ "\n");

}


}

break;

}

}

while(op!=6);

System.out.println("Thank you for using our services");

}

else

{

System.out.println("Username and password entered is wrong");

}


}


}