first commit
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
#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=java.lang.String\ getText()
|
||||
comment1.text=\n\ Return\ the\ text\ of\ this\ post.\n\ \n\ @return\ The\ post's\ message\ text.\n
|
||||
comment2.params=
|
||||
comment2.target=void\ display()
|
||||
comment2.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
|
||||
numComments=3
|
@@ -0,0 +1,47 @@
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* This class stores information about a post in a social network news feed.
|
||||
* 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.3
|
||||
*/
|
||||
public class MessagePost extends Post
|
||||
{
|
||||
private String message; // an arbitrarily long, multi-line message
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
super(author);
|
||||
message = text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the text of this post.
|
||||
*
|
||||
* @return The post's message text.
|
||||
*/
|
||||
public String getText()
|
||||
{
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(message);
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=NewsFeed()
|
||||
comment0.text=\n\ Construct\ an\ empty\ news\ feed.\n
|
||||
comment1.params=post
|
||||
comment1.target=void\ addPost(Post)
|
||||
comment1.text=\n\ Add\ a\ post\ to\ the\ news\ feed.\n\ \n\ @param\ post\ \ The\ post\ to\ be\ added.\n
|
||||
comment2.params=
|
||||
comment2.target=void\ show()
|
||||
comment2.text=\n\ Show\ the\ news\ feed.\ Currently\:\ print\ the\ news\ feed\ details\n\ to\ the\ terminal.\ (To\ do\:\ replace\ this\ later\ with\ display\n\ in\ web\ browser.)\n
|
||||
numComments=3
|
@@ -0,0 +1,49 @@
|
||||
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.3
|
||||
*/
|
||||
public class NewsFeed
|
||||
{
|
||||
private ArrayList<Post> posts;
|
||||
|
||||
/**
|
||||
* Construct an empty news feed.
|
||||
*/
|
||||
public NewsFeed()
|
||||
{
|
||||
posts = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a post to the news feed.
|
||||
*
|
||||
* @param post The post to be added.
|
||||
*/
|
||||
public void addPost(Post post)
|
||||
{
|
||||
posts.add(post);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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()
|
||||
{
|
||||
for(Post post : posts) {
|
||||
post.display();
|
||||
System.out.println(); // empty line between posts
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
#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=java.lang.String\ getImageFile()
|
||||
comment1.text=\n\ Return\ the\ file\ name\ of\ the\ image\ in\ this\ post.\n\ \n\ @return\ The\ post's\ image\ file\ name.\n
|
||||
comment2.params=
|
||||
comment2.target=java.lang.String\ getCaption()
|
||||
comment2.text=\n\ Return\ the\ caption\ of\ the\ image\ of\ this\ post.\n\ \n\ @return\ The\ image's\ caption.\n
|
||||
comment3.params=
|
||||
comment3.target=void\ display()
|
||||
comment3.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
|
||||
numComments=4
|
@@ -0,0 +1,61 @@
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* This class stores information about a post in a social network news feed.
|
||||
* 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.3
|
||||
*/
|
||||
public class PhotoPost extends Post
|
||||
{
|
||||
private String filename; // the name of the image file
|
||||
private String caption; // a one line image caption
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
super(author);
|
||||
this.filename = filename;
|
||||
this.caption = caption;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(" [" + filename + "]");
|
||||
System.out.println(" " + caption);
|
||||
}
|
||||
}
|
@@ -0,0 +1,23 @@
|
||||
#BlueJ class context
|
||||
comment0.params=author
|
||||
comment0.target=Post(java.lang.String)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Post.\n\ \n\ @param\ author\ \ \ \ The\ username\ of\ the\ author\ 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=long\ getTimeStamp()
|
||||
comment4.text=\n\ Return\ the\ time\ of\ creation\ of\ this\ post.\n\ \n\ @return\ The\ post's\ creation\ time,\ as\ a\ system\ time\ value.\n
|
||||
comment5.params=
|
||||
comment5.target=void\ display()
|
||||
comment5.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
|
||||
comment6.params=time
|
||||
comment6.target=java.lang.String\ timeString(long)
|
||||
comment6.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=7
|
117
Semester 1/Programming 1/Java/examples/projects/chapter11/network-v3/Post.java
Executable file
117
Semester 1/Programming 1/Java/examples/projects/chapter11/network-v3/Post.java
Executable file
@@ -0,0 +1,117 @@
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* This class stores information about a news feed post in a
|
||||
* social network. Posts can be stored and displayed. This class
|
||||
* serves as a superclass for more specific post types.
|
||||
*
|
||||
* @author Michael Kölling and David J. Barnes
|
||||
* @version 0.3
|
||||
*/
|
||||
public class Post
|
||||
{
|
||||
private String username; // username of the post's author
|
||||
private long timestamp;
|
||||
private int likes;
|
||||
private ArrayList<String> comments;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class Post.
|
||||
*
|
||||
* @param author The username of the author of this post.
|
||||
*/
|
||||
public Post(String author)
|
||||
{
|
||||
username = author;
|
||||
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 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.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-v3
|
||||
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
|
||||
|
||||
This is a third version of a news feed for a social network.
|
@@ -0,0 +1,71 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=NewsFeed
|
||||
dependency1.to=Post
|
||||
dependency1.type=UsesDependency
|
||||
objectbench.height=76
|
||||
objectbench.width=829
|
||||
package.editor.height=462
|
||||
package.editor.width=721
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=1
|
||||
package.numTargets=4
|
||||
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=730
|
||||
target1.editor.width=876
|
||||
target1.editor.x=88
|
||||
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=90
|
||||
target2.editor.height=749
|
||||
target2.editor.width=944
|
||||
target2.editor.x=53
|
||||
target2.editor.y=23
|
||||
target2.height=60
|
||||
target2.name=Post
|
||||
target2.naviview.expanded=true
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=110
|
||||
target2.x=350
|
||||
target2.y=170
|
||||
target3.editor.height=729
|
||||
target3.editor.width=867
|
||||
target3.editor.x=98
|
||||
target3.editor.y=70
|
||||
target3.height=60
|
||||
target3.name=PhotoPost
|
||||
target3.naviview.expanded=true
|
||||
target3.showInterface=false
|
||||
target3.type=ClassTarget
|
||||
target3.typeParameters=
|
||||
target3.width=110
|
||||
target3.x=440
|
||||
target3.y=290
|
||||
target4.editor.height=737
|
||||
target4.editor.width=980
|
||||
target4.editor.x=53
|
||||
target4.editor.y=66
|
||||
target4.height=60
|
||||
target4.name=MessagePost
|
||||
target4.naviview.expanded=true
|
||||
target4.showInterface=false
|
||||
target4.type=ClassTarget
|
||||
target4.typeParameters=
|
||||
target4.width=110
|
||||
target4.x=280
|
||||
target4.y=290
|
Reference in New Issue
Block a user