Inheritance in Java

mail@pastecode.io avatar
unknown
java
2 years ago
2.2 kB
11
Indexable
Never
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Person {
    public String firstName;

    public String lastName;

    public  String personalCode;

    public String profession;

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getPersonalCode() {
        return personalCode;
    }

    public String getProfession() {
        return profession;
    }

    public Person(String fname, String lname, String code, String spec) {
        this.firstName = fname;
        this.lastName = lname;
        this.personalCode = code;
        this.profession = spec;
    }

    public List<Person> getAll(String csvFile) {
        List<Person> person = new ArrayList<Person>();
        try {
            DataInputStream in = new DataInputStream(Person.class.getResourceAsStream(csvFile));
            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            String strLine;
            while ((strLine = br.readLine()) != null)   {
                String[] tokens = strLine.split(",");
                person.add(new Person(tokens[0], tokens[1], tokens[2], tokens[3]));
            }

            in.close();

        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
        return person;
    }
}


public class Patient extends Person{

    public Patient(String fname, String lname, String code, String spec){
        super(fname, lname, code, spec);
    }

    public String toString() {
        return "Patsient [" +
            "eesnimi = " + super.firstName + "; " +
            "perenimi = " + super.lastName + "; " +
            "isikukood = " + super.personalCode + "; " +
            "amet = " + super.profession +
        "]";
    }
}


import java.util.List;

public class Main {

    public static void main(String[] args) {
        List<Patient> patients = Patient.getAll("patients.csv");
        
        for (Patient patient : patients) {
            System.out.println(patient);
        }
    }
    
}