import news example
This commit is contained in:
parent
64c1d0e743
commit
c41072785c
31
ueb07_news_example/src/client/Client.java
Normal file
31
ueb07_news_example/src/client/Client.java
Normal file
@ -0,0 +1,31 @@
|
||||
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");
|
||||
|
||||
Article changedArticle = new Article("Harder, Better, Faster, Stronger. Doping controls loosened","ups");
|
||||
|
||||
masterCollection.addItem(new NewsCollection("Sports")
|
||||
.addItem(changedArticle)
|
||||
.addItem(new Image("Athletic cat contest", 800, 600, "zdf")))
|
||||
.addItem(new NewsCollection("Local news")
|
||||
.addItem(new Article("Missing Cat missed.", "mfg"))
|
||||
.addItem(new Article("Corrupted local politician selling cats.", "ard"))
|
||||
.addItem(new Image("Missing cat", 1280, 720, "anonymous")));
|
||||
|
||||
System.out.println("\n===List===\n");
|
||||
System.out.print(masterCollection.getOverviewInformation());
|
||||
System.out.println("\n===Contents===\n");
|
||||
System.out.print(masterCollection.getDetailedInformation());
|
||||
System.out.println("\n===After change===\n");
|
||||
|
||||
changedArticle.setTitle("Harder, Better, Faster, Stronger? Doping controls loosened");
|
||||
masterCollection.addItem(new Image("A tasty cat", 640, 480, "alf"));
|
||||
}
|
||||
}
|
54
ueb07_news_example/src/model/Article.java
Normal file
54
ueb07_news_example/src/model/Article.java
Normal file
@ -0,0 +1,54 @@
|
||||
package model;
|
||||
|
||||
public class Article extends NewsItem {
|
||||
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.";
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 author of the article.
|
||||
* @return author
|
||||
*/
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the content of the article.
|
||||
* @return
|
||||
*/
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getOverviewInformation() {
|
||||
return "Article: " + this.getTitle() + ", Author: " + this.getAuthor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDetailedInformation() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("###" + this.getTitle() + "###");
|
||||
builder.append("\n");
|
||||
builder.append(this.getContent());
|
||||
builder.append("\n");
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
79
ueb07_news_example/src/model/Image.java
Normal file
79
ueb07_news_example/src/model/Image.java
Normal file
@ -0,0 +1,79 @@
|
||||
package model;
|
||||
|
||||
public class Image extends NewsItem {
|
||||
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 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 > ^ < ";
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a list showing information about all directly or transitively
|
||||
* referenced items in the given collection.
|
||||
*/
|
||||
@Override
|
||||
public String getOverviewInformation() {
|
||||
return "Image: " + this.getTitle()
|
||||
+ ", Resolution: " + this.getWidth()
|
||||
+ "x" + this.getHeight()
|
||||
+ ", Author: " + this.getAuthor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDetailedInformation() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("###" + this.getTitle() + "###");
|
||||
builder.append("\n");
|
||||
builder.append(renderToString());
|
||||
builder.append("\n");
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
58
ueb07_news_example/src/model/NewsCollection.java
Normal file
58
ueb07_news_example/src/model/NewsCollection.java
Normal file
@ -0,0 +1,58 @@
|
||||
package model;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class NewsCollection extends NewsItem {
|
||||
|
||||
private List<NewsItem> newsItems = new LinkedList<NewsItem>();
|
||||
|
||||
public NewsCollection(String topic) {
|
||||
this.title = topic;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of items stored in this collection.
|
||||
* @return items stored in this collection
|
||||
*/
|
||||
public List<NewsItem> getNewsItems() {
|
||||
return newsItems;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store another item to this collection.
|
||||
* @param item
|
||||
* @return this collection (enables method chaining)
|
||||
*/
|
||||
public NewsCollection addItem(NewsItem item) {
|
||||
newsItems.add(item);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a list showing information about all directly or transitively
|
||||
* referenced items in the given collection.
|
||||
*/
|
||||
public String getOverviewInformation() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("###" + this.getTitle() + "###");
|
||||
builder.append("\n");
|
||||
for (NewsItem item : this.getNewsItems()) {
|
||||
builder.append(item.getOverviewInformation());
|
||||
builder.append("\n");
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDetailedInformation() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("###" + this.getTitle() + "###");
|
||||
builder.append("\n");
|
||||
for (NewsItem item : this.getNewsItems()) {
|
||||
builder.append(item.getDetailedInformation());
|
||||
builder.append("\n");
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
32
ueb07_news_example/src/model/NewsItem.java
Normal file
32
ueb07_news_example/src/model/NewsItem.java
Normal file
@ -0,0 +1,32 @@
|
||||
package model;
|
||||
|
||||
public abstract class NewsItem {
|
||||
|
||||
protected String title;
|
||||
|
||||
public NewsItem() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the topic of the collection.
|
||||
* @return
|
||||
*/
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String newTitle) {
|
||||
title = newTitle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get overview information about the news item.
|
||||
*/
|
||||
public abstract String getOverviewInformation();
|
||||
|
||||
/**
|
||||
* Get the contents of the news item.
|
||||
*/
|
||||
public abstract String getDetailedInformation();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user