Compare commits

...

12 Commits
main ... ueb05

11 changed files with 244 additions and 212 deletions

BIN
SQ - Exercise 5.pdf Normal file

Binary file not shown.

2
ueb05_news_example/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/out/
/.idea/

View File

@ -1,5 +1,6 @@
package client;
import decorator.FrameDecorator;
import model.Article;
import model.Image;
import model.NewsCollection;
@ -7,71 +8,22 @@ import model.NewsCollection;
public class Client {
public static void main(String[] args) {
FrameDecorator framedCat1 = new FrameDecorator(new Image("Athletic cat contest", 800, 600, "zdf"));
FrameDecorator framedCat2 = new FrameDecorator(new Article("Missing Cat missed.", "mfg"));
FrameDecorator framedCat3 = new FrameDecorator(new Image("Missing cat", 1280, 720, "anonymous"));
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")));
masterCollection.addItem(new NewsCollection("Sports")
.addItem(new Article("Harder, Better, Faster, Stronger. Doping controls loosened","ups"))
.addItem(framedCat1))
.addItem(new NewsCollection("Local news")
.addItem(framedCat2)
.addItem(new Article("Corrupted local politician selling cats.", "ard"))
.addItem(framedCat3));
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));
}
/**
* 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();
System.out.print(masterCollection.getDetails());
}
}

View File

@ -0,0 +1,43 @@
package decorator;
import model.Item;
import java.util.Arrays;
public class FrameDecorator extends StringDecorator {
public FrameDecorator(Item item) {
super(item);
}
public String getOverview() {
return wrapWithFrame(wrappee.getOverview());
}
public String getDetails() {
return wrapWithFrame(wrappee.getDetails());
}
public String getTitle() {
return wrapWithFrame(wrappee.getTitle());
}
private String wrapWithFrame(String content) {
String[] lines = content.split("\n");
int maxLength = Arrays.stream(lines).map(String::length).reduce(Integer::max).orElse(0);
StringBuilder builder = new StringBuilder();
builder.append("-".repeat(maxLength + 2)).append("\n");
for (String line : lines) {
int spaces = maxLength - line.length();
int leadingSpaces = spaces / 2;
int trailingSpaces = spaces - leadingSpaces;
builder.append("|")
.append(" ".repeat(leadingSpaces))
.append(line)
.append(" ".repeat(trailingSpaces))
.append("|\n");
}
builder.append("-".repeat(maxLength + 2)).append("\n");
return builder.toString();
}
}

View File

@ -0,0 +1,23 @@
package decorator;
import model.Item;
public abstract class StringDecorator extends Item {
protected Item wrappee;
public StringDecorator(Item item) {
this.wrappee = item;
}
public String getOverview() {
return wrappee.getOverview();
}
public String getDetails() {
return wrappee.getDetails();
}
public String getTitle() {
return wrappee.getTitle();
}
}

View File

@ -1,10 +1,6 @@
package model;
public class Article {
private String title;
private String author;
private String content;
public class Article extends AuthoredItem {
// Fill in dummy content
{
content = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, "
@ -15,35 +11,16 @@ public class Article {
/**
* Creates an article with the given title and author.
* @param title
* @param author
* @param title title of article
* @param author author of article
*/
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;
public String getOverview(){
return "Article: " + getTitle() + ", Author: "
+ getAuthor();
}
}

View File

@ -0,0 +1,31 @@
package model;
public abstract class AuthoredItem extends Item{
protected String author;
protected String content;
public String getDetails(){
return "###" + getTitle() + "###"
+ "\n"
+ getContent()
+ "\n\n";
}
/**
* Get the author of the AuthoredItem.
*
* @return author
*/
public String getAuthor() {
return author;
}
/**
* Get the content of the AuthoredItem.
*
* @return content
*/
public String getContent() {
return content;
}
}

View File

@ -1,18 +1,22 @@
package model;
public class Image {
private String title;
private String author;
private int width;
private int height;
public class Image extends AuthoredItem {
private final int width;
private final int height;
// Fill in dummy content
{
content = " /\\_/\\ \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
*
* @param title title of image
* @param width width of image
* @param height height of image
* @param author author of image
*/
public Image(String title, int width, int height, String author) {
this.title = title;
@ -21,24 +25,18 @@ public class Image {
this.author = author;
}
/**
* Get the title of the image.
* @return title
*/
public String getTitle() {
return title;
public String getOverview() {
return "Image: " + getTitle()
+ ", Resolution: " + getWidth()
+ "x" + getHeight()
+ ", Author: " + getAuthor();
}
/**
* Get the author of the image.
* @return author
*/
public String getAuthor() {
return author;
}
//=================================
/**
* Get the width of the image.
*
* @return width
*/
public int getWidth() {
@ -47,20 +45,10 @@ public class Image {
/**
* 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,17 @@
package model;
public abstract class Item {
protected String title;
public abstract String getOverview();
public abstract String getDetails();
/**
* Get the title of the item.
*
* @return title
*/
public String getTitle() {
return title;
}
}

View File

@ -3,75 +3,63 @@ package model;
import java.util.LinkedList;
import java.util.List;
public class NewsCollection {
public class NewsCollection extends Item {
private String topic;
private List<Article> articles = new LinkedList<Article>();
private List<Image> images = new LinkedList<Image>();
private List<NewsCollection> collections = new LinkedList<NewsCollection>();
private final List<Item> items = new LinkedList<>();
public NewsCollection(String topic) {
this.topic = topic;
public NewsCollection(String title) {
this.title = title;
}
/**
* Get the topic of the collection.
* @return
* Gets overview information about all directly or transitively
* referenced items in the given collection.
*/
public String getTopic() {
return topic;
public String getOverview(){
StringBuilder builder = new StringBuilder();
builder
.append("###").append(getTitle()).append("###")
.append("\n");
for(Item item : getItems()){
builder
.append(item.getOverview())
.append("\n");
}
/**
* Get the list of articles stored directly in this collection.
* @return articles stored in this collection
*/
public List<Article> getArticles() {
return articles;
return builder.toString();
}
/**
* Get the list of images stored directly in this collection.
* @return images stored in this collection
* Gets the contents of all directly or transitively referenced items in
* the given collection.
*/
public List<Image> getImages() {
return images;
public String getDetails(){
StringBuilder builder = new StringBuilder();
builder
.append("###").append(getTitle()).append("###")
.append("\n");
for (Item item : getItems()) {
builder.append(item.getDetails());
}
return builder.toString();
}
//=================================
/**
* Get the list of items stored directly in this collection.
* @return items stored in this collection
*/
public List<Item> getItems() {
return items;
}
/**
* 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
* Store an item directly in this collection.
* @param item to add
* @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);
public NewsCollection addItem(Item item) {
items.add(item);
return this;
}
}

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>