简体   繁体   中英

LinkedList in JAVA to call methods from other class

This is part of my code (I didn't upload the complete code since it's too long to read):

Student Class: ( Enter the subject number, and call methods from University class )

import java.util.*;

public class Student 
{
private String number;
private String name;
private LinkedList<Activity> activities = new LinkedList<Activity>();
}
public void enrol(University university)
{
    System.out.println("Select a subject");
    System.out.println("48024 Applications Programming");
    System.out.println("31284 Web Services Development");
    int number = selectSubject();

    if(number == 48024)
    {
        university.showActivity(university.subject(number));
    }
}

University Class: ( return subject type based on the subject number, and use that subject "ap" to call showActivity() method in subject class.

public class University
{
private LinkedList<Subject> subjects = new LinkedList<Subject>();

public University()
{
Subject ap = new Subject(48024, "Application Programming"); 
    ap.addActivity("Lec1", 1, "Wed", 18, 1, "CB11.00.405", 200);
    ap.addActivity("Cmp1", 1, "Wed", 19, 2, "CB11.B1.403", 2);
    ap.addActivity("Cmp1", 2, "Wed", 19, 2, "CB11.B1.401", 2);
    ap.addActivity("Cmp1", 3, "Wed", 19, 2, "CB11.B1.402", 2);
    subjects.add(ap);
}
public Subject subject(int number)
{
    Subject ap = new Subject(48024, "Application Programming"); 
    if(number == 48024)
    {
        return ap;
    }
    return null;
}
public void showActivity(Subject subject)
{
    subject.showActivity();
}

Subject Class: ( print out all activities under that subject )

public void showActivity()
{
    for(Activity list : activities)
    {
        System.out.println(list);
    }
}

There is one more Activity class with toString method.

In the Student Class, once user enters the subject number 48024, it should call the methods in University Class then show all activities under the subject 48024.

Now once I entered 48024 as subject number, nothing shows up. I don't understand where is wrong. Please help!

The method

public Subject subject(int number)
{
    Subject ap = new Subject(48024, "Application Programming"); 
    if(number == 48024)
    {
        return ap;
    }
    return null;
}

is creating a new Subject, since it is new, it will not have any Activity associated.

You need to add activities to it in order to show them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM