Search This Blog

Thursday, 10 May 2018

CS506 - Assignment


public class Person {
              
               String CNIC;
               String Name;
               String DOB;
               String Address;
              
               public Person() {
                               super();
                               CNIC = "";
                               Name = "";
                               DOB = "";
                               Address = "";
               }
               public Person(String cNIC, String name, String dOB, String address) {
                               super();
                               CNIC = cNIC;
                               Name = name;
                               DOB = dOB;
                               Address = address;
               }

               public String getCNIC() {
                               return CNIC;
               }

               public void setCNIC(String cNIC) {
                               CNIC = cNIC;
               }

               public String getName() {
                               return Name;
               }

               public void setName(String name) {
                               Name = name;
               }

               public String getDOB() {
                               return DOB;
               }

               public void setDOB(String dOB) {
                               DOB = dOB;
               }

               public String getAddress() {
                               return Address;
               }

               public void setAddress(String address) {
                               Address = address;
               }
              
};


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Student extends Person {
              
               String ID;
               String Program;
               String Email;
              
               public Student() {
                               super();
                               ID = "";
                               Program = "";
                               Email = "";
               }
               public Student(String cNIC, String name, String dOB, String address, String iD, String program, String email) {
                               super(cNIC, name, dOB, address);
                               ID = iD;
                               Program = program;
                               Email = email;
               }

               public String getID() {
                               return ID;
               }

               public void setID(String iD) {
                               ID = iD;
               }

               public String getProgram() {
                               return Program;
               }

               public void setProgram(String program) {
                               Program = program;
               }

               public String getEmail() {
                               return Email;
               }

               public void setEmail(String email) {
                               Email = email;
               }
              
               public static void main(String[] args) throws IOException {
                              
                               System.out.println("\nPress 1 to add a new student.");
                               System.out.println("Press 2 to display all students.");
                               System.out.println("Press 3 to exit.");
                              
                               Scanner scanner = new Scanner(System.in);
                   int choice = scanner.nextInt();
                  
                   if (choice == 1) {
                             
                              JFrame frame = new JFrame();
                              frame.setAlwaysOnTop(true);
                             
                              String cnic = JOptionPane.showInputDialog(frame, " CNIC");
                              String name = JOptionPane.showInputDialog(frame, "Name");
                              String dob = JOptionPane.showInputDialog(frame, " Date of birth?");
                              String address = JOptionPane.showInputDialog(frame, " Address?");
                              String id = JOptionPane.showInputDialog(frame, " Student ID?");
                              String program = JOptionPane.showInputDialog(frame, "study program?");
                              String email = JOptionPane.showInputDialog(frame, "Email?");
                             
                              Student s = new Student(cnic, name, dob, address, id, program, email);
                             
                              try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Students.txt",true)))) {
                                  out.println(s.getCNIC() + "," + s.getName() + "," + s.getDOB() + "," + s.getAddress() + "," + s.getID() + "," + s.getProgram() + "," + s.getEmail());
                              }
                   
                   }
                   else if (choice == 2) {
                             
                              BufferedReader br = new BufferedReader(new FileReader("Students.txt"));
                       String line = null;
                       int count = 0;

                       while ((line = br.readLine()) != null) {
                              String[] values = line.split(",");
                              count++;
                              System.out.println("\nStudent " + count + ":\n");
                             
                              System.out.println("CNIC: " + values[0]);
                              System.out.println("Name: " + values[1]);
                              System.out.println("Date of Birth: " + values[2]);
                              System.out.println("Address: " + values[3]);
                              System.out.println("Student ID: " + values[4]);
                              System.out.println("Program: " + values[5]);
                              System.out.println("Email: " + values[6]);
                       }
                       br.close();
                   }
                   else if (choice == 3) {
                              System.exit(0);
                   }
               }
};


No comments:

Post a Comment