Compare commits

...

7 Commits
main ... ueb07

Author SHA1 Message Date
0nlineSam
6bcf48a5f6 Added pdf 2024-12-07 13:30:55 +01:00
0nlineSam
221d9cb5d8 ueb07 b) 2024-12-07 13:10:08 +01:00
0nlineSam
fcc25f0c21 Merge remote-tracking branch 'origin/ueb07' into ueb07 2024-12-07 12:14:37 +01:00
b1b8db8065
ueb07 a) 2024-12-07 12:13:00 +01:00
49a4e61738
.idea 2024-12-07 12:12:54 +01:00
5fc0f28281
ueb07 a) 2024-12-07 12:04:43 +01:00
5b45264ce0
.idea 2024-12-07 12:04:03 +01:00
18 changed files with 223 additions and 89 deletions

BIN
SQ - Exercise 7.pdf Normal file

Binary file not shown.

1
ueb07_news_example/.gitignore vendored Normal file

@ -0,0 +1 @@
/out/

8
ueb07_news_example/.idea/.gitignore generated vendored Normal file

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

6
ueb07_news_example/.idea/misc.xml generated Normal file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
ueb07_news_example/.idea/modules.xml generated Normal file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/ueb07_news_example.iml" filepath="$PROJECT_DIR$/ueb07_news_example.iml" />
</modules>
</component>
</project>

6
ueb07_news_example/.idea/vcs.xml generated Normal file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

@ -1,8 +1,6 @@
package client; package client;
import model.Article; import model.*;
import model.Image;
import model.NewsCollection;
public class Client { public class Client {
@ -20,9 +18,13 @@ public class Client {
.addItem(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(masterCollection.getOverviewInformation()); StringBuilder sb = new StringBuilder();
masterCollection.accept(new OverviewPrinter(sb));
System.out.print(sb);
System.out.println("\n===Contents===\n"); System.out.println("\n===Contents===\n");
System.out.print(masterCollection.getDetailedInformation()); sb = new StringBuilder();
masterCollection.accept(new DetailedPrinter(sb));
System.out.print(sb);
System.out.println("\n===After change===\n"); System.out.println("\n===After change===\n");
changedArticle.setTitle("Harder, Better, Faster, Stronger? Doping controls loosened"); changedArticle.setTitle("Harder, Better, Faster, Stronger? Doping controls loosened");

@ -20,7 +20,12 @@ public class Article extends NewsItem {
this.title = title; this.title = title;
this.author = author; this.author = author;
} }
@Override
public void accept(NewsItemVisitor visitor) {
visitor.visit(this);
}
/** /**
* Get the author of the article. * Get the author of the article.
* @return author * @return author
@ -36,19 +41,4 @@ public class Article extends NewsItem {
public String getContent() { public String getContent() {
return content; 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();
}
} }

@ -0,0 +1,50 @@
package model;
public class DetailedPrinter implements NewsItemVisitor {
StringBuilder sb;
public DetailedPrinter(StringBuilder sb) {
this.sb = sb;
}
/**
* Print details about news article.
*/
@Override
public void visit(final Article article) {
sb.append("###")
.append(article.getTitle())
.append("###");
sb.append("\n");
sb.append(article.getContent());
sb.append("\n");
}
/**
* Print details about image.
*/
@Override
public void visit(final Image image) {
sb.append("###")
.append(image.getTitle())
.append("###");
sb.append("\n");
sb.append(image.renderToString());
sb.append("\n");
}
/**
* Print details about news collection.
*/
@Override
public void visit(final NewsCollection collection) {
sb.append("###")
.append(collection.getTitle())
.append("###");
sb.append("\n");
for (NewsItem item : collection.getNewsItems()) {
item.accept(this);
sb.append("\n");
}
}
}

@ -19,6 +19,11 @@ public class Image extends NewsItem {
this.height = height; this.height = height;
this.author = author; this.author = author;
} }
@Override
public void accept(NewsItemVisitor visitor) {
visitor.visit(this);
}
/** /**
* Get the author of the image. * Get the author of the image.
@ -54,26 +59,4 @@ public class Image extends NewsItem {
*/ */
return " /\\_/\\ \n( o.o )\n > ^ < "; 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();
}
} }

@ -10,7 +10,12 @@ public class NewsCollection extends NewsItem {
public NewsCollection(String topic) { public NewsCollection(String topic) {
this.title = topic; this.title = topic;
} }
@Override
public void accept(NewsItemVisitor visitor) {
visitor.visit(this);
}
/** /**
* Get the list of items stored in this collection. * Get the list of items stored in this collection.
* @return items stored in this collection * @return items stored in this collection
@ -28,31 +33,4 @@ public class NewsCollection extends NewsItem {
newsItems.add(item); newsItems.add(item);
return this; 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();
}
} }

@ -8,6 +8,8 @@ public abstract class NewsItem {
super(); super();
} }
public abstract void accept(NewsItemVisitor visitor);
/** /**
* Get the topic of the collection. * Get the topic of the collection.
* @return * @return
@ -19,14 +21,4 @@ public abstract class NewsItem {
public void setTitle(String newTitle) { public void setTitle(String newTitle) {
title = 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();
} }

@ -0,0 +1,7 @@
package model;
public interface NewsItemVisitor {
void visit(final Article article);
void visit(final Image image);
void visit(final NewsCollection collection);
}

@ -0,0 +1,52 @@
package model;
public class OverviewPrinter implements NewsItemVisitor {
final StringBuilder sb;
/**
* @param sb StringBuilder to print overview into.
*/
public OverviewPrinter(StringBuilder sb) {
this.sb = sb;
}
/**
* Print overview about news article.
*/
@Override
public void visit(final Article article) {
sb.append("Article: ")
.append(article.getTitle())
.append(", Author: ")
.append(article.getAuthor());
}
/**
* Print overview about image.
*/
@Override
public void visit(final Image image) {
sb.append("Image: ")
.append(image.getTitle())
.append(", Resolution: ")
.append(image.getWidth())
.append("x")
.append(image.getHeight())
.append(", Author: ")
.append(image.getAuthor());
}
/**
* Print overview about news collection.
*/
@Override
public void visit(final NewsCollection collection) {
sb.append("###")
.append(collection.getTitle())
.append("###\n");
for (NewsItem item : collection.getNewsItems()) {
item.accept(this);
sb.append("\n");
}
}
}

@ -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>

@ -4,10 +4,19 @@ import model.Article;
import model.Image; import model.Image;
import model.NewsCollection; import model.NewsCollection;
public class Client { import java.util.Observable;
import java.util.Observer;
public class Client implements Observer {
public static void main(String[] args) { public static void main(String[] args) {
Client client = new Client();
client.run();
}
public void run() {
NewsCollection masterCollection = new NewsCollection("Daily news"); NewsCollection masterCollection = new NewsCollection("Daily news");
masterCollection.addObserver(this);
Article changedArticle = new Article("Harder, Better, Faster, Stronger. Doping controls loosened","ups"); Article changedArticle = new Article("Harder, Better, Faster, Stronger. Doping controls loosened","ups");
@ -23,9 +32,18 @@ public class Client {
System.out.print(masterCollection.getOverviewInformation()); System.out.print(masterCollection.getOverviewInformation());
System.out.println("\n===Contents===\n"); System.out.println("\n===Contents===\n");
System.out.print(masterCollection.getDetailedInformation()); System.out.print(masterCollection.getDetailedInformation());
masterCollection.addObserver(this);
System.out.println("\n===After change===\n"); System.out.println("\n===After change===\n");
changedArticle.setTitle("Harder, Better, Faster, Stronger? Doping controls loosened"); changedArticle.setTitle("Harder, Better, Faster, Stronger? Doping controls loosened");
masterCollection.addItem(new Image("A tasty cat", 640, 480, "alf")); masterCollection.addItem(new Image("A tasty cat", 640, 480, "alf"));
} }
@Override
public void update(Observable o, Object arg) {
System.out.println("### UPDATE ###");
System.out.println(arg);
}
} }

@ -1,16 +1,29 @@
package model; package model;
import java.util.LinkedList; import java.util.*;
import java.util.List;
public class NewsCollection extends NewsItem { public class NewsCollection extends NewsItem {
private Set<Observer> observers = new HashSet<>();
private List<NewsItem> newsItems = new LinkedList<NewsItem>(); private List<NewsItem> newsItems = new LinkedList<NewsItem>();
public NewsCollection(String topic) { public NewsCollection(String topic) {
this.title = topic; this.title = topic;
} }
@Override
public synchronized void addObserver(Observer o) {
super.addObserver(o);
observers.add(o);
newsItems.forEach(i -> i.addObserver(o));
}
@Override
public synchronized void deleteObserver(Observer o) {
super.deleteObserver(o);
observers.remove(o);
newsItems.forEach(i -> i.deleteObserver(o));
}
/** /**
* Get the list of items stored in this collection. * Get the list of items stored in this collection.
* @return items stored in this collection * @return items stored in this collection
@ -25,7 +38,11 @@ public class NewsCollection extends NewsItem {
* @return this collection (enables method chaining) * @return this collection (enables method chaining)
*/ */
public NewsCollection addItem(NewsItem item) { public NewsCollection addItem(NewsItem item) {
String updateNotification = getTitle() + "\n+ " + item.getTitle() + "\n";
newsItems.add(item); newsItems.add(item);
setChanged();
notifyObservers(updateNotification);
observers.forEach(item::addObserver);
return this; return this;
} }
@ -54,5 +71,5 @@ public class NewsCollection extends NewsItem {
builder.append("\n"); builder.append("\n");
} }
return builder.toString(); return builder.toString();
} }
} }

@ -1,6 +1,8 @@
package model; package model;
public abstract class NewsItem { import java.util.Observable;
public abstract class NewsItem extends Observable {
protected String title; protected String title;
@ -17,7 +19,10 @@ public abstract class NewsItem {
} }
public void setTitle(String newTitle) { public void setTitle(String newTitle) {
String updateNotification = "- " + title + "\n+ " + newTitle + "\n";
title = newTitle; title = newTitle;
setChanged();
notifyObservers(updateNotification);
} }
/** /**