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

test

parent d8a62f24
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
<dependency> <dependency>
<groupId>org.openjfx</groupId> <groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId> <artifactId>javafx-fxml</artifactId>
<version>16-ea+5</version> <version>17.0.2</version>
</dependency> </dependency>
<!-- JUnit 5 --> <!-- JUnit 5 -->
......
package of7.kode.employees;
public class Assistant extends Employee {
public Assistant(String name) {
super(name, "Assistant");
}
public static void main(String[] args) {
Employee assistant = new Assistant("Magnus");
assistant.performTask("Arrange meeting");
assistant.performTask("Order stock");
}
}
package of7.lf.employees;
public class Assistant extends Employee {
public Assistant(String name) {
super(name, "Assistant");
}
@Override
public boolean canPerformTask(String task) {
return task.equals("Arrange meeting") || task.equals("Send email");
}
public static void main(String[] args) {
Employee assistant = new Assistant("Magnus");
assistant.performTask("Arrange meeting");
assistant.performTask("Order stock");
}
}
package of7.lf.employees;
import java.util.Arrays;
import java.util.List;
public class Boss extends Employee {
private List<Employee> subordinates;
public Boss(String name, List<Employee> subordinates) {
super(name, "Boss");
if (subordinates.isEmpty()) {
throw new IllegalArgumentException("A boss needs at least one subordinate");
}
this.subordinates = subordinates;
}
public Boss(String name, Employee... employees) {
this(name, Arrays.asList(employees));
}
@Override
public boolean canPerformTask(String task) {
return subordinates.stream().anyMatch((subordinate) -> subordinate.canPerformTask(task));
}
@Override
public void performTask(String task) {
subordinates.stream()
.filter((subordinate) -> subordinate.canPerformTask(task))
.findAny()
.orElseThrow(() -> new IllegalArgumentException("No subordinates can perform this task"))
.performTask(task);
}
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");
}
}
package of7.lf.employees;
public class Consultant extends Employee {
private int numberOfRemainingTasks;
public Consultant(String name, String role, int numberOfRemainingTasks) {
super(name, role);
this.numberOfRemainingTasks = numberOfRemainingTasks;
}
@Override
public void performTask(String task) {
if (numberOfRemainingTasks <= 0) {
throw new IllegalStateException(getName() + " has performed all their tasks");
}
super.performTask(task);
numberOfRemainingTasks = numberOfRemainingTasks - 1;
if (numberOfRemainingTasks == 0) {
role = "Quit";
}
}
@Override
public boolean canPerformTask(String task) {
return numberOfRemainingTasks > 0;
}
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");
}
}
package of7.lf.employees;
public abstract 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) {
if (!canPerformTask(task)) {
throw new IllegalArgumentException(name + " cannot perform task: " + task);
}
System.out.println(name + " [" + role + "] performed task: " + task);
}
public abstract boolean canPerformTask(String task);
}
package of7.lf.pushnotifications;
import java.time.LocalDate;
public class App extends PushNotificationSender {
private int lastNotificationSent = 0;
public void sendPushNotification(String notification) {
if (lastNotificationSent == LocalDate.now().toEpochDay()) {
throw new IllegalArgumentException("Daily notification already sent");
}
super.sendPushNotification(notification);
lastNotificationSent = (int) LocalDate.now().toEpochDay();
}
}
package of7.lf.pushnotifications;
public class OfficialApp extends PushNotificationSender {
}
package of7.lf.pushnotifications;
import java.util.ArrayList;
import java.util.List;
public class Phone implements PushNotificationListener {
private String name;
private List<String> notificationFeed = new ArrayList<>();
private List<PushNotificationSender> installedApps = new ArrayList<>();
public Phone(String name) {
this.name = name;
}
public void install(PushNotificationSender app) {
if (!installedApps.contains(app)) {
app.subscribe(this);
installedApps.add(app);
}
}
public void uninstall(PushNotificationSender 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();
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");
}
}
package of7.lf.pushnotifications;
public interface PushNotificationListener {
void receivePushNotification(String notification);
}
package of7.lf.pushnotifications;
import java.util.ArrayList;
import java.util.List;
public class PushNotificationSender {
private List<PushNotificationListener> clients = new ArrayList<>();
private List<String> sentPushNotifications = new ArrayList<>();
protected void sendPushNotification(String notification) {
sentPushNotifications.add(notification);
firePushNotification(notification);
}
public void subscribe(PushNotificationListener listener) {
if (!clients.contains(listener)) {
clients.add(listener);
}
}
public void unsubscribe(PushNotificationListener listener) {
if (clients.contains(listener)) {
clients.remove(listener);
}
}
private void firePushNotification(String notification) {
for (PushNotificationListener appListener : clients) {
appListener.receivePushNotification(notification);
}
}
}
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
<dependency> <dependency>
<groupId>org.openjfx</groupId> <groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId> <artifactId>javafx-fxml</artifactId>
<version>16-ea+5</version> <version>17.0.2</version>
</dependency> </dependency>
<!-- JUnit 5 --> <!-- JUnit 5 -->
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
<dependency> <dependency>
<groupId>org.openjfx</groupId> <groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId> <artifactId>javafx-fxml</artifactId>
<version>16-ea+5</version> <version>17.0.2</version>
</dependency> </dependency>
<!-- JUnit 5 --> <!-- JUnit 5 -->
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
<dependency> <dependency>
<groupId>org.openjfx</groupId> <groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId> <artifactId>javafx-fxml</artifactId>
<version>16</version> <version>17.0.2</version>
</dependency> </dependency>
<!-- JUnit 5 --> <!-- JUnit 5 -->
......
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