Compare commits

...

8 Commits

8 changed files with 174 additions and 213 deletions

2
ueb05_news_example/.gitignore vendored Normal file
View File

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

View File

@ -6,72 +6,19 @@ import model.NewsCollection;
public class Client { public class Client {
public static void main(String[] args) { public static void main() {
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(new Image("Athletic cat contest", 800, 600, "zdf")))
.addCollection(new NewsCollection("Local news") .addItem(new NewsCollection("Local news")
.addArticle(new Article("Missing Cat missed.", "mfg")) .addItem(new Article("Missing Cat missed.", "mfg"))
.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(new Image("Missing cat", 1280, 720, "anonymous")));
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

@ -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;
/** // Fill in dummy content
* Creates an image with the given title and author and a resolution {
* defined by width and height. content = " /\\_/\\ \n( o.o )\n > ^ < ";
* @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. * Creates an image with the given title and author and a resolution
* @return title * defined by width and height.
*/ *
public String getTitle() { * @param title title of image
return title; * @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() {
* Get the author of the image. return "Image: " + getTitle()
* @return author + ", Resolution: " + getWidth()
*/ + "x" + getHeight()
public String getAuthor() { + ", Author: " + getAuthor();
return author; }
}
/** //=================================
* Get the width of the image.
* @return width
*/
public int getWidth() {
return width;
}
/** /**
* Get the height of the image. * Get the width of the image.
* @return height *
*/ * @return width
public int getHeight() { */
return height; public int getWidth() {
} return width;
}
/** /**
* Renders the image as String and return the rendered image. * Get the height of the image.
* @return String representing the rendered image *
*/ * @return height
public String renderToString() { */
/* public int getHeight() {
* Dummy implementation return height;
*/ }
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.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>();
private List<NewsCollection> collections = new LinkedList<NewsCollection>();
public NewsCollection(String topic) { public NewsCollection(String title) {
this.topic = topic; this.title = title;
} }
/** /**
* 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("###")
* Get the list of articles stored directly in this collection. .append("\n");
* @return articles stored in this collection for(Item item : getItems()){
*/ builder
public List<Article> getArticles() { .append(item.getOverview())
return articles; .append("\n");
}
return builder.toString();
} }
/** /**
* Get the list of images stored directly in this collection. * Gets the contents of all directly or transitively referenced items in
* @return images stored in this collection * the given collection.
*/ */
public List<Image> getImages() { public String getDetails(){
return images; 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. * Store an item directly in this collection.
* @return collections stored in this collection * @param item to add
*/
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>