Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

one to many relation between objects

import java.util.*;
 
//Professor Class
class Professor {
 private String name;
 
 //Assign professor name
 Professor(String name)
 {
   this.name = name;
 }
 
 //Retrieve name
 public String getName()
 {
   return this.name;
 }
 
}
 
//Department Class
class Department {
 private String name;
 List<Professor> staff;
 
 //Assign department name
 Department(String name)
 {
   this.name = name;
 }
 
 //Retrieve name
 public String getName()
 {
   return this.name;
 }
 
 //Assign list of department staff
 public void setStaff(List<Professor> staff)
 {
   this.staff = staff;
 }
 
 //Return the list of staff names
 public List<String> getStaff()
 {
    
   List<String> professor_names = new ArrayList<String>();
   for(Professor prof : this.staff)
   {
     professor_names.add(prof.getName()); //add name of each professor to names list
   }
   return professor_names; // return names list
 }
}
 
 
 
class Main {
 public static void main(String[] args) {
 
   //Create two professor objects
    Professor ak = new Professor("Arun Kumar");
    Professor ry = new Professor("Rahul Yadav");
 
    //Create a department
    Department cse = new Department("CSE");
 
    //Add the professors to a list and add this list to department
    List<Professor> staff_cse = new ArrayList<Professor>();
    staff_cse.add(ak);
    staff_cse.add(ry);
 
    //Add staff to cse department
    cse.setStaff(staff_cse);
 
    //Output
 
    System.out.println("The staff of department " + cse.getName() + " is: " + cse.getStaff());
 
 
 }
}
Comment

PREVIOUS NEXT
Code Example
Java :: EXPECTTIONS JAVA 
Java :: Java Labeled continue Statement Java 
Java :: konva crop outside width and height of image 
Java :: gradle project load test data json file with jackson 
Java :: radio button lambda javafx 
Java :: java catch stop signal 
Java :: Was ist ein String in java 
Java :: Sauvegarder une partie en cours dans un fichier texte java 
Java :: how to mutate value in vector in java 
Java :: print jtable in java 
Java :: Java Protected Access Modifier package two 
Java :: how to read json object from text file in java 
Java :: Java Create a FileOutputStream 
Java :: sfadffocusbutton 
Java :: para que sirve getcontentpane en java 
Java :: Java Method Overloading by changing the data type of parameters 
Java :: java blocks 
Java :: create file with java 
Java :: java include class 
Java :: what singleton java spring 
Java :: komplettes array ausgeben java 
Java :: number output swing java 
Java :: Accept Integer only in if else statement 
Java :: View get text android Close 
Java :: How to code the Fibonacci Sequence using simple iterative loops java 
Java :: ignore sonarlint line java 
Java :: how to use advanced math in java 
Java :: regex plit string with dash or undescore java 
Java :: customize the androidx.appcompat.widget.SearchView site:stackoverflow.com 
Java :: focus_button_java_swing 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =