Java program to insert records using arraylist.
import java.util.*;
class employee
{
emp e1=new emp();
static int limit;
static Scanner s1=new Scanner(System.in);
ArrayList<emp> my_list=new ArrayList<emp>();
public static void main(String[] args)
{
System.out.println("WELCOME");
System.out.print("Enter the limit=");
limit=s1.nextInt();
employee em=new employee();
for(int i=0;i<limit;i++)
{
em.input();
}
em.output();
em.menu();
}
public int menu()
{
System.out.println("\n Menu \n 1-insert \n 2-Delete \n 3-Display\n4-Exit");
System.out.print("Enter your option[1-4]=");
int op=s1.nextInt();
return op;
}
public void input()
{
System.out.println("Enter Empname=");
e1.empname=s1.next();
System.out.println("Enter Empid=");
e1.empid=s1.nextInt();
System.out.println("Enter Empsalary=");
e1.empsalary=s1.nextFloat();
my_list.add(e1);
}
public void output()
{
System.out.println("Empid\tEmpname\tEmpsalary");
emp temp=new emp();
for(int j=1;j<limit;j++)
{
temp=my_list.get(j);
System.out.println(temp.empid+"\t"+temp.empname+"\t"+temp.empsalary);
}
}
}
class emp
{
public int empid;
public String empname;
public float empsalary;
}//this program does not print different records as entered according to the limit.it displays only the value last entered. example- if i enter 1st record- riti 1 2900
enter 2nd record-abc 2 1200
then it displays-
abc 2 1200
abc 2 1200
kindly help me.