Write a program using java to add, delete, update and
search a student database using linked list.
Source Code:
import java.util.*;
import java.io.*;
class Llist
{
LinkedList ll=new LinkedList();
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
void enter(int j)throws IOException,IndexOutOfBoundsException
{
String s,s1,s2,s3,s4;
int k=j;
LinkedList ll1=new LinkedList();
System.out.print("Enter name of the Student:");
s=br1.readLine();
ll1.add("Name: "+s);
System.out.print("Enter class:");
s1=br1.readLine();
ll1.add("Class: "+s1);
System.out.print("Enter roll no:");
s2=br1.readLine();
ll1.add("Roll No: "+s2);
System.out.print("Enter marks:");
s3=br1.readLine();
ll1.add("Marks: "+s3);
System.out.print("Enter date of birth:");
s4=br1.readLine();
ll1.add("D.O.B: "+s4);
ll.add(k,ll1);
}
void display()throws IOException
{
String str1="";
int x1;
System.out.println("\nStudent Details:\n");
for(int i=0;i<ll.size();i++)
{
x1=i+1;
System.out.println(x1+") "+ll.get(i));
}
}
void delete()throws IOException
{
String str1;
String str2;
int i,j=0;
LinkedList ll2=new LinkedList();
System.out.print("Enter name of the student to delete his/her record:");
str1=br1.readLine();
str1="Name: "+str1;
for(i=0;i<ll.size();i++)
{
ll2=(LinkedList)ll.get(i);
str2=(String)ll2.getFirst();
if(str1.equals(str2))
{
ll.remove(i);
j=1;
break;
}
}
if(j==0)
{
System.out.println("\nName Not found in the list");
}
}
void update()throws IOException,ClassCastException
{
String str1;
String str2;
String g1,g2;
int i,j=0,m1;
LinkedList ll2=new LinkedList();
System.out.print("Enter name of the student to update his/her record:");
str1=br1.readLine();
str1="Name: "+str1;
for(i=0;i<ll.size();i++)
{
ll2=(LinkedList)ll.get(i);
str2=(String)ll2.getFirst();
if(str1.equals(str2))
{
System.out.println("Enter 1 to update Class");
System.out.println("Enter 2 to update Roll No.");
System.out.println("Enter 3 to update Marks");
System.out.println("Enter 4 to update Date of Birth");
System.out.print("Enter your option:");
m1=Integer.parseInt(br1.readLine());
if(m1==1)
{
System.out.print("Enter new Class of the student:");
g1=br1.readLine();
g1="Class: "+g1;
g2=(String)ll2.set(m1,g1);
}
if(m1==2)
{
System.out.print("Enter new Roll No. of the student:");
g1=br1.readLine();
g1="Roll No: "+g1;
g2=(String)ll2.set(m1,g1);
}
if(m1==3)
{
System.out.print("Enter new Marks of the student:");
g1=br1.readLine();
g1="Marks: "+g1;
g2=(String)ll2.set(m1,g1);
}
if(m1==4)
{
System.out.print("Enter new Date of Birth of the student:");
g1=br1.readLine();
g1="D.O.B: "+g1;
g2=(String)ll2.set(m1,g1);
}
j=1;
break;
}
}
if(j==0)
{
System.out.println("\nName Not found in the list");
}
}
void search()throws IOException
{
String str1;
String str2;
int i,j=0;
LinkedList ll2=new LinkedList();
System.out.print("Enter name of the student to show his/her record:");
str1=br1.readLine();
str1="Name: "+str1;
for(i=0;i<ll.size();i++)
{
ll2=(LinkedList)ll.get(i);
str2=(String)ll2.getFirst();
System.out.println("First:"+str1);
System.out.println("Second:"+str2);
if(str1.equals(str2))
{
System.out.println("\nFound in the list at position "+(i+1)+"\n");
System.out.println(ll2);
j=1;
break;
}
}
if(j==0)
{
System.out.println("\nNot found in the list");
}
}
}
************MAIN FUNCTION*************
class main
{
public static void main(String ars[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int ch,i=0;
Llist ob=new Llist();
System.out.println("\n.........Student Database.........\n");
while(true)
{
System.out.println("\n1.Add a student's detail");
System.out.println("2.Delete a student's detail");
System.out.println("3.Update a student's detail");
System.out.println("4.Search a student's detail");
System.out.println("5.Display Student details");
System.out.println("6.Exit");
System.out.print("Enter your choice:");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
ob.enter(i);
i=i+1;
break;
case 2:
ob.delete();
break;
case 3:
ob.update();
break;
case 4:
ob.search();
break;
case 5:
ob.display();
break;
case 6:
System.exit(0);
}
}
}
}
OUTPUT:
D:\java>javac assignment11.java
Note: assignment11.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
D:\java>java main
.........Student Database.........
1.Add a student's detail
2.Delete a student's detail
3.Update a student's detail
4.Search a student's detail
5.Display Student details
6.Exit
Enter your choice:1
Enter name of the Student:Suman
Enter class:B,Sc
Enter roll no:48
Enter marks:94
Enter date of birth:9.9.94
1.Add a student's detail
2.Delete a student's detail
3.Update a student's detail
4.Search a student's detail
5.Display Student details
6.Exit
Enter your choice:1
Enter name of the Student:Pratick
Enter class:B.Sc
Enter roll no:52
Enter marks:98
Enter date of birth:7.8.94
1.Add a student's detail
2.Delete a student's detail
3.Update a student's detail
4.Search a student's detail
5.Display Student details
6.Exit
Enter your choice:1
Enter name of the Student:Mrinmoy
Enter class:M.Sc
Enter roll no:55
Enter marks:88
Enter date of birth:4.12.92
1.Add a student's detail
2.Delete a student's detail
3.Update a student's detail
4.Search a student's detail
5.Display Student details
6.Exit
Enter your choice:1
Enter name of the Student:Sumit
Enter class:XII
Enter roll no:33
Enter marks:78
Enter date of birth:7.8.95
1.Add a student's detail
2.Delete a student's detail
3.Update a student's detail
4.Search a student's detail
5.Display Student details
6.Exit
Enter your choice:5
Student Details:
1) [Name: Suman, Class: B,Sc, Roll No: 48, Marks: 94, D.O.B: 9.9.94]
2) [Name: Pratick, Class: B.Sc, Roll No: 52, Marks: 98, D.O.B: 7.8.94]
3) [Name: Mrinmoy, Class: M.Sc, Roll No: 55, Marks: 88, D.O.B: 4.12.92]
4) [Name: Sumit, Class: XII, Roll No: 33, Marks: 78, D.O.B: 7.8.95]
1.Add a student's detail
2.Delete a student's detail
3.Update a student's detail
4.Search a student's detail
5.Display Student details
6.Exit
Enter your choice:4
Enter name of the student to show his/her record:Mrinmoy
First:Name: Mrinmoy
Second:Name: Suman
First:Name: Mrinmoy
Second:Name: Pratick
First:Name: Mrinmoy
Second:Name: Mrinmoy
Found in the list at position 3
[Name: Mrinmoy, Class: M.Sc, Roll No: 55, Marks: 88, D.O.B: 4.12.92]
1.Add a student's detail
2.Delete a student's detail
3.Update a student's detail
4.Search a student's detail
5.Display Student details
6.Exit
Enter your choice:3
Enter name of the student to update his/her record:Sumit
Enter 1 to update Class
Enter 2 to update Roll No.
Enter 3 to update Marks
Enter 4 to update Date of Birth
Enter your option:3
Enter new Marks of the student:82
1.Add a student's detail
2.Delete a student's detail
3.Update a student's detail
4.Search a student's detail
5.Display Student details
6.Exit
Enter your choice:5
Student Details:
1) [Name: Suman, Class: B,Sc, Roll No: 48, Marks: 94, D.O.B: 9.9.94]
2) [Name: Pratick, Class: B.Sc, Roll No: 52, Marks: 98, D.O.B: 7.8.94]
3) [Name: Mrinmoy, Class: M.Sc, Roll No: 55, Marks: 88, D.O.B: 4.12.92]
4) [Name: Sumit, Class: XII, Roll No: 33, Marks: 82, D.O.B: 7.8.95]
1.Add a student's detail
2.Delete a student's detail
3.Update a student's detail
4.Search a student's detail
5.Display Student details
6.Exit
Enter your choice:2
Enter name of the student to delete his/her record:Mrinmoy
1.Add a student's detail
2.Delete a student's detail
3.Update a student's detail
4.Search a student's detail
5.Display Student details
6.Exit
Enter your choice:5
Student Details:
1) [Name: Suman, Class: B,Sc, Roll No: 48, Marks: 94, D.O.B: 9.9.94]
2) [Name: Pratick, Class: B.Sc, Roll No: 52, Marks: 98, D.O.B: 7.8.94]
3) [Name: Sumit, Class: XII, Roll No: 33, Marks: 82, D.O.B: 7.8.95]
1.Add a student's detail
2.Delete a student's detail
3.Update a student's detail
4.Search a student's detail
5.Display Student details
6.Exit
Enter your choice:6
0 comments