Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

package test1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Pattern;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

class Person {

private String cnic, name, dateOfBirth, address;

public Person() {

public Person(String cnic, String name, String dateOfBirth, String address) {


this.cnic = cnic;
this.name = name;
this.dateOfBirth = dateOfBirth;
this.address = address;
}

public String getCnic() {


return cnic;
}

public void setCnic(String cnic) {


this.cnic = cnic;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getDateOfBirth() {


return dateOfBirth;
}

public void setDateOfBirth(String dateOfBirth) {


this.dateOfBirth = dateOfBirth;
}

public String getAddress() {


return address;
}

public void setAddress(String address) {


this.address = address;
}
}

public class Student extends Person {

private String studentId, studentProgram, email;

public Student() {

public Student(String studentId, String studentProgram, String email, String


cnic, String name, String dateOfBirth, String address) {
super(cnic, name, dateOfBirth, address);
this.studentId = studentId;
this.studentProgram = studentProgram;
this.email = email;

public String getStudentId() {


return studentId;
}

public void setStudentId(String studentId) {


this.studentId = studentId;
}

public String getStudentProgram() {


return studentProgram;
}

public void setStudentProgram(String studentProgram) {


this.studentProgram = studentProgram;
}

public String getEmail() {


return email;
}

public void setEmail(String email) {


this.email = email;
}

public static void main(String[] args) throws IOException {


Student std;
String cnic, name, dateOfBirth, address, studyProgram, email;
String studentId;
File file = new File("students.txt");
FileWriter fw;
BufferedWriter bw;
FileReader fr;
BufferedReader br;
JFrame frame = new JFrame();
do {
Byte b = Byte.parseByte(JOptionPane.showInputDialog(frame, "Press 1 to
add a new student.\nPress 2 to display all students.\nPress 3 to exit."));
switch (b) {
case 1:
String temp;
temp = JOptionPane.showInputDialog(frame, "Enter CNIC:");
while (true) {
if (Pattern.matches("[0-9]{5}[-]{1}[0-9]{7}[-]{1}[0-9]{1}",
temp)) {
cnic = temp;
break;
} else {
JOptionPane.showMessageDialog(frame, "Invalid cnic
format", "Error", JOptionPane.ERROR_MESSAGE);
temp = JOptionPane.showInputDialog(frame, "Enter
CNIC:");
}
}
temp = JOptionPane.showInputDialog(frame, "Enter Name:");
while (true) {
if ((Pattern.matches("[A-Za-z\\s]+", temp))) {
name = temp;
break;
} else {
JOptionPane.showMessageDialog(frame, "Invalid name",
"Error", JOptionPane.ERROR_MESSAGE);
temp = JOptionPane.showInputDialog(frame, "Enter
Name:");
}
}
dateOfBirth = JOptionPane.showInputDialog(frame, "Enter Date of
Birth:");
address = JOptionPane.showInputDialog(frame, "Enter Address:");
studentId = JOptionPane.showInputDialog(frame, "Enter Student
ID:");
studyProgram = JOptionPane.showInputDialog(frame, "Enter Study
Program:");
temp = JOptionPane.showInputDialog(frame, "Enter Email:");
while (true) {
if (Pattern.matches("[\\w\\S]+", temp)) {
email = temp;
break;
} else {
JOptionPane.showMessageDialog(frame, "Invalid email",
"Error", JOptionPane.ERROR_MESSAGE);
temp = JOptionPane.showInputDialog(frame, "Enter
Eamil:");
}
}
std = new Student(studentId, studyProgram, email, cnic, name,
dateOfBirth, address);
fw = new FileWriter(file.getAbsoluteFile(), true);
bw = new BufferedWriter(fw);
bw.write(std.getCnic() + ", " + std.getName() + ", " +
std.getDateOfBirth() + ", " + std.getAddress() + ", " + std.getStudentId() + ", " +
std.getStudentProgram() + ", " + std.getEmail());
bw.newLine();
bw.close();
break;
case 2:
fr = new FileReader(file);
br = new BufferedReader(fr);
String line;
byte i = 0;
while ((line = br.readLine()) != null) {
String[] s = line.split(",");
System.out.println("Student " + ++i + ":\n");
System.out.println("CNIC : " + s[0]);
System.out.println("Name : " + s[1]);
System.out.println("Date of Birth : " + s[2]);
System.out.println("Address : " + s[3]);
System.out.println("Student ID : " + s[4]);
System.out.println("Program : " + s[5]);
System.out.println("Email : " + s[6] + "\n");
}
break;
case 3:
System.exit(0);
}
} while (true);
}
}

You might also like