import ueb05_news_example

This commit is contained in:
Daniel Langbein 2024-11-24 17:07:25 +01:00
parent 73a279f24a
commit 64c1d0e743
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
4 changed files with 269 additions and 0 deletions

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -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 > ^ < ";
}
}

View File

@ -0,0 +1,77 @@
package model;
import java.util.LinkedList;
import java.util.List;
public class NewsCollection {
private String topic;
private List<Article> articles = new LinkedList<Article>();
private List<Image> images = new LinkedList<Image>();
private List<NewsCollection> collections = new LinkedList<NewsCollection>();
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<Article> getArticles() {
return articles;
}
/**
* Get the list of images stored directly in this collection.
* @return images stored in this collection
*/
public List<Image> getImages() {
return images;
}
/**
* Get the list of collections stored directly in this collection.
* @return collections stored in this collection
*/
public List<NewsCollection> 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;
}
}