Commit af174178 authored by Børge Haugset's avatar Børge Haugset
Browse files
parents ed1c3f69 a9fae713
package of6.delegation.kode;
public class Employee {
private String name;
private 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 static void main(String[] args) {
CallCenter callCenter = new CallCenter();
List<Employee> employeeList = List.of(
new Employee("Børge", "Teamleder"),
new Employee("Magnus", "Junior"),
new Employee("Nora", "Junior"),
new Employee("Jostein", "Junior"),
new Employee("Mattias", "Teamleder"),
new Employee("Eirik", "HR"));
employeeList.forEach(employee -> callCenter.addEmployee(employee));
callCenter.answerCall("klage");
callCenter.answerCall("klage");
callCenter.answerCall("internt");
callCenter.answerCall("eskalering");
callCenter.answerCall("spørsmål");
callCenter.answerCall("klage");
callCenter.answerCall("eskalering");
callCenter.answerCall("spørsmål");
callCenter.answerCall("spørsmål");
employeeList.forEach(
employee -> System.out.println(employee.getName() + " " + employee.getNumberOfCallsAnswered()));
}
\ No newline at end of file
package of6.delegation.lf;
import java.util.ArrayList;
import java.util.List;
public class CallCenter implements CallRecipient {
private List<Employee> employees = new ArrayList<>();
public void addEmployee(Employee employee) {
if (!employees.contains(employee)) {
employees.add(employee);
}
}
public Employee getEmployeeForTask(String role) {
return employees.stream()
.filter(employee -> employee.getRole().equals(role))
.min((employee1, employee2) -> employee1.getNumberOfCallsAnswered()
- employee2.getNumberOfCallsAnswered())
.get();
}
@Override
public void answerCall(String call) {
String role = switch (call) {
case "spørsmål", "klage" -> "Junior";
case "eskalering" -> "Teamleder";
case "intern" -> "HR";
default -> "Junior";
};
getEmployeeForTask(role).answerCall(call);
}
public static void main(String[] args) {
CallCenter callCenter = new CallCenter();
List<Employee> employeeList = List.of(
new Employee("Børge", "Teamleder"),
new Employee("Magnus", "Junior"),
new Employee("Nora", "Junior"),
new Employee("Jostein", "Junior"),
new Employee("Mattias", "Teamleder"),
new Employee("Eirik", "HR"));
employeeList.forEach(employee -> callCenter.addEmployee(employee));
callCenter.answerCall("klage");
callCenter.answerCall("klage");
callCenter.answerCall("internt");
callCenter.answerCall("eskalering");
callCenter.answerCall("spørsmål");
callCenter.answerCall("klage");
callCenter.answerCall("eskalering");
callCenter.answerCall("spørsmål");
callCenter.answerCall("spørsmål");
employeeList.forEach(
employee -> System.out.println(employee.getName() + " " + employee.getNumberOfCallsAnswered()));
}
}
package of6.delegation.lf;
public interface CallRecipient {
void answerCall(String call);
}
package of6.delegation.lf;
public class Employee implements CallRecipient {
private String name;
private String role;
private int numberOfCallsAnswered = 0;
public Employee(String name, String role) {
this.name = name;
this.role = role;
}
public String getName() {
return name;
}
public String getRole() {
return role;
}
public int getNumberOfCallsAnswered() {
return numberOfCallsAnswered;
}
@Override
public void answerCall(String call) {
System.out.println(name + " [" + role + "] answered call: " + call);
numberOfCallsAnswered++;
}
}
public static void main(String[] args) {
CallCenter callCenter = new CallCenter();
List<Employee> employeeList = List.of(
new Employee("Børge", "Teamleder"),
new Employee("Magnus", "Junior"),
new Employee("Nora", "Junior"),
new Employee("Jostein", "Junior"),
new Employee("Mattias", "Teamleder"),
new Employee("Eirik", "HR"));
employeeList.forEach(employee -> callCenter.addEmployee(employee));
callCenter.answerCall("klage");
callCenter.answerCall("klage");
callCenter.answerCall("internt");
callCenter.answerCall("eskalering");
callCenter.answerCall("spørsmål");
callCenter.answerCall("klage");
callCenter.answerCall("eskalering");
callCenter.answerCall("spørsmål");
callCenter.answerCall("spørsmål");
employeeList.forEach(
employee -> System.out.println(employee.getName() + " " + employee.getNumberOfCallsAnswered()));
}
\ No newline at end of file
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());
}
package of6.observable.lf;
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 of6.observable.lf;
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();
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());
}
package of6.observable.lf;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BinaryOperator;
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());
}
}
......@@ -4,7 +4,7 @@ Oppgaven handler om feilsøking ("debugging") av en Iterator-implementasjon ved
Les først denne artikkelen om bruk av debugger i VSCode om du ikke er kjent med dette: [Kjøring av kode og debugging i VS Code](https://www.ntnu.no/wiki/pages/viewpage.action?pageId=235996724)
Klassen **StringMergingIterator** implementerer grensesnittet [**Iterator<String>**](https://www.ntnu.no/wiki/display/tdt4100/Iterasjon+med+Iterator+og+Iterable), og leverer annenhver verdi fra to andre iteratorer av typen **Iterator<String>**. Denne iteratoren fletter altså verdiene fra to andre gitte iteratorer, og er altså en meta-iterator. Meta-iteratorer er iteratorer som bruker andre iteratorer som datakilder. Se lysark om teknikken: [Forelesningslysark om meta-iteratorer](https://docs.google.com/file/d/0B9IltfWcNirndERTb1RnTGM3YWs/preview)
Klassen **StringMergingIterator** implementerer grensesnittet [**Iterator<String>**](https://www.ntnu.no/wiki/display/tdt4100/Iterasjon+med+Iterator+og+Iterable), og leverer annenhver verdi fra to andre iteratorer av typen **Iterator<String>**. Denne iteratoren fletter altså verdiene fra to andre gitte iteratorer, og er altså en meta-iterator. Meta-iteratorer er iteratorer som bruker andre iteratorer som datakilder. Se lysark om teknikken inne på blackboard-siden til emnet -> Undervisningsmateriell -> Forelesninger: Slides og opptak -> Uke 12 -> Slides om meta-iteratorer.
**StringMergingIterator** har følgende konstruktør:
......
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