Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
e3e6da257a | ||
![]() |
f7e214eb7e | ||
![]() |
70c90c1a21 | ||
![]() |
ebab952bd9 | ||
728dd877bd | |||
cfeaba3cab | |||
3f30173a5b | |||
67e5565b54 | |||
db12a40520 | |||
261f9bb800 | |||
3ea5ab1e79 | |||
05650caa2b |
BIN
SQ - Exercise 5.pdf
Normal file
BIN
SQ - Exercise 5.pdf
Normal file
Binary file not shown.
2
ueb05_news_example/.gitignore
vendored
Normal file
2
ueb05_news_example/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/out/
|
||||
/.idea/
|
@ -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));
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
43
ueb05_news_example/src/decorator/FrameDecorator.java
Normal file
43
ueb05_news_example/src/decorator/FrameDecorator.java
Normal 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();
|
||||
}
|
||||
}
|
23
ueb05_news_example/src/decorator/StringDecorator.java
Normal file
23
ueb05_news_example/src/decorator/StringDecorator.java
Normal 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();
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
31
ueb05_news_example/src/model/AuthoredItem.java
Normal file
31
ueb05_news_example/src/model/AuthoredItem.java
Normal 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;
|
||||
}
|
||||
}
|
@ -1,66 +1,54 @@
|
||||
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;
|
||||
}
|
||||
public class Image extends AuthoredItem {
|
||||
private final int width;
|
||||
private final int height;
|
||||
|
||||
/**
|
||||
* 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 > ^ < ";
|
||||
}
|
||||
// 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 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;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
17
ueb05_news_example/src/model/Item.java
Normal file
17
ueb05_news_example/src/model/Item.java
Normal 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;
|
||||
}
|
||||
}
|
@ -3,75 +3,63 @@ 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;
|
||||
public class NewsCollection extends Item {
|
||||
|
||||
private final List<Item> items = new LinkedList<>();
|
||||
|
||||
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");
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of articles stored directly in this collection.
|
||||
* @return articles stored in this collection
|
||||
* Gets the contents of all directly or transitively referenced items in
|
||||
* the given collection.
|
||||
*/
|
||||
public List<Article> getArticles() {
|
||||
return articles;
|
||||
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 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
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
11
ueb05_news_example/ueb05_news_example.iml
Normal file
11
ueb05_news_example/ueb05_news_example.iml
Normal 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>
|
Loading…
x
Reference in New Issue
Block a user