diff --git a/ueb05_news_example/src/client/Client.java b/ueb05_news_example/src/client/Client.java index a1a55cd4..baa626c6 100644 --- a/ueb05_news_example/src/client/Client.java +++ b/ueb05_news_example/src/client/Client.java @@ -17,61 +17,8 @@ public class Client { .addImage(new Image("Missing cat", 1280, 720, "anonymous"))); System.out.println("\n===List===\n"); - System.out.print(getOverviewInformation(masterCollection)); + System.out.print(masterCollection.getOverview()); System.out.println("\n===Contents===\n"); - System.out.print(getDetailedInformation(masterCollection)); + System.out.print(masterCollection.getDetails()); } - - /** - * Gets overview information about all directly or transitively - * referenced items in the given collection. - */ - public static String getOverviewInformation(NewsCollection rootCollection) { - StringBuilder builder = new StringBuilder(); - builder.append("###" + rootCollection.getTopic() + "###"); - builder.append("\n"); - for (Article article : rootCollection.getArticles()) { - builder.append("Article: " + article.getTitle() + ", Author: " - + article.getAuthor()); - builder.append("\n"); - } - for (Image image : rootCollection.getImages()) { - builder.append("Image: " + image.getTitle() - + ", Resolution: " + image.getWidth() - + "x" + image.getHeight() - + ", Author: " + image.getAuthor()); - builder.append("\n"); - } - for (NewsCollection collection : rootCollection.getCollections()) { - builder.append(getOverviewInformation(collection)); - builder.append("\n"); - } - return builder.toString(); - } - - /** - * Gets the contents of all directly or transitively referenced items in - * the given collection. - */ - public static String getDetailedInformation(NewsCollection rootCollection) { - StringBuilder builder = new StringBuilder(); - builder.append("###" + rootCollection.getTopic() + "###"); - builder.append("\n"); - for (Article article : rootCollection.getArticles()) { - builder.append("###" + article.getTitle() + "###"); - builder.append("\n"); - builder.append(article.getContent()); - builder.append("\n\n"); - } - for (Image image : rootCollection.getImages()) { - builder.append("###" + image.getTitle() + "###"); - builder.append("\n"); - builder.append(image.renderToString()); - builder.append("\n\n"); - } - for (NewsCollection collection : rootCollection.getCollections()) { - builder.append(getDetailedInformation(collection)); - } - return builder.toString(); - } } diff --git a/ueb05_news_example/src/model/Article.java b/ueb05_news_example/src/model/Article.java index c73b50ad..bb0b8fc2 100644 --- a/ueb05_news_example/src/model/Article.java +++ b/ueb05_news_example/src/model/Article.java @@ -1,6 +1,6 @@ package model; -public class Article { +public class Article extends Item { private String title; private String author; private String content; @@ -22,7 +22,21 @@ public class Article { this.title = title; this.author = author; } - + + public String getOverview(){ + return "Article: " + getTitle() + ", Author: " + + getAuthor(); + } + + public String getDetails(){ + return "###" + getTitle() + "###" + + "\n" + + getContent() + + "\n\n"; + } + + //================================= + /** * Get the title of the article. * @return title diff --git a/ueb05_news_example/src/model/Image.java b/ueb05_news_example/src/model/Image.java index cef0c424..6c3b671f 100644 --- a/ueb05_news_example/src/model/Image.java +++ b/ueb05_news_example/src/model/Image.java @@ -1,66 +1,88 @@ package model; -public class Image { - private String title; - private String author; - private int width; - private int height; - - /** - * Creates an image with the given title and author and a resolution - * defined by width and height. - * @param title - * @param width - * @param height - * @param author - */ - public Image(String title, int width, int height, String author) { - this.title = title; - this.width = width; - this.height = height; - this.author = author; - } - - /** - * Get the title of the image. - * @return title - */ - public String getTitle() { - return title; - } - - /** - * Get the author of the image. - * @return author - */ - public String getAuthor() { - return author; - } - - /** - * Get the width of the image. - * @return width - */ - public int getWidth() { - return width; - } +public class Image extends Item { + private String title; + private String author; + private int width; + private int height; - /** - * Get the height of the image. - * @return height - */ - public int getHeight() { - return height; - } - - /** - * Renders the image as String and return the rendered image. - * @return String representing the rendered image - */ - public String renderToString() { - /* - * Dummy implementation - */ - return " /\\_/\\ \n( o.o )\n > ^ < "; - } + /** + * Creates an image with the given title and author and a resolution + * defined by width and height. + * + * @param title + * @param width + * @param height + * @param author + */ + public Image(String title, int width, int height, String author) { + this.title = title; + this.width = width; + this.height = height; + this.author = author; + } + + public String getOverview() { + return "Image: " + getTitle() + + ", Resolution: " + getWidth() + + "x" + getHeight() + + ", Author: " + getAuthor(); + } + + public String getDetails(){ + return "###" + getTitle() + "###" + + "\n" + + getContent() + + "\n\n"; + } + + //================================= + + /** + * Get the title of the image. + * + * @return title + */ + public String getTitle() { + return title; + } + + /** + * Get the author of the image. + * + * @return author + */ + public String getAuthor() { + return author; + } + + /** + * Get the width of the image. + * + * @return width + */ + public int getWidth() { + return width; + } + + /** + * Get the height of the image. + * + * @return height + */ + public int getHeight() { + return height; + } + + /** + * Renders the image as String and return the rendered image. + * + * @return String representing the rendered image + */ + public String getContent() { + /* + * Dummy implementation + */ + return " /\\_/\\ \n( o.o )\n > ^ < "; + } } diff --git a/ueb05_news_example/src/model/Item.java b/ueb05_news_example/src/model/Item.java new file mode 100644 index 00000000..e4f63f48 --- /dev/null +++ b/ueb05_news_example/src/model/Item.java @@ -0,0 +1,6 @@ +package model; + +public abstract class Item { + public abstract String getOverview(); + public abstract String getDetails(); +} diff --git a/ueb05_news_example/src/model/NewsCollection.java b/ueb05_news_example/src/model/NewsCollection.java index 9fb0069b..fab07637 100644 --- a/ueb05_news_example/src/model/NewsCollection.java +++ b/ueb05_news_example/src/model/NewsCollection.java @@ -3,7 +3,7 @@ package model; import java.util.LinkedList; import java.util.List; -public class NewsCollection { +public class NewsCollection extends Item { private String topic; private List
articles = new LinkedList
(); @@ -13,7 +13,52 @@ public class NewsCollection { public NewsCollection(String topic) { this.topic = topic; } - + + /** + * Gets overview information about all directly or transitively + * referenced items in the given collection. + */ + public String getOverview(){ + StringBuilder builder = new StringBuilder(); + builder.append("###" + getTopic() + "###"); + builder.append("\n"); + for (Article article : getArticles()) { + builder.append(article.getOverview()); + builder.append("\n"); + } + for (Image image : getImages()) { + builder.append(image.getOverview()); + builder.append("\n"); + } + for (NewsCollection collection : getCollections()) { + builder.append(collection.getOverview()); + builder.append("\n"); + } + return builder.toString(); + } + + /** + * Gets the contents of all directly or transitively referenced items in + * the given collection. + */ + public String getDetails(){ + StringBuilder builder = new StringBuilder(); + builder.append("###" + getTopic() + "###"); + builder.append("\n"); + for (Article article : getArticles()) { + builder.append(article.getDetails()); + } + for (Image image : getImages()) { + builder.append(image.getDetails()); + } + for (NewsCollection collection : getCollections()) { + builder.append(collection.getDetails()); + } + return builder.toString(); + } + + //================================= + /** * Get the topic of the collection. * @return