From 64c1d0e7433aa63d71b99b69ea8482dbfca66494 Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Sun, 24 Nov 2024 17:07:25 +0100 Subject: [PATCH] import ueb05_news_example --- ueb05_news_example/src/client/Client.java | 77 +++++++++++++++++++ ueb05_news_example/src/model/Article.java | 49 ++++++++++++ ueb05_news_example/src/model/Image.java | 66 ++++++++++++++++ .../src/model/NewsCollection.java | 77 +++++++++++++++++++ 4 files changed, 269 insertions(+) create mode 100644 ueb05_news_example/src/client/Client.java create mode 100644 ueb05_news_example/src/model/Article.java create mode 100644 ueb05_news_example/src/model/Image.java create mode 100644 ueb05_news_example/src/model/NewsCollection.java diff --git a/ueb05_news_example/src/client/Client.java b/ueb05_news_example/src/client/Client.java new file mode 100644 index 00000000..a1a55cd4 --- /dev/null +++ b/ueb05_news_example/src/client/Client.java @@ -0,0 +1,77 @@ +package client; + +import model.Article; +import model.Image; +import model.NewsCollection; + +public class Client { + + public static void main(String[] args) { + NewsCollection masterCollection = new NewsCollection("Daily news"); + masterCollection.addCollection(new NewsCollection("Sports") + .addArticle(new Article("Harder, Better, Faster, Stronger. Doping controls loosened","ups")) + .addImage(new Image("Athletic cat contest", 800, 600, "zdf"))) + .addCollection(new NewsCollection("Local news") + .addArticle(new Article("Missing Cat missed.", "mfg")) + .addArticle(new Article("Corrupted local politician selling cats.", "ard")) + .addImage(new Image("Missing cat", 1280, 720, "anonymous"))); + + System.out.println("\n===List===\n"); + System.out.print(getOverviewInformation(masterCollection)); + System.out.println("\n===Contents===\n"); + System.out.print(getDetailedInformation(masterCollection)); + } + + /** + * 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 new file mode 100644 index 00000000..c73b50ad --- /dev/null +++ b/ueb05_news_example/src/model/Article.java @@ -0,0 +1,49 @@ +package model; + +public class Article { + private String title; + private String author; + private String content; + + // Fill in dummy content + { + content = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, " + + "sed diam nonumy eirmod tempor invidunt ut labore et dolore " + + "magna aliquyam erat, sed diam voluptua. At vero eos et " + + "accusam et justo duo dolores et ea rebum."; + } + + /** + * Creates an article with the given title and author. + * @param title + * @param author + */ + public Article(String title, String author) { + this.title = title; + this.author = author; + } + + /** + * Get the title of the article. + * @return title + */ + public String getTitle() { + return title; + } + + /** + * Get the author of the article. + * @return author + */ + public String getAuthor() { + return author; + } + + /** + * Get the content of the article. + * @return + */ + public String getContent() { + return content; + } +} diff --git a/ueb05_news_example/src/model/Image.java b/ueb05_news_example/src/model/Image.java new file mode 100644 index 00000000..cef0c424 --- /dev/null +++ b/ueb05_news_example/src/model/Image.java @@ -0,0 +1,66 @@ +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; + } + + /** + * 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 > ^ < "; + } +} diff --git a/ueb05_news_example/src/model/NewsCollection.java b/ueb05_news_example/src/model/NewsCollection.java new file mode 100644 index 00000000..9fb0069b --- /dev/null +++ b/ueb05_news_example/src/model/NewsCollection.java @@ -0,0 +1,77 @@ +package model; + +import java.util.LinkedList; +import java.util.List; + +public class NewsCollection { + + private String topic; + private List
articles = new LinkedList
(); + private List images = new LinkedList(); + private List collections = new LinkedList(); + + public NewsCollection(String topic) { + this.topic = topic; + } + + /** + * Get the topic of the collection. + * @return + */ + public String getTopic() { + return topic; + } + /** + * Get the list of articles stored directly in this collection. + * @return articles stored in this collection + */ + public List
getArticles() { + return articles; + } + + /** + * Get the list of images stored directly in this collection. + * @return images stored in this collection + */ + public List getImages() { + return images; + } + + /** + * Get the list of collections stored directly in this collection. + * @return collections stored in this collection + */ + public List getCollections() { + return collections; + } + + /** + * Store an article directly in this collection. + * @param article + * @return this collection (enables method chaining) + */ + public NewsCollection addArticle(Article article) { + articles.add(article); + return this; + } + + /** + * Store an image directly in this collection. + * @param image + * @return this collection (enables method chaining) + */ + public NewsCollection addImage(Image image) { + images.add(image); + return this; + } + + /** + * Store another collection directly in this collection. + * @param collection + * @return this collection (enables method chaining) + */ + public NewsCollection addCollection(NewsCollection collection) { + collections.add(collection); + return this; + } +}