Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MainPerson
- import java.util.ArrayList;
- public class MainPerson{
- public static void main(String[] args) {
- /*Add to List*/
- Person p = new Person();
- p.setName("Guita");
- p.setAge(22);
- System.out.println("-- Person --");
- p.printDetails();
- Teacher t = new Teacher("Khunpiny",22,"Coe");
- t.setFaculty("CoE");
- System.out.println("-- Teacher --");
- System.out.println(t.getName()+" " + t.getAge() +" " + t.getFaculty());
- Employee e = new Employee("Bow_e",22,"57_062");
- e.setemployeeId("5735512062");
- System.out.println("-- Employee --");
- System.out.println(e.getName()+" " + e.getAge() +" " + e.getemployeeId());
- ContractEmployee c = new ContractEmployee("Sirirat",22,"57_148",30000);
- c.setConsalary(50000);
- System.out.println("-- ContractEmployee --");
- System.out.println(c.getName()+" " + c.getAge() +" " + c.getemployeeId() +" " + c.getConsalary());
- PermanentEmployee per = new PermanentEmployee("Aunyamane",22,"57_149",30000);
- per.setPersalary(45000);
- System.out.println("-- PermanentEmployee --");
- System.out.println(per.getName()+" " + per.getAge() +" " + per.getemployeeId() +" " + per.getPersalary());
- System.out.println("-- ----------------- --");
- ArraylistPerson p1 = new ArraylistPerson();
- p1.addPerson(p);
- p1.addPerson(t);
- p1.addPerson(e);
- p1.addPerson(per);
- p1.addPerson(c);
- p1.list();
- }
- }
- // Person
- public class Person {
- private String name;
- private int age;
- public Person(){
- }
- public Person(String n, int a){
- name = n;
- age = a;
- }
- public void printDetails () {
- System.out.println("Name: " + name);
- System.out.println("Age: " + age);
- }
- public String getName(){
- return name;
- }
- public void setName(String n){
- this.name = n;
- }
- public int getAge(){
- return age;
- }
- public void setAge(int a){
- this.age = a;
- }
- public String getFaculty() {
- return "";
- }
- public String getemployeeId() {
- return "";
- }
- public int getPersalary(){
- return 0;
- }
- public int getConsalary(){
- return 0;
- }
- }
- //ArrayListPerson
- import java.util.ArrayList;
- public class ArraylistPerson{
- private ArrayList<Person> items;
- public ArraylistPerson(){
- items = new ArrayList<Person>();
- }
- public void addPerson(Person theItem){
- items.add(theItem);
- }
- public void list()
- {
- for(Person item : items){
- if(item.getFaculty() != ""){
- System.out.print("Teacher : ");
- System.out.println(item.getName() +" "+ item.getAge() +" "+ item.getFaculty());
- }
- else if((item.getFaculty() == "") &&item.getemployeeId() != "" && (item.getConsalary()== 0) && (item.getPersalary()==0)){
- System.out.print("Employee : ");
- System.out.println(item.getName() +" "+ item.getAge() +" "+ item.getemployeeId() );
- }
- else if(item.getFaculty() == "" &&item.getemployeeId() != "" && (item.getPersalary()!= 0)){
- System.out.print("PermanentEmployee : ");
- System.out.println(item.getName() +" "+ item.getAge() +" "+ " "+ item.getemployeeId()+" " + item.getPersalary());
- }
- else if(item.getFaculty() == "" && (item.getConsalary()!= 0)){
- System.out.print("ContractEmployee : ");
- System.out.println(item.getName() +" "+ item.getAge() +" "+ item.getemployeeId() +" "+ item.getConsalary());
- }
- else{
- System.out.print("Person : ");
- System.out.println(item.getName() +" "+ item.getAge());
- }
- }
- }
- }
- // Teacher
- public class Teacher extends Person{
- private String faculty;
- public Teacher(String name, int age,String f){
- super(name, age);
- faculty = f;
- }
- public String getFaculty(){
- return faculty;
- }
- public void setFaculty (String f) {
- faculty = f;
- }
- }
- // Employee
- public class Employee extends Person{
- private String employeeId;
- public Employee(String name, int age,String em){
- super(name, age);
- employeeId = em;
- }
- public String getemployeeId(){
- return employeeId;
- }
- public void setemployeeId (String em) {
- employeeId = em;
- }
- }
- // ContractEmployee
- public class ContractEmployee extends Employee{
- private int payment;
- public ContractEmployee(String name, int age,String em,int pay){
- super(name, age,em);
- payment = pay;
- }
- public int getConsalary(){
- return payment;
- }
- public void setConsalary (int pay) {
- payment = pay;
- }
- }
- // PermanentEmployee
- public class PermanentEmployee extends Employee{
- private int salary;
- public PermanentEmployee(String name, int age,String em,int salary){
- super(name, age,em);
- this.salary = salary;
- }
- public int getPersalary(){
- return this.salary;
- }
- public void setPersalary(int salary) {
- this.salary = salary;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment