first commit
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
#BlueJ class context
|
||||
comment0.params=author\ text
|
||||
comment0.target=MessagePost(java.lang.String,\ java.lang.String)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ MessagePost.\n\ \n\ @param\ author\ \ \ \ The\ username\ of\ the\ author\ of\ this\ post.\n\ @param\ text\ \ \ \ \ \ The\ text\ of\ this\ post.\n
|
||||
comment1.params=
|
||||
comment1.target=void\ like()
|
||||
comment1.text=\n\ Record\ one\ more\ 'Like'\ indication\ from\ a\ user.\n
|
||||
comment2.params=
|
||||
comment2.target=void\ unlike()
|
||||
comment2.text=\n\ Record\ that\ a\ user\ has\ withdrawn\ his/her\ 'Like'\ vote.\n
|
||||
comment3.params=text
|
||||
comment3.target=void\ addComment(java.lang.String)
|
||||
comment3.text=\n\ Add\ a\ comment\ to\ this\ post.\n\ \n\ @param\ text\ \ The\ new\ comment\ to\ add.\n
|
||||
comment4.params=
|
||||
comment4.target=java.lang.String\ getText()
|
||||
comment4.text=\n\ Return\ the\ text\ of\ this\ post.\n\ \n\ @return\ The\ post's\ text.\n
|
||||
comment5.params=
|
||||
comment5.target=long\ getTimeStamp()
|
||||
comment5.text=\n\ Return\ the\ time\ of\ creation\ of\ this\ post.\n\ \n\ @return\ The\ post's\ creation\ time,\ as\ a\ system\ time\ value.\n
|
||||
comment6.params=
|
||||
comment6.target=void\ display()
|
||||
comment6.text=\n\ Display\ the\ details\ of\ this\ post.\n\ \n\ (Currently\:\ Print\ to\ the\ text\ terminal.\ This\ is\ simulating\ display\ \n\ in\ a\ web\ browser\ for\ now.)\n
|
||||
comment7.params=time
|
||||
comment7.target=java.lang.String\ timeString(long)
|
||||
comment7.text=\n\ Create\ a\ string\ describing\ a\ time\ point\ in\ the\ past\ in\ terms\ \n\ relative\ to\ current\ time,\ such\ as\ "30\ seconds\ ago"\ or\ "7\ minutes\ ago".\n\ Currently,\ only\ seconds\ and\ minutes\ are\ used\ for\ the\ string.\n\ \n\ @param\ time\ \ The\ time\ value\ to\ convert\ (in\ system\ milliseconds)\n\ @return\ \ \ \ \ \ A\ relative\ time\ string\ for\ the\ given\ time\n
|
||||
numComments=8
|
@@ -0,0 +1,131 @@
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* This class stores information about a post in a social network.
|
||||
* The main part of the post consists of a (possibly multi-line)
|
||||
* 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, multi-line 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<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Record one more 'Like' indication from a user.
|
||||
*/
|
||||
public void like()
|
||||
{
|
||||
likes++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Record that a user has withdrawn his/her 'Like' vote.
|
||||
*/
|
||||
public void unlike()
|
||||
{
|
||||
if (likes > 0) {
|
||||
likes--;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a comment to this post.
|
||||
*
|
||||
* @param text The new comment to add.
|
||||
*/
|
||||
public void addComment(String text)
|
||||
{
|
||||
comments.add(text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the text of this post.
|
||||
*
|
||||
* @return The post's text.
|
||||
*/
|
||||
public String getText()
|
||||
{
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the time of creation of this post.
|
||||
*
|
||||
* @return The post's creation time, as a system time value.
|
||||
*/
|
||||
public long getTimeStamp()
|
||||
{
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the details of this post.
|
||||
*
|
||||
* (Currently: Print to the text terminal. This is simulating display
|
||||
* in a web browser for now.)
|
||||
*/
|
||||
public void display()
|
||||
{
|
||||
System.out.println(username);
|
||||
System.out.println(message);
|
||||
System.out.print(timeString(timestamp));
|
||||
|
||||
if(likes > 0) {
|
||||
System.out.println(" - " + likes + " people like this.");
|
||||
}
|
||||
else {
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
if(comments.isEmpty()) {
|
||||
System.out.println(" No comments.");
|
||||
}
|
||||
else {
|
||||
System.out.println(" " + comments.size() + " comment(s). Click here to view.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a string describing a time point in the past in terms
|
||||
* relative to current time, such as "30 seconds ago" or "7 minutes ago".
|
||||
* Currently, only seconds and minutes are used for the string.
|
||||
*
|
||||
* @param time The time value to convert (in system milliseconds)
|
||||
* @return A relative time string for the given time
|
||||
*/
|
||||
|
||||
private String timeString(long time)
|
||||
{
|
||||
long current = System.currentTimeMillis();
|
||||
long pastMillis = current - time; // time passed in milliseconds
|
||||
long seconds = pastMillis/1000;
|
||||
long minutes = seconds/60;
|
||||
if(minutes > 0) {
|
||||
return minutes + " minutes ago";
|
||||
}
|
||||
else {
|
||||
return seconds + " seconds ago";
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=NewsFeed()
|
||||
comment0.text=\n\ Construct\ an\ empty\ news\ feed.\n
|
||||
comment1.params=message
|
||||
comment1.target=void\ addMessagePost(MessagePost)
|
||||
comment1.text=\n\ Add\ a\ text\ post\ to\ the\ news\ feed.\n\ \n\ @param\ text\ \ The\ text\ post\ to\ be\ added.\n
|
||||
comment2.params=photo
|
||||
comment2.target=void\ addPhotoPost(PhotoPost)
|
||||
comment2.text=\n\ Add\ a\ photo\ post\ to\ the\ news\ feed.\n\ \n\ @param\ photo\ \ The\ photo\ post\ to\ be\ added.\n
|
||||
comment3.params=
|
||||
comment3.target=void\ show()
|
||||
comment3.text=\n\ Show\ the\ news\ feed.\ Currently\:\ print\ the\ news\ feed\ details\ to\ the\n\ terminal.\ (To\ do\:\ replace\ this\ later\ with\ display\ in\ web\ browser.)\n
|
||||
numComments=4
|
@@ -0,0 +1,68 @@
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* The NewsFeed class stores news posts for the news feed in a social network
|
||||
* application.
|
||||
*
|
||||
* Display of the posts is currently simulated by printing the details to the
|
||||
* terminal. (Later, this should display in a browser.)
|
||||
*
|
||||
* This version does not save the data to disk, and it does not provide any
|
||||
* search or ordering functions.
|
||||
*
|
||||
* @author Michael Kölling and David J. Barnes
|
||||
* @version 0.1
|
||||
*/
|
||||
public class NewsFeed
|
||||
{
|
||||
private ArrayList<MessagePost> messages;
|
||||
private ArrayList<PhotoPost> photos;
|
||||
|
||||
/**
|
||||
* Construct an empty news feed.
|
||||
*/
|
||||
public NewsFeed()
|
||||
{
|
||||
messages = new ArrayList<>();
|
||||
photos = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a text post to the news feed.
|
||||
*
|
||||
* @param text The text post to be added.
|
||||
*/
|
||||
public void addMessagePost(MessagePost message)
|
||||
{
|
||||
messages.add(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a photo post to the news feed.
|
||||
*
|
||||
* @param photo The photo post to be added.
|
||||
*/
|
||||
public void addPhotoPost(PhotoPost photo)
|
||||
{
|
||||
photos.add(photo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
#BlueJ class context
|
||||
comment0.params=author\ filename\ caption
|
||||
comment0.target=PhotoPost(java.lang.String,\ java.lang.String,\ java.lang.String)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ PhotoPost.\n\ \n\ @param\ author\ \ \ \ The\ username\ of\ the\ author\ of\ this\ post.\n\ @param\ filename\ \ The\ filename\ of\ the\ image\ in\ this\ post.\n\ @param\ caption\ \ \ A\ caption\ for\ the\ image.\n
|
||||
comment1.params=
|
||||
comment1.target=void\ like()
|
||||
comment1.text=\n\ Record\ one\ more\ 'Like'\ indication\ from\ a\ user.\n
|
||||
comment2.params=
|
||||
comment2.target=void\ unlike()
|
||||
comment2.text=\n\ Record\ that\ a\ user\ has\ withdrawn\ his/her\ 'Like'\ vote.\n
|
||||
comment3.params=text
|
||||
comment3.target=void\ addComment(java.lang.String)
|
||||
comment3.text=\n\ Add\ a\ comment\ to\ this\ post.\n\ \n\ @param\ text\ \ The\ new\ comment\ to\ add.\n
|
||||
comment4.params=
|
||||
comment4.target=java.lang.String\ getImageFile()
|
||||
comment4.text=\n\ Return\ the\ file\ name\ of\ the\ image\ in\ this\ post.\n\ \n\ @return\ The\ post's\ image\ file\ name.\n
|
||||
comment5.params=
|
||||
comment5.target=java.lang.String\ getCaption()
|
||||
comment5.text=\n\ Return\ the\ caption\ of\ the\ image\ of\ this\ post.\n\ \n\ @return\ The\ image's\ caption.\n
|
||||
comment6.params=
|
||||
comment6.target=long\ getTimeStamp()
|
||||
comment6.text=\n\ Return\ the\ time\ of\ creation\ of\ this\ post.\n\ \n\ @return\ The\ post's\ creation\ time,\ as\ a\ system\ time\ value.\n
|
||||
comment7.params=
|
||||
comment7.target=void\ display()
|
||||
comment7.text=\n\ Display\ the\ details\ of\ this\ post.\n\ \n\ (Currently\:\ Print\ to\ the\ text\ terminal.\ This\ is\ simulating\ display\ \n\ in\ a\ web\ browser\ for\ now.)\n
|
||||
comment8.params=time
|
||||
comment8.target=java.lang.String\ timeString(long)
|
||||
comment8.text=\n\ Create\ a\ string\ describing\ a\ time\ point\ in\ the\ past\ in\ terms\ \n\ relative\ to\ current\ time,\ such\ as\ "30\ seconds\ ago"\ or\ "7\ minutes\ ago".\n\ Currently,\ only\ seconds\ and\ minutes\ are\ used\ for\ the\ string.\n\ \n\ @param\ time\ \ The\ time\ value\ to\ convert\ (in\ system\ milliseconds)\n\ @return\ \ \ \ \ \ A\ relative\ time\ string\ for\ the\ given\ time\n
|
||||
numComments=9
|
@@ -0,0 +1,145 @@
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* This class stores information about a post in a social network.
|
||||
* The main part of the post consists of a photo and a caption.
|
||||
* Other data, such as author and time, are also stored.
|
||||
*
|
||||
* @author Michael Kölling and David J. Barnes
|
||||
* @version 0.1
|
||||
*/
|
||||
public class PhotoPost
|
||||
{
|
||||
private String username; // username of the post's author
|
||||
private String filename; // the name of the image file
|
||||
private String caption; // a one line image caption
|
||||
private long timestamp;
|
||||
private int likes;
|
||||
private ArrayList<String> comments;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class PhotoPost.
|
||||
*
|
||||
* @param author The username of the author of this post.
|
||||
* @param filename The filename of the image in this post.
|
||||
* @param caption A caption for the image.
|
||||
*/
|
||||
public PhotoPost(String author, String filename, String caption)
|
||||
{
|
||||
username = author;
|
||||
this.filename = filename;
|
||||
this.caption = caption;
|
||||
timestamp = System.currentTimeMillis();
|
||||
likes = 0;
|
||||
comments = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Record one more 'Like' indication from a user.
|
||||
*/
|
||||
public void like()
|
||||
{
|
||||
likes++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Record that a user has withdrawn his/her 'Like' vote.
|
||||
*/
|
||||
public void unlike()
|
||||
{
|
||||
if (likes > 0) {
|
||||
likes--;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a comment to this post.
|
||||
*
|
||||
* @param text The new comment to add.
|
||||
*/
|
||||
public void addComment(String text)
|
||||
{
|
||||
comments.add(text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the file name of the image in this post.
|
||||
*
|
||||
* @return The post's image file name.
|
||||
*/
|
||||
public String getImageFile()
|
||||
{
|
||||
return filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the caption of the image of this post.
|
||||
*
|
||||
* @return The image's caption.
|
||||
*/
|
||||
public String getCaption()
|
||||
{
|
||||
return caption;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the time of creation of this post.
|
||||
*
|
||||
* @return The post's creation time, as a system time value.
|
||||
*/
|
||||
public long getTimeStamp()
|
||||
{
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the details of this post.
|
||||
*
|
||||
* (Currently: Print to the text terminal. This is simulating display
|
||||
* in a web browser for now.)
|
||||
*/
|
||||
public void display()
|
||||
{
|
||||
System.out.println(username);
|
||||
System.out.println(" [" + filename + "]");
|
||||
System.out.println(" " + caption);
|
||||
System.out.print(timeString(timestamp));
|
||||
|
||||
if(likes > 0) {
|
||||
System.out.println(" - " + likes + " people like this.");
|
||||
}
|
||||
else {
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
if(comments.isEmpty()) {
|
||||
System.out.println(" No comments.");
|
||||
}
|
||||
else {
|
||||
System.out.println(" " + comments.size() + " comment(s). Click here to view.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a string describing a time point in the past in terms
|
||||
* relative to current time, such as "30 seconds ago" or "7 minutes ago".
|
||||
* Currently, only seconds and minutes are used for the string.
|
||||
*
|
||||
* @param time The time value to convert (in system milliseconds)
|
||||
* @return A relative time string for the given time
|
||||
*/
|
||||
|
||||
private String timeString(long time)
|
||||
{
|
||||
long current = System.currentTimeMillis();
|
||||
long pastMillis = current - time; // time passed in milliseconds
|
||||
long seconds = pastMillis/1000;
|
||||
long minutes = seconds/60;
|
||||
if(minutes > 0) {
|
||||
return minutes + " minutes ago";
|
||||
}
|
||||
else {
|
||||
return seconds + " seconds ago";
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
Project: network-v1
|
||||
Authors: Michael Kölling and David J. Barnes
|
||||
|
||||
This project is part of the material for chapter 8 of the book
|
||||
|
||||
Objects First with Java - A Practical Introduction using BlueJ
|
||||
Sixth edition
|
||||
David J. Barnes and Michael Kölling
|
||||
Pearson Education, 2016
|
||||
|
||||
It implements a first version of a news feed for a social network.
|
@@ -0,0 +1,61 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=NewsFeed
|
||||
dependency1.to=PhotoPost
|
||||
dependency1.type=UsesDependency
|
||||
dependency2.from=NewsFeed
|
||||
dependency2.to=MessagePost
|
||||
dependency2.type=UsesDependency
|
||||
objectbench.height=76
|
||||
objectbench.width=798
|
||||
package.editor.height=437
|
||||
package.editor.width=690
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=2
|
||||
package.numTargets=3
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.editor.height=592
|
||||
readme.editor.width=806
|
||||
readme.editor.x=53
|
||||
readme.editor.y=23
|
||||
target1.editor.height=792
|
||||
target1.editor.width=927
|
||||
target1.editor.x=81
|
||||
target1.editor.y=23
|
||||
target1.height=60
|
||||
target1.name=NewsFeed
|
||||
target1.naviview.expanded=true
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=100
|
||||
target1.x=150
|
||||
target1.y=100
|
||||
target2.editor.height=753
|
||||
target2.editor.width=940
|
||||
target2.editor.x=59
|
||||
target2.editor.y=22
|
||||
target2.height=60
|
||||
target2.name=PhotoPost
|
||||
target2.naviview.expanded=true
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=110
|
||||
target2.x=370
|
||||
target2.y=260
|
||||
target3.editor.height=742
|
||||
target3.editor.width=944
|
||||
target3.editor.x=205
|
||||
target3.editor.y=24
|
||||
target3.height=60
|
||||
target3.name=MessagePost
|
||||
target3.naviview.expanded=true
|
||||
target3.showInterface=false
|
||||
target3.type=ClassTarget
|
||||
target3.typeParameters=
|
||||
target3.width=110
|
||||
target3.x=370
|
||||
target3.y=190
|
Reference in New Issue
Block a user