Compare commits

...

12 Commits

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

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; package model;
public class Article { public class Article extends AuthoredItem {
private String title;
private String author;
private String content;
// Fill in dummy content // Fill in dummy content
{ {
content = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, " 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. * Creates an article with the given title and author.
* @param title * @param title title of article
* @param author * @param author author of article
*/ */
public Article(String title, String author) { public Article(String title, String author) {
this.title = title; this.title = title;
this.author = author; this.author = author;
} }
/** public String getOverview(){
* Get the title of the article. return "Article: " + getTitle() + ", Author: "
* @return title + getAuthor();
*/
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,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,66 +1,54 @@
package model; package model;
public class Image { public class Image extends AuthoredItem {
private String title; private final int width;
private String author; private final int height;
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;
}
/** // Fill in dummy content
* Get the height of the image. {
* @return height content = " /\\_/\\ \n( o.o )\n > ^ < ";
*/ }
public int getHeight() {
return height; /**
} * Creates an image with the given title and author and a resolution
* defined by width and height.
/** *
* Renders the image as String and return the rendered image. * @param title title of image
* @return String representing the rendered image * @param width width of image
*/ * @param height height of image
public String renderToString() { * @param author author of image
/* */
* Dummy implementation public Image(String title, int width, int height, String author) {
*/ this.title = title;
return " /\\_/\\ \n( o.o )\n > ^ < "; this.width = width;
} this.height = height;
this.author = author;
}
public String getOverview() {
return "Image: " + getTitle()
+ ", Resolution: " + getWidth()
+ "x" + getHeight()
+ ", Author: " + getAuthor();
}
//=================================
/**
* 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;
}
} }

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