-1

I have a class look like this:

public class People {
    private String Name;
    private String Address;
    public People(String aName, String aAddress) {
        this.Name=aName;
        this.Address=aAddress;
    }
    public String getName() {
        return Name;
    }
    public String getAddress() {
        return Address;
    }
    void display() {
        System.out.println("Name:\t"+Name);
        System.out.println("Address:\t" +Address);
    }
}

and another 2:

class Students extends People{
    private int  MatriculationNumber;
    private String CourseName;
    public Students (String aName, String aAddress, int matriculationNumber, String courseName){
        super(aName,aAddress);
        this.MatriculationNumber=matriculationNumber;
        this.CourseName=courseName;
    }
    void display() {
        super.display();
        System.out.println("Matriculation Number: \t" +MatriculationNumber);
        System.out.println("Course Name: \t" +CourseName);
    }
}
class Staffs extends People{
    private int  EmployeeNumber;
    private String Department;
    public Staffs (String aName, String aAddress, int employeeNumber, String department){
        super(aName,aAddress);
        this.EmployeeNumber=employeeNumber;
        this.Department=department;
    }
    void display() {
        super.display();
        System.out.println("Employee Number: \t" +EmployeeNumber);
        System.out.println("Department: \t" +Department);
    }
}

The question is how to create a class named "School" which have a List can contain both Students and Staffs, so I can add a method like AddPeople() which can add or remove Students or Staffs from it?

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 3
    On StackOverflow, to ask a good question, you have to show what you tried and what problem you exactly encountered. – GeertPt Jan 19 '21 at 09:44
  • You can create a static method `addPeople` for `School` such that it can be called by the class name. But I think it might be more reasonable to define `addPeople` as an instance method that can only be called from an instance of school. – PIG208 Jan 19 '21 at 09:46
  • Create two collections inside, one holding Staff, second Students. Then you can create two methods: addStaff and addStudent. – Beri Jan 19 '21 at 09:48
  • So you want one list which can contain both Students and Staff? – Aalexander Jan 19 '21 at 09:49
  • Note that you should follow the Java Naming Conventions: variable names and method names are written in camelCase. – MC Emperor Jan 19 '21 at 10:54
  • Oh yes, u right i want one list can contain both student and staff, sorry i thought to much so i don't know what to focus to ask – Anh Minh Diệp Jan 20 '21 at 18:37

2 Answers2

0

You can create a new Staff Class which also extends from the People Class

package so;

public class Staff extends People {
    
    public Staff(String aName,String aAddress) {
        super(aName, aAddress);
    }
    
}

Then give as type of your list the base class People.

package so;

import java.util.ArrayList;
import java.util.List;

public class School {
    List<People> peopleContainer = new ArrayList<People>();
    
    
    public void addPeople(People p){
        this.peopleContainer.add(p);
    }
    
     public void removePeople(People p) {
         this.peopleContainer.remove(p);
    }
    
    public void displayPeople() {
        for(People person : peopleContainer) {
                person.display();
        }
    }
}

Then you can add staff and student objects to your list and also all other objects which classes extends from people or even people objects itself.

package so;

public class Main {
    public static void main(String[] args) {
        Students stud1 = new Students("Max", "MustermanAddress", 181242, "CS50");
        Staff staff1 = new Staff("Christian", "AugustAddress");
        School school = new School();
        school.addPeople(stud1);
        school.addPeople(staff1);
        school.displayPeople();
        school.removePeople(staff1);
        System.out.println();
        System.out.println();
        school.displayPeople();

        
    }
}

Output will be

Name:   Max
Address:    MustermanAddress
Matriculation Number:   181242
Course Name:    CS50
Name:   Christian
Address:    AugustAddress


Name:   Max
Address:    MustermanAddress
Matriculation Number:   181242
Course Name:    CS50
Aalexander
  • 4,987
  • 3
  • 11
  • 34
  • `ArrayList peopleContainer = new ArrayList();` better be `List peopleContainer = new ArrayList<>()` ... – GhostCat Jan 19 '21 at 10:14
  • @GhostCat ty for the advice why would you prefer the List? – Aalexander Jan 19 '21 at 10:25
  • Because you prefer to work against interfaces, not specific implementations. The fact that you are using an ArrayList an implementation detail, you avoid relying on it. One way to do it: give the field the interface type. – GhostCat Jan 19 '21 at 12:48
  • Ty @GhostCat you are right I have found a nice SO explanation https://stackoverflow.com/questions/9852831/polymorphism-why-use-list-list-new-arraylist-instead-of-arraylist-list-n – Aalexander Jan 19 '21 at 14:29
  • Yep, and I think you can find even more other questions around the same subject ;-) – GhostCat Jan 19 '21 at 14:35
0

You can use the below solution. The solution is not thread-safe but should work fine for your usecase.

import java.util.ArrayList;

class School {
    private List<People> peopleList = new ArrayList<People>();

    public void addPeople(People people) {
        peopleList.add(people);
    }

    public void removePeople(People people) {
        peopleList.remove(people);
    }

    public static void main(String... args) {
        Student student = new Student("Name", "Address", 90, "Course");
        Staff staff = new Staff("Name", "Address", 1001, "Department");
    
        School school = new School();
        school.addPeople(student);
        school.addPeople(staff);

        school.removePeople(student);
        school.removePeople(staff);
    }
}
Deepanshu
  • 291
  • 1
  • 3
  • 8
  • Wew, i think i have to read more about this (although I spent 4 hours reading this). But thanks so much, your solutions help me so so much guys, i can't try this right now but i thought this will work – Anh Minh Diệp Jan 20 '21 at 18:43