-
Passing Objects
Hello dear developers
I am desigining & implementing a system that follows these classes: News papers awhich they are exchanging news using news agencies. A news paper register itself at a news agency and sends their news to that agency. The news agency receives news and sends them to all registred news papers.
I have four classes News,Newspaper,Main,Agency.
I have send the newspaper and the news to the agency but I don't know how can I list of all newspapers and forward the news to other newspaper.
Can anyone help me please?
-
The Agency would likely have a collection of Newspapers, perhaps an ArrayList or a HashMap, and I imagine that to broadcast to all the newspapers the agency would simply iterate through its collection, sending to each paper in the collection.
-
Code:
private ArrayList<NewsPaperD> list = new ArrayList<NewsPaperD>();
private NewsPaperD getNewsPaperData(NewsPaper np,News n)
{
String s = n.getNews();
for(NewsPaperD nd:list)
{
if(nd.getNewsPaper()==np )
return nd;
}
System.out.println("Newspaper: "+np);
NewsPaperData nd=new NewsPaperData(np,s);
for(int i=0;i<list.size();i++)
{
list.set(i, nd);
}
list.add(nd);
return nd;
}
I have done like above and the problem is that right now it will only show the first NEWS related to each NewsPaper.
I have added the result also:
Code:
Newspaper: Local
Local: News = Title1 nNewsPaper: Local, tTitle = Title1
Local: News = Title2 nNewsPaper: Local, tTitle = Title1
Local: News = Title3 nNewsPaper: Local, tTitle = Title1
Newspaper: Times
Times : News = News1 nNewsPaper: Times , tTitle = News1
Times : News = News2 nNewsPaper: Times , tTitle = News1
Times : News = News3 nNewsPaper: Times , tTitle = News1
Can you please help me with that?
-
I don't really know what you're trying to do with this snippet of code or really what your problems are.
Please have a look here as it may show you how to avoid making assumptions about what we know about your current problem, so that your questions may be more understandable: smart questions
-
Ok sorry for that.
actually my problem is that I don't know how can I send an object to other objects. and you have guided me in this way.
The Agency would likely have a collection of Newspapers, perhaps an ArrayList or a HashMap, and I imagine that to broadcast to all the newspapers the agency would simply iterate through its collection, sending to each paper in the collection.
I have followed the steps you mentioned like this :
Code:
//collection of Newspapers
private ArrayList<NewsPaperD> list = new ArrayList<NewsPaperD>();
//broadcast to all the Newspaper from agency
Code:
private NewsPaperD getNewsPaperData(NewsPaper np,News n)
{
String s = n.getNews();
for(NewsPaperD nd:list)
{
if(nd.getNewsPaper()==np )
return nd;
}
System.out.println("Newspaper: "+np);
NewsPaperData nd=new NewsPaperData(np,s);
for(int i=0;i<list.size();i++)
{
list.set(i, nd);
}
list.add(nd);
return nd;
}
But as I have mentioned the result is just the first News that each Newspaper has which is broadcasted to the same Newspaper which is wrong I want to send all the News that each Newspaper have to all other NewsPapers. Can you please guide me?
Here is the result that right now I got.
Code:
Newspaper: Local
Local: News = Title1 NewsPaper: Local, News = Title1
Local: News = Title2 NewsPaper: Local, News = Title1
Local: News = Title3 NewsPaper: Local, News = Title1
Newspaper: Times
Times : News = News1 NewsPaper: Times , News = News1
Times : News = News2 NewsPaper: Times , News = News1
Times : News = News3 NewsPaper: Times , News = News1
-
What you want to do is have a method like getNewsPaperData, and have that method be public. Then, each NewsPaper should have a List of all the other newspapers it receives data from. To get data from all of its newspapers that it's subscribed to, create a method called getAllNewspaperData() which loops through the List and calls getNewsPaperData on each one.
getNewsPaperData would simply return the news from each Newspaper.
It may also help if you tell us what all of the classes you use are. You listed them in your first post but it would be better if we knew what each one actually did. I also see a NewsPaperData object, and I'm having trouble figuring out why you'd create a new NewsPaperData object inside of your method.
-
Your getNewsPaperData method confuses me again as it does not appear to be a method for broadcasting to all papers. First it takes a NewsPaper parameter, np (what is the difference by the way between the NewsPaper class and the NewsPaperD class?), and then iterates through the list looking for this paper. If it finds this newspaper, the method returns it and ends, end of story, and no broadcast occurs. That doesn't make sense. I'm also a little concerned that you're using == to check for equality rather than the equals method as most all your classes should have a decent equals override (and perhaps a hashcode override as well).
If I would have a newspaper broadcast method, it would look something like:
Code:
private void broadcastNews(News news) {
for (NewsPaperD nd : list) {
nd.getNewsPaper().receiveNews(news); // or whatever method you use to send news to a newspaper
}
}