Java Reference
In-Depth Information
In practice, to implement the full network application, we would have more classes to handle
things such as saving the data to a database and providing a user interface, most likely through
a web browser. These are not very relevant to the present discussion, so we shall skip describing
those for now and concentrate on a more detailed discussion of the core classes mentioned here.
8.1.2
Network source code
So far, the design of the three current classes ( MessagePost , PhotoPost , and NewsFeed ) has
been very straightforward. Translating these ideas into Java code is equally easy. Code 8.1 shows
the source code of the MessagePost class. It defines the appropriate fields, sets in its constructor all
the data items that are not expected to change over time, and provides accessor and mutator methods
where appropriate. It also implements the display method to show the post in the text terminal.
Code 8.1
Source code of the
MessagePost class
import java.util.ArrayList;
/**
* This class stores information about a post on a social network.
* The main part of the post consists of a (possibly multiline)
* text message. Other data such as author and time are also stored.
*
* @author Michael Kölling and David J. Barnes
* @version 0.1
*/
public class MessagePost
{
private String username; // username of the post's author
private String message; // an arbitrarily long, multiline message
private long timestamp;
private int likes;
private ArrayList<String> comments;
/**
* Constructor for objects of class MessagePost.
*
* @param author The username of the author of this post.
* @param text The text of this post.
*/
public MessagePost(String author, String text)
{
username = author;
message = text;
timestamp = System.currentTimeMillis();
likes = 0;
comments = new ArrayList<String>();
}
 
Search WWH ::




Custom Search