Thursday, April 3, 2014

Java code for maintaining a student records database using File Handling

/*
Program for maintaining a student records database using File Handling.
*/
import java.io.*;
class studentRecords
{
static BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
public void addRecords() throws IOException
{
// Create or Modify a file for Database
PrintWriter pw = new PrintWriter(new BufferedWriter(new
FileWriter("studentRecords.txt",true)));
String name, Class, fname, mname, address, dob;
int age;
long telephoneNo;
String s;
boolean addMore = false;
// Read Data
do
{
System.out.print("
Enter name
name = br.readLine();
System.out.print("Fathers Name
:
fname = br.readLine();
System.out.print("Mothers Name
:
mname = br.readLine();
System.out.print("Address
:
address = br.readLine();
System.out.print("Class
:
Class = br.readLine();
System.out.print("Date of Birth (dd/mm/yy) :
dob = br.readLine();
System.out.print("Age
:
age = Integer.parseInt(br.readLine());
System.out.print("Telephone No.
:
telephoneNo = Long.parseLong(br.readLine());
// Print to File
pw.println(name);
pw.println(fname);
pw.println(mname);
pw.println(address);
pw.println(Class);
pw.println(dob);
pw.println(age);
pw.println(telephoneNo);
System.out.print("
Records added successfully !

Do
you want to add more records ? (y/n) : ");
s = br.readLine();
if(s.equalsIgnoreCase("y"))
{
addMore = true;
System.out.println();
}
else
addMore = false;
}
while(addMore);
pw.close();
showMenu();
}
public void readRecords() throws IOException
{
try
{
// Open the file
BufferedReader file = new BufferedReader(new
FileReader("studentRecords.txt"));
String name;
int i=1;
// Read records from the file
while((name = file.readLine()) != null)
{
System.out.println("S.No. : " +(i++));
System.out.println("-------------");
System.out.println("
Name
: " +name);
System.out.println("Fathers Name : "
+file.readLine());
System.out.println("Mothers Name : "
+file.readLine());
System.out.println("Address
: "
+file.readLine());
System.out.println("Class
: "
+file.readLine());
System.out.println("Date of Birth : "
+file.readLine());
System.out.println("Age
: "
+Integer.parseInt(file.readLine()));
System.out.println("Tel. No.
: "
+Long.parseLong(file.readLine()));
System.out.println();
}
file.close();
showMenu();
}
catch(FileNotFoundException e)
{
System.out.println("
ERROR : File not Found !!!");
}
}
public void clear() throws IOException
{
// Create a blank file
PrintWriter pw = new PrintWriter(new BufferedWriter(new
FileWriter("studentRecords.txt")));
pw.close();
System.out.println("
All Records cleared successfully !");
for(int i=0;i<999999999;i++); // Wait for some time
showMenu();
}
public void showMenu() throws IOException
{
System.out.print("1 : Add Records
2 : Display Records
3 :
Clear All Records
4 : Exit

Your Choice : ");
int choice = Integer.parseInt(br.readLine());
switch(choice)
{
case 1:
addRecords();
break;
case 2:
readRecords();
break;
case 3:
clear();
break;
case 4:
System.exit(1);
break;
default:
System.out.println("
Invalid Choice !");
break;
}
}
public static void main(String args[]) throws IOException
{
studentRecords call = new studentRecords();
call.showMenu();
}
}


/**
* Algorithm for Adding Records :
* ----------------------------
* 1. Start
* 2. Open the database file.
* 3. Read data from the user.
* 4. Print the data to file.
* 5. Close the file.
* 6. End
*
* Algorithm for Displaying Records :
* --------------------------------
* 1. Start
* 2. Open the database file.
* 3. Read data from the filr.
* 4. Print the data on screen.
* 5. Close the file.
* 6. End
*
* Algorithm for Clearing All Records :
* ----------------------------------
* 1. Start
* 2. Overwrite the database file with a blank file.
* 5. Close the file.
* 6. End
*/


/*
OUTPUT :
------

1:Add Records
2:Display Records
3:Clear All Records
4:Exit
Your Choice : 1
Enter name : Mayank K. Rastogi
Fathers Name : Surendra Kumar Rastogi
Mothers Name : Sushma Rastogi
Address : Vipul Khand, Gomti Nagar, Lucknow.
Class : 12 - C
Date of Birth (dd/mm/yy) : 01/10/91
Age : 18
Telephone No. : 9546354565
Records added successfully !
Do you want to add more records ? (y/n) : y
....
...
...
.
*/


Related Posts by Categories

0 comments:

Post a Comment