Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
tdt4100
v2022
students
Commits
8d022588
Commit
8d022588
authored
Mar 25, 2022
by
Magnus Schjølberg
Browse files
Add solutions to exercise lecture 6
parent
ccc62f43
Changes
8
Hide whitespace changes
Inline
Side-by-side
foreksempel/src/main/java/of6/delegation/lf/CallCenter.java
0 → 100644
View file @
8d022588
package
of6.delegation.kode
;
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
()));
}
}
foreksempel/src/main/java/of6/delegation/lf/CallRecipient.java
0 → 100644
View file @
8d022588
package
of6.delegation.kode
;
public
interface
CallRecipient
{
void
answerCall
(
String
call
);
}
foreksempel/src/main/java/of6/delegation/lf/Employee.java
0 → 100644
View file @
8d022588
package
of6.delegation.kode
;
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
++;
}
}
foreksempel/src/main/java/of6/delegation/lf/Oppgave4.txt
0 → 100644
View file @
8d022588
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
foreksempel/src/main/java/of6/observable/lf/App.java
0 → 100644
View file @
8d022588
package
of6.observable.kode
;
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
);
}
}
}
foreksempel/src/main/java/of6/observable/lf/AppListener.java
0 → 100644
View file @
8d022588
package
of6.observable.kode
;
public
interface
AppListener
{
void
receivePushNotification
(
String
notification
);
}
foreksempel/src/main/java/of6/observable/lf/Oppgave5.txt
0 → 100644
View file @
8d022588
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());
}
foreksempel/src/main/java/of6/observable/lf/Phone.java
0 → 100644
View file @
8d022588
package
of6.observable.kode
;
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
());
BinaryOperator
<
Double
>
op
=
(
a
,
b
)
->
a
+
b
;
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment