You are on page 1of 25

INSTITUTE OF ENGINEERING

& MANAGEMENT

Department of Computer Science & Engineering

Name : Adrineel Saha ; Ayush Kumar Jha


Class Roll : 191 ; 197
Enrollment No. : 12019002003114 ;
12019002007020
Subject Name : OOP Lab Mini Project
Date : 08/12/2021
Students’ Information System
Suppose that you have to create a students’ information system for the
enrolled students in the
order of their first names. But the problem is that they may take admission in a
random fashion.
So, you have to maintain these records using a linked list where each node
represents a student.
Each student node is an object containing his/her name, roll number (unique),
marks and grade.
The students' roll numbers are generated in the order of their admission.
There are three operations –
A. To add a student's records (roll number, name, marks, grade)
B. To delete a student's record
C. To search a student's record based on their first name, last name and roll
number
D. To modify a student's record (e.g., the marks of a particular student need to
be changed, the
name has been misspelt, etc.)
The linked list records are to be stored in a file. Also, the operations which are
performed on the
register, are to be saved in a log file. Each log file entry consists of two fields -
A. Operation name
B. Timestamp of the operation
The deleted records are to be stored in a stack such that the delete operations
can be undone.
You have to design the information system using Java Swing. You should use
different
components of Java Swing to enhance the look and feel of the designed
system. There are no
restrictions to design your system layout.
Source Code:-
package stud_info_system;

import java.util.*;
import java.util.logging.*;
import java.awt.event.*;
import javax.swing.*;

import java.awt.FlowLayout;
import java.io.*;
class Record implements Serializable{
String F_name;
String L_name;
int Rollno;
int Marks;
String Grade;
Record(String F_name,String L_name,int Rollno,int Marks,String
Grade){
this.F_name=F_name;
this.L_name=L_name;
this.Rollno=Rollno;
this.Marks=Marks;
this.Grade=Grade;
}
public String toString(){
return F_name+" : "+L_name+" : "+Rollno+" : "+Marks+" :
"+Grade;
}
}
public class Stud_Info_System implements ActionListener{
private static Logger logger=Logger.getLogger("Log");
String s0,s1,s2,s3,s4;
Stud_Info_System()
{
s0=s1=s2=s3=s4="";

}
public static void main(String[]args) throws Exception{
Stud_Info_System SIS =new Stud_Info_System();
// Create and set up a frame window
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("OOPS lab project:PCCCS593");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Define new buttons


JButton jb1 = new JButton("Insert record");
JButton jb2 = new JButton("Display record");
JButton jb3 = new JButton("Search record");
JButton jb4 = new JButton("Update record");
JButton jb5 = new JButton("Delete record");
JButton jb6 = new JButton("Restore record");

JTextField t=new JTextField();


// Define the panel to hold the buttons
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(jb1);
panel.add(jb2);
panel.add(jb3);
panel.add(jb4);
panel.add(jb5);
panel.add(jb6);

panel.add(t);
// Set the window to be visible as the default to be false
frame.add(panel);
frame.pack();
frame.setVisible(true);
jb1.addActionListener(SIS);
jb2.addActionListener(SIS);
jb3.addActionListener(SIS);
jb4.addActionListener(SIS);
jb5.addActionListener(SIS);
jb6.addActionListener(SIS);
frame.setLayout(new FlowLayout(FlowLayout.LEFT, 18, 28));
frame.setSize(400,400);
}

public void actionPerformed(ActionEvent E)


{
ArrayList<Record> ll=new ArrayList<Record>();
ArrayList<Record> rl=new ArrayList<Record>();
File file=new File("StuData.txt");
File newfile=new File("Recyclebin.txt");
ObjectOutputStream oos=null;
ObjectOutputStream rcs=null;
ObjectInputStream ois=null;
ObjectInputStream ris=null;

try{
if(file.isFile()){
ois=new ObjectInputStream(new FileInputStream(file));
ll=(ArrayList<Record>)ois.readObject();
ois.close();}}
catch(FileNotFoundException F){
F.printStackTrace();
}
catch(IOException IO){
IO.printStackTrace();
}
catch(ClassNotFoundException C){
C.printStackTrace();
}
if(E.getActionCommand().equals("Insert record")){
Scanner s1,s2;
s1=new Scanner(System.in);
s2=new Scanner(System.in);
System.out.println("How many records you want to add?:");
int n=s1.nextInt();
int i;
for(i=0;i<n;i++){
System.out.println("Enter First name");
String fn=s2.nextLine();
System.out.println("Enter Last name");
String ln=s2.nextLine();
System.out.println("Enter roll no:");
int roll=s1.nextInt();
System.out.println("Enter marks :");
int marks=s1.nextInt();
System.out.println("Enter grade :");
String grade=s2.nextLine();
ll.add(new Record(fn,ln,roll,marks,grade));

try{
oos=new ObjectOutputStream(new
FileOutputStream(file));
oos.writeObject(ll);
oos.close();
}
catch(IOException I){
I.printStackTrace();
}
System.out.println("");
System.out.println("Log file content:");
FileHandler fh;
try{
fh=new FileHandler("Mylogfile.log");
logger.addHandler(fh);
SimpleFormatter formatter=new SimpleFormatter();
logger.info("Insert operation");
}catch(Exception e){
logger.log(Level.WARNING,"Exception ::",e);
}

}
else if(E.getActionCommand().equals("Display record")){

ListIterator li=null;
li=ll.listIterator();
while(li.hasNext())
System.out.println(li.next());
System.out.println("...................");
FileHandler fh;
try{
fh=new FileHandler("Mylogfile.log");
logger.addHandler(fh);
SimpleFormatter formatter=new SimpleFormatter();
logger.info("Display operation");
}catch(Exception e){
logger.log(Level.WARNING,"Exception ::",e);
}

}
else if(E.getActionCommand().equals("Search record")){
Scanner s1,s2;
s1=new Scanner(System.in);
s2=new Scanner(System.in);

try{
if(file.isFile()){
ois=new ObjectInputStream(new FileInputStream(file));
ll=(ArrayList<Record>)ois.readObject();
ois.close();}}
catch(FileNotFoundException fe){
fe.printStackTrace();
}
catch(IOException ie){
ie.printStackTrace();
}
catch(ClassNotFoundException ce){
ce.printStackTrace();
}
System.out.println("Enter the roll no, First name and Last name
to search the record");
int roll=s1.nextInt();
String fs=s2.nextLine();
String ls=s2.nextLine();
int count=0;
ListIterator li=null;
li=ll.listIterator();
while(li.hasNext()){
Record R=(Record)li.next();
if(R.Rollno==roll){

count=count+1;
System.out.println(R);

}
else{
count=count;
}
}

if(count!=0){
System.out.println("Yes.The record is found");
}else{System.out.println("No.The record is not found.");}
FileHandler fh;
try{
fh=new FileHandler("Mylogfile.log");
logger.addHandler(fh);
SimpleFormatter formatter=new SimpleFormatter();
logger.info("Searching operation");
}catch(Exception e){
logger.log(Level.WARNING,"Exception ::",e);
}

}
else if(E.getActionCommand().equals("Update record")){
Scanner s1,s2;
s1=new Scanner(System.in);
s2=new Scanner(System.in);

try{
if(file.isFile()){
ois=new ObjectInputStream(new FileInputStream(file));
ll=(ArrayList<Record>)ois.readObject();
ois.close();}}
catch(FileNotFoundException FE){
FE.printStackTrace();
}
catch(IOException O){
O.printStackTrace();
}
catch(ClassNotFoundException CE){
CE.printStackTrace();
}
System.out.println("Enter the roll no to update the datils for ");
int roll=s1.nextInt();
int count=0;
ListIterator li=null;
li=ll.listIterator();
while(li.hasNext()){
Record R=(Record)li.next();
if(R.Rollno==roll){

count=count+1;
System.out.println(R);
int ch;

System.out.println("1.Update first name");


System.out.println("2.Update last name");
System.out.println("3.Update marks");
System.out.println("4.Update grade");
System.out.println("Please select what field you
want to update");
ch=s1.nextInt();
switch(ch){
case 1:
System.out.println("Your previous first
name was:"+R.F_name);
String New_First;
System.out.println("Please give the
new first name of your choice");
New_First=s2.nextLine();
R.F_name=New_First;
System.out.println("Now your new
first name is:"+R.F_name);
break;
case 2:
System.out.println("Your previous last
name was:"+R.L_name);
String New_Last;
System.out.println("Please give the
new last name of your choice");
New_Last=s2.nextLine();
R.L_name=New_Last;
System.out.println("Now your new last
name is:"+R.L_name);
break;
case 3:
System.out.println("Your previous marks
was:"+R.Marks);
int newmarks;
System.out.println("Input new
marks");
newmarks=s1.nextInt();
R.Marks=newmarks;
System.out.println("Your marks is
updated and now it is:"+R.Marks);
break;
case 4:
System.out.println("Previous grade of the
student was:"+R.Grade);
String newgrade;
System.out.println("Input new grade
");
newgrade=s2.nextLine();
R.Grade=newgrade;
System.out.println("Now your updated
grade is:"+R.Grade);
break;
default:
System.out.println("Please choose a valid
option");
break;
}

}
else{
count=count;
}
}

if(count!=0){

try{
oos=new ObjectOutputStream(new
FileOutputStream(file));
oos.writeObject(ll);
oos.close();
}
catch(IOException pe){
pe.printStackTrace();
}
System.out.println("The record is updated successfully");
}else{System.out.println("No.The record is not found.");}
FileHandler fh;
try{
fh=new FileHandler("Mylogfile.log");
logger.addHandler(fh);
SimpleFormatter formatter=new SimpleFormatter();
logger.info("Update operation");
}catch(Exception e){
logger.log(Level.WARNING,"Exception ::",e);
}

}
else if(E.getActionCommand().equals("Delete record")){
Scanner s1,s2;
s1=new Scanner(System.in);
s2=new Scanner(System.in);

try{
if(file.isFile()){
ois=new ObjectInputStream(new FileInputStream(file));
ll=(ArrayList<Record>)ois.readObject();
ois.close();
}}
catch(FileNotFoundException fe){
fe.printStackTrace();
}
catch(IOException ie){
ie.printStackTrace();
}
catch(ClassNotFoundException ce){
ce.printStackTrace();
}
try{
if(newfile.isFile()){
ris=new ObjectInputStream(new
FileInputStream(newfile));
rl=(ArrayList<Record>)ris.readObject();
ris.close();
}}
catch(FileNotFoundException XE){
XE.printStackTrace();
}
catch(IOException Y){
Y.printStackTrace();
}
catch(ClassNotFoundException Z){
Z.printStackTrace();
}
System.out.println("Enter the roll no to delete the record");
int roll=s1.nextInt();

int count=0;
ListIterator li=null;
li=ll.listIterator();
while(li.hasNext()){
Record R=(Record)li.next();
if(R.Rollno==roll){
System.out.println("The following record will be
removed:"+R);
count=count+1;
li.remove();
rl.add(new
Record(R.F_name,R.L_name,R.Rollno,R.Marks,R.Grade));

}
else{
count=count;
}
}

if(count!=0){
try{
oos=new ObjectOutputStream(new
FileOutputStream(file));
oos.writeObject(ll);
oos.close();
rcs=new ObjectOutputStream(new
FileOutputStream(newfile));
rcs.writeObject(rl);
rcs.close();
}
catch(IOException PE){
PE.printStackTrace();
}
System.out.println("The record is deleted successfully and the
databse is updated");

}else{System.out.println("No.The record is not found.");}


FileHandler fh;
try{
fh=new FileHandler("Mylogfile.log");
logger.addHandler(fh);
SimpleFormatter formatter=new SimpleFormatter();
logger.info("Deletion operation");
}catch(Exception e){
logger.log(Level.WARNING,"Exception ::",e);
}
}
else if(E.getActionCommand().equals("Restore record")){
Scanner s1,s2;
s1=new Scanner(System.in);
s2=new Scanner(System.in);
try{
if(file.isFile()){
ois=new ObjectInputStream(new FileInputStream(file));
ll=(ArrayList<Record>)ois.readObject();
ois.close();
}}
catch(FileNotFoundException fe){
fe.printStackTrace();
}
catch(IOException ie){
ie.printStackTrace();
}
catch(ClassNotFoundException ce){
ce.printStackTrace();
}
try{
if(newfile.isFile()){
ris=new ObjectInputStream(new
FileInputStream(newfile));
rl=(ArrayList<Record>)ris.readObject();
ris.close();
}}
catch(FileNotFoundException xe){
xe.printStackTrace();
}
catch(IOException ye){
ye.printStackTrace();
}
catch(ClassNotFoundException ze){
ze.printStackTrace();
}
ListIterator li=null;
li=rl.listIterator();
System.out.println("Enter the roll no for which you want to
restore the data");
int roll=s1.nextInt();
int count=0;
while(li.hasNext()){
Record R=(Record)li.next();
if(R.Rollno==roll){
System.out.println("The following record will be
restored:"+R);

count=count+1;
li.remove();
ll.add(new
Record(R.F_name,R.L_name,R.Rollno,R.Marks,R.Grade));

}
else{
count=count;
}
}

if(count!=0){
try{
oos=new ObjectOutputStream(new
FileOutputStream(file));
oos.writeObject(ll);
oos.close();
rcs=new ObjectOutputStream(new
FileOutputStream(newfile));
rcs.writeObject(rl);
rcs.close();
}
catch(IOException pe){
pe.printStackTrace();
}
System.out.println("The record is restored successfully and the
databse is updated");

}else{System.out.println("No.The record is not found.");}

FileHandler fh;
try{
fh=new FileHandler("Mylogfile.log");
logger.addHandler(fh);
SimpleFormatter formatter=new SimpleFormatter();
logger.info("Restore operation");
}catch(Exception e){
logger.log(Level.WARNING,"Exception ::",e);
}
}
else{
System.out.print("Nothing");}
}
}

Output:-

You might also like