Record management application for a school


 

Record management application for a school

In this section, we are going to create an application to record the information of all the students.

In this section, we are going to create an application to record the information of all the students.

Record management application for a school

In this section, we are going to create an application to record the information of all the students. All the data has been stored in the text files.Through this application, you can add record of new students, edit the record of students and can delete the record of student. We have used swing to create this application. All the functions have been performed through the form and will modified the text file. Here, we haven't use database.

Here is the code:

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

class StudentApplication {
	JFrame f;
	JPanel p1, p2, p3, p, p4;
	JTabbedPane tp;
	ImageIcon btnimg1, btnimg2;
	JLabel l1, l2, l3, l4, l5, l6, l7, l8, l9, l10;
	JTextField tf1, tf2, tf3, tf4, tf5, tf6, tf7, tf8, tf9, tf10;
	JScrollPane sp1;
	JButton savebtn, resetbtn, editbtn1, editbtn2, delBtn;

	StudentApplication() {
		f = new JFrame("Form");
		p = new JPanel(new GridLayout(2, 1));
		p1 = new JPanel(new GridLayout(5, 2));
		p2 = new JPanel(new GridLayout(5, 2));
		p3 = new JPanel(new GridLayout(2, 2));
		tp = new JTabbedPane();
		l1 = new JLabel("ID:");
		l2 = new JLabel("Name:");
		l3 = new JLabel("Age:");
		l4 = new JLabel("Notes:");
		l5 = new JLabel("Enter ID to delete Record:");
		l7 = new JLabel("ID:");
		l8 = new JLabel("Name:");
		l9 = new JLabel("Age:");
		l10 = new JLabel("Notes:");
		tf1 = new JTextField(12);
		tf2 = new JTextField(12);
		tf3 = new JTextField(12);
		tf4 = new JTextField(12);
		tf5 = new JTextField(12);
		tf6 = new JTextField(12);
		tf7 = new JTextField(12);
		tf8 = new JTextField(12);
		tf9 = new JTextField(12);
		tf10 = new JTextField(12);
		savebtn = new JButton(" Add ");
		resetbtn = new JButton(" Reset");
		editbtn1 = new JButton(" Edit ");
		editbtn2 = new JButton(" Save");
		delBtn = new JButton("Delete");
		final JTextArea area = new JTextArea(10, 20);
		final JScrollPane pane = new JScrollPane(area);
		p1.add(l1);
		p1.add(tf1);
		p1.add(l2);
		p1.add(tf2);
		p1.add(l3);
		p1.add(tf3);
		p1.add(l4);
		p1.add(tf4);
		p1.add(savebtn);
		p1.add(resetbtn);
		p2.add(l7);
		p2.add(tf7);
		p2.add(l8);
		p2.add(tf8);
		p2.add(l9);
		p2.add(tf9);
		p2.add(l10);
		p2.add(tf10);
		p2.add(editbtn1);
		p2.add(editbtn2);
		p3.add(l5);
		p3.add(tf5);
		p3.add(delBtn);
		p.add(p3);
		pane.setVisible(false);

		resetbtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				tf1.setText("");
				tf2.setText("");
				tf3.setText("");
				tf4.setText("");
			}
		});
		savebtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				String value1 = tf1.getText();
				String value2 = tf2.getText();
				String value3 = tf3.getText();
				String value4 = tf4.getText();
				try {
					File file = new File("school.txt");
					FileWriter fstream = new FileWriter(file, true);
					BufferedWriter out = new BufferedWriter(fstream);
					out.write(value1 + " " + value2 + " " + value3 + " "
							+ value4);
					out.newLine();
					out.close();
					JOptionPane.showMessageDialog(null,
							"Data is successfully inserted.");
				} catch (Exception e) {
				}
			}
		});
		delBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				File f1 = new File("new.txt");
				File f2 = new File("school.txt");
				try {
					String value = tf5.getText();
					BufferedWriter output = new BufferedWriter(new FileWriter(
							f1));
					BufferedReader freader = new BufferedReader(new FileReader(
							f2));
					String s;
					while ((s = freader.readLine()) != null) {
						String[] f = s.split(" ");
						String id = f[0];
						String name = f[1];
						String c = f[2];
						String note = f[3];
						if (!id.equals(value)) {
							output.write(s);
							output.newLine();
						}
					}
					freader.close();
					output.close();
				} catch (Exception e) {
				}
				f2.delete();
				f1.renameTo(f2);
			}
		});
		editbtn1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				String value = tf7.getText();
				File f = new File("school.txt");
				try {
					BufferedReader freader = new BufferedReader(new FileReader(
							f));
					String s;
					while ((s = freader.readLine()) != null) {

						String[] st = s.split(" ");
						String id = st[0];
						String name = st[1];
						String c = st[2];
						String note = st[3];
						if (id.equals(value)) {
							tf7.setText(id);
							tf8.setText(name);
							tf9.setText(c);
							tf10.setText(note);
						}
					}
					freader.close();
				} catch (Exception e) {
				}
			}
		});
		editbtn2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				String t1 = tf7.getText();
				String t2 = tf8.getText();
				String t3 = tf9.getText();
				String t4 = tf10.getText();
				File f3 = new File("new.txt");
				File f4 = new File("school.txt");
				try {

					BufferedWriter output = new BufferedWriter(new FileWriter(
							f3));
					BufferedReader freader = new BufferedReader(new FileReader(
							f4));
					String s;
					while ((s = freader.readLine()) != null) {
						String[] f = s.split(" ");
						String id = f[0];
						String name = f[1];
						String c = f[2];
						String note = f[3];
						if (!id.equals(t1)) {
							output.write(s);
							output.newLine();
						}
					}
					freader.close();
					output.write(t1 + " " + t2 + " " + t3 + " " + t4);
					output.close();
				} catch (Exception e) {
				}
				f4.delete();
				f3.renameTo(f4);
			}
		});

		f.getContentPane().add(tp);
		tp.addTab("Add Record", p1);
		tp.addTab("Edit Record", p2);
		tp.addTab("Delete Record", p);
		f.setSize(450, 180);
		f.setVisible(true);
	}

	public static void main(String z[]) {
		StudentApplication app = new StudentApplication();
	}
}

Ads