Commit d8a62f24 authored by Magnus Schjølberg's avatar Magnus Schjølberg
Browse files

Boilerplate for exercise lecture 7

parent 919fc924
package of7.kode.employees;
public class Employee {
private String name;
protected String role;
public Employee(String name, String role) {
this.name = name;
this.role = role;
}
public String getName() {
return name;
}
public String getRole() {
return role;
}
public void performTask(String task) {
System.out.println(name + " [" + role + "] performed task: " + task);
}
}
public static void main(String[] args) {
Employee assistant = new Assistant("Magnus");
assistant.performTask("Arrange meeting");
assistant.performTask("Order stock");
}
public static void main(String[] args) {
Consultant employee = new Consultant("Magnus", "Sommerjobber", 2);
employee.performTask("Svare på mail");
System.out.println(employee.getRole());
employee.performTask("Svare på mail");
System.out.println(employee.getRole());
employee.performTask("Svare på mail");
}
\ No newline at end of file
public static void main(String[] args) {
Assistant assistant = new Assistant("Magnus");
Consultant temp = new Consultant("Trine", "IT Consultant", 1);
Boss boss = new Boss("Børge", assistant, temp);
boss.performTask("Arrange meeting");
boss.performTask("Order stock");
boss.performTask("Pay salaries");
}
\ No newline at end of file
package of7.kode.pushnotifications;
import java.util.ArrayList;
import java.util.List;
public class App {
private List<AppListener> clients = new ArrayList<>();
private List<String> sentPushNotifications = new ArrayList<>();
public void sendPushNotification(String notification) {
sentPushNotifications.add(notification);
firePushNotification(notification);
}
public void subscribe(AppListener listener) {
if (!clients.contains(listener)) {
clients.add(listener);
}
}
public void unsubscribe(AppListener listener) {
if (clients.contains(listener)) {
clients.remove(listener);
}
}
private void firePushNotification(String notification) {
for (AppListener appListener : clients) {
appListener.receivePushNotification(notification);
}
}
}
package of7.kode.pushnotifications;
public interface AppListener {
void receivePushNotification(String notification);
}
public static void main(String[] args) {
Phone iPhone11Pro = new Phone("Magnus sin iPhone");
Phone SamsungGalaxy = new Phone("Kjetils Android");
App OOPmini = new App();
App OOPbank = new App();
OfficialApp SlowTime = new OfficialApp();
iPhone11Pro.install(OOPmini);
iPhone11Pro.install(OOPbank);
iPhone11Pro.install(SlowTime);
SamsungGalaxy.install(OOPmini);
SlowTime.sendPushNotification("Mamma ringer...");
SlowTime.sendPushNotification("5x tapte anrop fra Sjef McSjefete");
OOPmini.sendPushNotification("Nå har vi 40% rabatt på Hummus!");
System.out.println(iPhone11Pro.printPushNotifications());
System.out.println();
System.out.println(SamsungGalaxy.printPushNotifications());
System.out.println();
OOPbank.sendPushNotification("Søk om høyere kredittgrense idag!");
System.out.println(iPhone11Pro.printPushNotifications());
System.out.println();
System.out.println(SamsungGalaxy.printPushNotifications());
OOPbank.sendPushNotification("VÆR SÅ VENNLIG, SØK OM FORBRUKSLÅN");
}
\ No newline at end of file
package of7.kode.pushnotifications;
import java.util.ArrayList;
import java.util.List;
public class Phone implements AppListener {
private String name;
private List<String> notificationFeed = new ArrayList<>();
private List<App> installedApps = new ArrayList<>();
public Phone(String name) {
this.name = name;
}
public void install(App app) {
if (!installedApps.contains(app)) {
app.subscribe(this);
installedApps.add(app);
}
}
public void uninstall(App app) {
if (installedApps.contains(app)) {
app.unsubscribe(this);
installedApps.remove(app);
}
}
@Override
public void receivePushNotification(String notification) {
notificationFeed.add(notification);
}
public String printPushNotifications() {
return name + ":\n" + String.join("\n", notificationFeed);
}
public static void main(String[] args) {
Phone iPhone11Pro = new Phone("Magnus sin iPhone");
Phone SamsungGalaxy = new Phone("Kjetils Android");
App OOPmini = new App();
App OOPbank = new App();
iPhone11Pro.install(OOPmini);
iPhone11Pro.install(OOPbank);
SamsungGalaxy.install(OOPmini);
OOPmini.sendPushNotification("Nå har vi 40% rabatt på Hummus!");
System.out.println(iPhone11Pro.printPushNotifications());
System.out.println();
System.out.println(SamsungGalaxy.printPushNotifications());
System.out.println();
OOPbank.sendPushNotification("Søk om høyere kredittgrense idag!");
System.out.println(iPhone11Pro.printPushNotifications());
System.out.println();
System.out.println(SamsungGalaxy.printPushNotifications());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment