1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

about many to many relationship?

Discussion in 'Java' started by angus203, Mar 10, 2008.

  1. angus203

    angus203 New Member

    Joined:
    Jan 13, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    could you help check this code.
    why IT2399/3A & IT2399/3B same answer of lecturer name? it may be opposite.


    Code:
    import java.util.*;
    
    class Course
    {
    	String CourseName;
    	int NumberOfStudetn;
    
    	Vector activity;
    
    	public Course(String CourseName, int NumberOfStudetn){
    		this.CourseName = CourseName;
    		this.NumberOfStudetn = NumberOfStudetn;
    		
    		activity = new Vector();
    	}
    
    	public void addActivity(Activity act){
    		activity.add(act);
    	}
    
    	public Enumeration getModule() {
    		Vector module = new Vector();
    		for (int i = 0; i < activity.size(); i++){
    			module.add(((Activity) activity.elementAt(i)).getModule());
    		}
    		
    		return module.elements();
    	 }
    }
    
    class Lecturer
    {
    	String FirstName;
    	String LastName;
    
    	Vector activity;
    
    	public Lecturer(String fn, String ln){
    		FirstName = fn;
    		LastName = ln;
    
    		activity = new Vector();
    	}
    
    	public void addActivity(Activity act){
    		activity.add(act);
    	}
    
    	public Enumeration getModule() {
    		Vector module = new Vector();
    		for (int i = 0; i < activity.size(); i++){
    			module.add(((Activity) activity.elementAt(i)).getModule());
    		}
    		
    		return module.elements();
    	 }
    }
    
    class Module
    {
    	String ModuleName;
    	int LectureTime;
    	int TutorialTime;
    
    	Vector activity;
    
    	public Module(String ModuleName, int LectureTime, int TutorialTime){
    		this.ModuleName = ModuleName;
    		this.LectureTime = LectureTime;
    		this.TutorialTime = TutorialTime;
    
    		activity = new Vector();
    	}
    
    	public void addActivity(Activity act){
    		activity.add(act);
    	}
    
    	public Enumeration getCourse() {
    		Vector course = new Vector();
    		for (int i = 0; i < activity.size(); i++){
    			course.add(((Activity) activity.elementAt(i)).getCourse());
    		}
    		
    		return course.elements();
    	 }
    
    	 public Enumeration getLecturer() {
    		Vector lecturer = new Vector();
    		for (int i = 0; i < activity.size(); i++){
    			lecturer.add(((Activity) activity.elementAt(i)).getLecturer());
    		}
    		
    		return lecturer.elements();
    	 }
    }
    
    class Activity
    {
    	Course course;
    	Module module;
    	Lecturer lecturer;
    	
    	public Activity(Course c, Module m, Lecturer l){
    		course = c;
    		module = m;
    		lecturer = l;
    	}
    
    	static public void CreateActivity(Course c, Module m, Lecturer l){
    		Activity act = new Activity(c, m, l);
    		c.addActivity(act);
    		m.addActivity(act);
    		l.addActivity(act);
    
    	}
    
    	public Module getModule(){
    		return module;
    	}
    
    	public Course getCourse(){
    		return course;
    	}
    
    	public Lecturer getLecturer(){
    		return lecturer;
    	}
    }
    
    public class Main_Org {
    
    	public static void main(String argv[]) {
    
    		String courseNames[] = {"IT2399/3A", "IT2399/3B", "IT2399/1A", "IT2399/1B", "IT2399/2A"};
            String ModuleNames[] = {"J2ME", "J2SE", "ABAP", "C#", "VB"};
    		String LecturerNames[] = {"Peter", "Jason", "Tom", "Jack", "Mark"};
    
    		Course courses[] = new Course[5];
            Module modules[] = new Module[5];
    		Lecturer lecturers[] = new Lecturer[LecturerNames.length];
    
    		for (int i = 0; i < 5; i++) {
    			courses[i] = new Course(courseNames[i], 30);
    			modules[i] = new Module(ModuleNames[i], 1, 2);
    			lecturers[i] = new Lecturer(LecturerNames[i], LecturerNames[i]);
    			
    		}
    
    		Activity.CreateActivity(courses[0], modules[0], lecturers[3]);
    		Activity.CreateActivity(courses[0], modules[1], lecturers[1]);
    		Activity.CreateActivity(courses[0], modules[2], lecturers[2]);
    		Activity.CreateActivity(courses[0], modules[3], lecturers[3]);
    		Activity.CreateActivity(courses[0], modules[4], lecturers[4]);
    
    		Activity.CreateActivity(courses[1], modules[0], lecturers[4]);
    		Activity.CreateActivity(courses[1], modules[1], lecturers[3]);
    		Activity.CreateActivity(courses[1], modules[2], lecturers[2]);
    		Activity.CreateActivity(courses[1], modules[3], lecturers[1]);
    		Activity.CreateActivity(courses[1], modules[4], lecturers[0]);
    		
    
    		for (int i = 0; i < courses.length; i++ )
    		{
    		
    			Enumeration e = courses[i].getModule();
    			while (e.hasMoreElements())
    			{
    				Module mod = (Module)e.nextElement();
    				String name = ((Lecturer)(mod.getLecturer().nextElement())).FirstName;	// why IT2399/3A & IT2399/3B same answer of lecturer name. it may be opposite.
    				
    				System.out.println(courses[i].CourseName + ": " + mod.ModuleName + " teach by " + name );
    			}
    			System.out.println();
    		}
    
    	}
    
    }
    
     

Share This Page