Java Reference
In-Depth Information
Code 8.3
continued
Source code of the
NewsFeed class
/**
* Show the news feed. Currently: print the news feed details
* to the terminal. (To do: replace this later with display
* in web browser.)
*/
public void show()
{
// display all text posts
for (MessagePost message : messages) {
message.display();
System.out.println(); // empty line between posts
}
// display all photos
for (PhotoPost photo : photos) {
photo.display();
System.out.println(); // empty line between posts
}
}
}
This is by no means a complete application. It has no user interface yet (so it will not be usable
outside BlueJ), and the data entered is not stored to the file system or in a database. This means
that all data entered will be lost each time the application ends. There are no functions to sort
the displayed list of posts—for example, by date and time or by relevance. Currently, we will
always get messages first, in the order in which they were entered, followed by the photos.
Also, the functions for entering and editing data, as well as searching for data and displaying it,
are not flexible enough for what we would want from a real program.
However, this does not matter in our context. We can work on improving the application later.
The basic structure is there, and it works. This is enough for us to discuss design problems and
possible improvements.
Exercise 8.1 Open the project network-v1. It contains the classes exactly as we have
discussed them here. Create some MessagePost objects and some PhotoPost objects.
Create a NewsFeed object. Enter the posts into the news feed, and then display the feed's
contents.
Exercise 8.2 Try the following. Create a MessagePost object. Enter it into the news feed.
Display the news feed. You will see that the post has no associated comments. Add a com-
ment to the MessagePost object on the object bench (the one you entered into the news
feed). When you now list the news feed again, will the post listed there have a comment
attached? Try it. Explain the behavior you observe.
 
Search WWH ::




Custom Search