1

I using MVC for developing a web project in Java.
My problem is that i need to return three different beans from a class. All three beans have multiple objects so I am right now adding each same bean object in a list and returning three different lists.
Okay to make it more clear I need to retrieve all contents from a table that stores comments. So all comment text are stored in one bean called comment and added in a list called listcomment. The name of member who made the comment are added in another bean called member and again those are added in list called listmember.
So is there any possible way so that both these beans can be added in a same list?

public class TeleCommentView {

int qid;
TeleComment comment;
TeleHospital hospital;
doctorperson doctor;

ConnectionFile connection = new ConnectionFile();
List<TeleComment> listcomment = new ArrayList<TeleComment>();
List<doctorperson> listdoctor = new ArrayList<doctorperson>();
List<TeleHospital> listhospital = new ArrayList<TeleHospital>();




public TeleCommentView(int qid)
{
    this.qid = qid;
    process();
}
public void process()
{
    int count=0;
    try
    {
    Connection con = connection.connectionfile();
    PreparedStatement  pstmt = con.prepareStatement("select TeleHospital.HospitalName,DoctorDetail.Name,TeleComment.Comment,TeleComment.SDate from"
                                                     + "( (TeleComment left join TeleHospital on TeleHospital.HospitalId=TeleComment.Hid) "
                                                    + "left join DoctorDetail on DoctorDetail.DoctorId = TeleComment.Did) "
                                                    + "where TeleComment.Qid=?");
    ResultSet rs = pstmt.executeQuery();

    while(rs.next())
    {
        comment = new TeleComment();

        comment.setComment(rs.getString("Comment"));
        comment.setSdate(rs.getDate("SDate"));

        listcomment.add(count,comment) ;

        /******End of comment**************/

        //Add doctor or hospital name as required

        doctor = new doctorperson();
        hospital = new TeleHospital();

        if(rs.getString("HospitalName").equals(null))
            {
                doctor.setName(rs.getString("Name"));
                listdoctor.add(count,doctor);
            }
            else
                {
                    hospital.setHospitalname(rs.getString("HospitalName"));
                    listhospital.add(count,hospital);
                 }
        count++;
         }
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }

}

  public List getCommentList()
  {
   return listcomment;
  }

 public List getDoctorList()
  {
   return listdoctor;
  }

 public List getHospitalList()
  {
   return listhospital;
  }

 }
ntstha
  • 1,187
  • 4
  • 23
  • 41

1 Answers1

2

If the different beans all contain a certain method (or methods), you can create an interface and make each bean implement it.

Here's a similar question that just got asked:

Treating different objects the same

Silly example code:

interface CommentItem {
    public String getComment();
}


class ModeratorComment implements CommentItem {
    public String getComment() {
        return "Comment from moderator";
    }
    // other moderator-specific code...
}



class StudentComment implements CommentItem {
    public String getComment() {
        return "Comment from student";
    }
    // other student-specific code...
}


class CommentContainer {

    private List<CommentItem> commentList;

    public List<CommentItem> getCommentList() {
        return commentList;
    }

    public void addComment(CommentItem someComment) {
        commentList.add(someComment);
    }
}


class TestIt() {

    public static void main(String[] args) {

        StudentComment sc = new StudentComment();
        ModeratorComment mc = new ModeratorComment();

        CommentContainer comments = CommentContainerFactory.createCommentContainer();
        comments.add(sc);
        comments.add(mc);

        for (CommentItem ci : comments.getCommentList()) {
            System.out.println(ci.getComment());
        }

    }

}
Community
  • 1
  • 1
jahroy
  • 22,322
  • 9
  • 59
  • 108