From c41072785c5abeb8342775d956ddabc2b50a470c Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Sat, 7 Dec 2024 10:30:42 +0100 Subject: [PATCH] import news example --- ueb07_news_example/src/client/Client.java | 31 ++++++++ ueb07_news_example/src/model/Article.java | 54 +++++++++++++ ueb07_news_example/src/model/Image.java | 79 +++++++++++++++++++ .../src/model/NewsCollection.java | 58 ++++++++++++++ ueb07_news_example/src/model/NewsItem.java | 32 ++++++++ 5 files changed, 254 insertions(+) create mode 100644 ueb07_news_example/src/client/Client.java create mode 100644 ueb07_news_example/src/model/Article.java create mode 100644 ueb07_news_example/src/model/Image.java create mode 100644 ueb07_news_example/src/model/NewsCollection.java create mode 100644 ueb07_news_example/src/model/NewsItem.java diff --git a/ueb07_news_example/src/client/Client.java b/ueb07_news_example/src/client/Client.java new file mode 100644 index 00000000..94f2afd4 --- /dev/null +++ b/ueb07_news_example/src/client/Client.java @@ -0,0 +1,31 @@ +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"); + + Article changedArticle = new Article("Harder, Better, Faster, Stronger. Doping controls loosened","ups"); + + masterCollection.addItem(new NewsCollection("Sports") + .addItem(changedArticle) + .addItem(new Image("Athletic cat contest", 800, 600, "zdf"))) + .addItem(new NewsCollection("Local news") + .addItem(new Article("Missing Cat missed.", "mfg")) + .addItem(new Article("Corrupted local politician selling cats.", "ard")) + .addItem(new Image("Missing cat", 1280, 720, "anonymous"))); + + System.out.println("\n===List===\n"); + System.out.print(masterCollection.getOverviewInformation()); + System.out.println("\n===Contents===\n"); + System.out.print(masterCollection.getDetailedInformation()); + System.out.println("\n===After change===\n"); + + changedArticle.setTitle("Harder, Better, Faster, Stronger? Doping controls loosened"); + masterCollection.addItem(new Image("A tasty cat", 640, 480, "alf")); + } +} diff --git a/ueb07_news_example/src/model/Article.java b/ueb07_news_example/src/model/Article.java new file mode 100644 index 00000000..0d821a38 --- /dev/null +++ b/ueb07_news_example/src/model/Article.java @@ -0,0 +1,54 @@ +package model; + +public class Article extends NewsItem { + 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."; + } + + /** + * 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 author of the article. + * @return author + */ + public String getAuthor() { + return author; + } + + /** + * Get the content of the article. + * @return + */ + public String getContent() { + return content; + } + + @Override + public String getOverviewInformation() { + return "Article: " + this.getTitle() + ", Author: " + this.getAuthor(); + } + + @Override + public String getDetailedInformation() { + StringBuilder builder = new StringBuilder(); + builder.append("###" + this.getTitle() + "###"); + builder.append("\n"); + builder.append(this.getContent()); + builder.append("\n"); + return builder.toString(); + } +} diff --git a/ueb07_news_example/src/model/Image.java b/ueb07_news_example/src/model/Image.java new file mode 100644 index 00000000..00307bad --- /dev/null +++ b/ueb07_news_example/src/model/Image.java @@ -0,0 +1,79 @@ +package model; + +public class Image extends NewsItem { + 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 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 > ^ < "; + } + + /** + * Print a list showing information about all directly or transitively + * referenced items in the given collection. + */ + @Override + public String getOverviewInformation() { + return "Image: " + this.getTitle() + + ", Resolution: " + this.getWidth() + + "x" + this.getHeight() + + ", Author: " + this.getAuthor(); + } + + @Override + public String getDetailedInformation() { + StringBuilder builder = new StringBuilder(); + builder.append("###" + this.getTitle() + "###"); + builder.append("\n"); + builder.append(renderToString()); + builder.append("\n"); + return builder.toString(); + } +} diff --git a/ueb07_news_example/src/model/NewsCollection.java b/ueb07_news_example/src/model/NewsCollection.java new file mode 100644 index 00000000..eab02042 --- /dev/null +++ b/ueb07_news_example/src/model/NewsCollection.java @@ -0,0 +1,58 @@ +package model; + +import java.util.LinkedList; +import java.util.List; + +public class NewsCollection extends NewsItem { + + private List newsItems = new LinkedList(); + + public NewsCollection(String topic) { + this.title = topic; + } + + /** + * Get the list of items stored in this collection. + * @return items stored in this collection + */ + public List getNewsItems() { + return newsItems; + } + + /** + * Store another item to this collection. + * @param item + * @return this collection (enables method chaining) + */ + public NewsCollection addItem(NewsItem item) { + newsItems.add(item); + return this; + } + + /** + * Print a list showing information about all directly or transitively + * referenced items in the given collection. + */ + public String getOverviewInformation() { + StringBuilder builder = new StringBuilder(); + builder.append("###" + this.getTitle() + "###"); + builder.append("\n"); + for (NewsItem item : this.getNewsItems()) { + builder.append(item.getOverviewInformation()); + builder.append("\n"); + } + return builder.toString(); + } + + @Override + public String getDetailedInformation() { + StringBuilder builder = new StringBuilder(); + builder.append("###" + this.getTitle() + "###"); + builder.append("\n"); + for (NewsItem item : this.getNewsItems()) { + builder.append(item.getDetailedInformation()); + builder.append("\n"); + } + return builder.toString(); + } +} diff --git a/ueb07_news_example/src/model/NewsItem.java b/ueb07_news_example/src/model/NewsItem.java new file mode 100644 index 00000000..e7b7a86a --- /dev/null +++ b/ueb07_news_example/src/model/NewsItem.java @@ -0,0 +1,32 @@ +package model; + +public abstract class NewsItem { + + protected String title; + + public NewsItem() { + super(); + } + + /** + * Get the topic of the collection. + * @return + */ + public String getTitle() { + return title; + } + + public void setTitle(String newTitle) { + title = newTitle; + } + + /** + * Get overview information about the news item. + */ + public abstract String getOverviewInformation(); + + /** + * Get the contents of the news item. + */ + public abstract String getDetailedInformation(); +} \ No newline at end of file