Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
3.4 kB
2
Indexable
Never
package com.company;

import jdk.swing.interop.SwingInterOpUtils;

import java.awt.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.*;

abstract class Person{

    public String name;
    public int age;

    Person(String name, int age){
        this.age=age;
        this.name=name;
    }
    abstract void typeofPerson();


}

class Student extends Person{

    private double cgpa;
    private String id;

    Student(String name, int age, double cgpa, String id) {
        super(name, age);
        this.cgpa=cgpa;
        this.id=id;
    }

    String getStudentname()
    {
        String name = super.name;
        return name;
    }

    @Override
    void typeofPerson() {
        System.out.println("This is a Student class");
    }


    public static Comparator<Student> StuNameComparator = new Comparator<Student>() {

        public int compare(Student s1, Student s2) {
            String StudentName1 = s1.getStudentname().toLowerCase();
            String StudentName2 = s2.getStudentname().toLowerCase();

            return StudentName1.compareTo(StudentName2);

        }};

    @Override
    public String toString() {
        return "[ ID= " + id + ", name= " + name + ", cgpa= " + cgpa + ", age= "+age+"]";
    }




}
class Teacher extends Person{

    private int salary;

    Teacher(String name, int age, int salary) {
        super(name, age);
        this.salary=salary;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    @Override
    void typeofPerson() {
        System.out.println("This is a Teacher Class");
    }



    public static Comparator<Teacher> Com_Salary = new Comparator<Teacher>() {

        public int compare(Teacher s1, Teacher s2) {

            int sal1 = s1.getSalary();
            int sal2 = s2.getSalary();

            return sal1-sal2;

        }};

    @Override
    public String toString() {
        return "[ Name=" + name + ", Age=" + age + ", Salary=" + salary + "]";
    }


}

class TestAbstraction1{
    public static void main(String args[]) throws Exception {
        FileReader fr=new FileReader("D:\\people.txt");
        BufferedReader br=new BufferedReader(fr);

        ArrayList<Student>s_list=new ArrayList<Student>();
        ArrayList<Teacher>t_list=new ArrayList<Teacher>();
        String i;

        while(((i=br.readLine())!=null)){
            String Words[] = i.split(",");

            /*for(String s: Words)
                System.out.println(s);*/

            if(Words[0].equals('s'))
            {
                s_list.add(new Student(Words[1],Integer.parseInt(Words[2]),Double.parseDouble(Words[3]),Words[4]));
            }

            else if(Words[0].equals('t')){
                t_list.add(new Teacher(Words[0],Integer.parseInt(Words[1]),Integer.parseInt(Words[2])));
            }


        }
        
        Collections.sort(s_list, Student.StuNameComparator);
        Collections.sort(t_list, Teacher.Com_Salary);

        for(Student str: s_list){
            System.out.println(str);
        }
        for(Teacher str2: t_list){
            System.out.println(str2);
        }

        br.close();
        fr.close();
    }
}