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
d62582e3
Commit
d62582e3
authored
Apr 03, 2022
by
Jostein Hjortland Tysse
Browse files
Merge branch 'main' of
https://gitlab.stud.idi.ntnu.no/tdt4100/v2022/students
into main
parents
9ca48b27
036b0ac6
Changes
74
Hide whitespace changes
Inline
Side-by-side
foreksempel/src/main/java/of7/kode/employees/Employee.java
0 → 100644
View file @
d62582e3
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
);
}
}
foreksempel/src/main/java/of7/kode/employees/Oppgave1.txt
0 → 100644
View file @
d62582e3
public static void main(String[] args) {
Employee assistant = new Assistant("Magnus");
assistant.performTask("Arrange meeting");
assistant.performTask("Order stock");
}
foreksempel/src/main/java/of7/kode/employees/Oppgave3.txt
0 → 100644
View file @
d62582e3
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
foreksempel/src/main/java/of7/kode/employees/Oppgave4.txt
0 → 100644
View file @
d62582e3
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
foreksempel/src/main/java/of7/kode/pushnotifications/App.java
0 → 100644
View file @
d62582e3
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
);
}
}
}
foreksempel/src/main/java/of7/kode/pushnotifications/AppListener.java
0 → 100644
View file @
d62582e3
package
of7.kode.pushnotifications
;
public
interface
AppListener
{
void
receivePushNotification
(
String
notification
);
}
foreksempel/src/main/java/of7/kode/pushnotifications/Oppgave5.txt
0 → 100644
View file @
d62582e3
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
foreksempel/src/main/java/of7/kode/pushnotifications/Phone.java
0 → 100644
View file @
d62582e3
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
());
}
}
foreksempel/src/main/java/of7/lf/employees/Assistant.java
0 → 100644
View file @
d62582e3
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"
);
}
}
\ No newline at end of file
foreksempel/src/main/java/of7/lf/employees/Boss.java
0 → 100644
View file @
d62582e3
package
of7.lf.employees
;
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 must have at least one subordinate"
);
}
this
.
subordinates
=
subordinates
;
}
@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 to perform task!"
))
.
performTask
(
task
);
}
public
static
void
main
(
String
[]
args
)
{
Assistant
assistant
=
new
Assistant
(
"Magnus"
);
Consultant
temp
=
new
Consultant
(
"Trine"
,
"IT Consultant"
,
2
);
Boss
boss
=
new
Boss
(
"Børge"
,
List
.
of
(
assistant
,
temp
));
boss
.
performTask
(
"Arrange meeting"
);
boss
.
performTask
(
"Order stock"
);
boss
.
performTask
(
"Pay salaries"
);
}
}
foreksempel/src/main/java/of7/lf/employees/Consultant.java
0 → 100644
View file @
d62582e3
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
boolean
canPerformTask
(
String
task
)
{
return
numberOfRemainingTasks
>
0
;
}
@Override
public
void
performTask
(
String
task
)
{
if
(!
canPerformTask
(
task
))
{
throw
new
IllegalArgumentException
(
this
.
getName
()
+
" has performed all of their tasks"
);
}
super
.
performTask
(
task
);
numberOfRemainingTasks
-=
1
;
if
(
numberOfRemainingTasks
==
0
)
{
role
=
"Quit"
;
}
}
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"
);
}
}
foreksempel/src/main/java/of7/lf/employees/Employee.java
0 → 100644
View file @
d62582e3
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
);
}
foreksempel/src/main/java/of7/lf/pushnotifications/App.java
0 → 100644
View file @
d62582e3
package
of7.lf.pushnotifications
;
import
java.time.LocalDate
;
public
class
App
extends
PushNotificationSender
{
private
int
lastNotificationSent
=
0
;
@Override
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
();
}
}
foreksempel/src/main/java/of7/lf/pushnotifications/OfficialApp.java
0 → 100644
View file @
d62582e3
package
of7.lf.pushnotifications
;
public
class
OfficialApp
extends
PushNotificationSender
{
}
foreksempel/src/main/java/of7/lf/pushnotifications/Phone.java
0 → 100644
View file @
d62582e3
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"
);
}
}
foreksempel/src/main/java/of7/lf/pushnotifications/PushNotificationListener.java
0 → 100644
View file @
d62582e3
package
of7.lf.pushnotifications
;
public
interface
PushNotificationListener
{
void
receivePushNotification
(
String
notification
);
}
foreksempel/src/main/java/of7/lf/pushnotifications/PushNotificationSender.java
0 → 100644
View file @
d62582e3
package
of7.lf.pushnotifications
;
import
java.util.ArrayList
;
import
java.util.List
;
public
abstract
class
PushNotificationSender
{
private
List
<
PushNotificationListener
>
clients
=
new
ArrayList
<>();
private
List
<
String
>
sentPushNotifications
=
new
ArrayList
<>();
public
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
listener
:
clients
)
{
listener
.
receivePushNotification
(
notification
);
}
}
}
foreksempel/src/main/java/uke12/listener/stocks_forelesning/FileObserver.java
0 → 100644
View file @
d62582e3
package
uke12.listener.stocks_forelesning
;
import
java.io.BufferedWriter
;
import
java.io.FileWriter
;
import
java.io.PrintWriter
;
public
class
FileObserver
implements
IObserver
{
public
FileObserver
(
Observable
observable
)
{
observable
.
addObserver
(
this
);
}
@Override
public
void
update
(
Observable
observable
,
String
name
)
{
try
(
PrintWriter
printWriter
=
new
PrintWriter
(
new
BufferedWriter
(
new
FileWriter
(
"Fil.txt"
,
true
))))
{
}
catch
(
Exception
e
)
{
}
}
}
foreksempel/src/main/java/uke12/listener/stocks_forelesning/IObserver.java
0 → 100644
View file @
d62582e3
package
uke12.listener.stocks_forelesning
;
public
interface
IObserver
{
public
void
update
(
Observable
observable
,
String
name
);
}
foreksempel/src/main/java/uke12/listener/stocks_forelesning/Observable.java
0 → 100644
View file @
d62582e3
package
uke12.listener.stocks_forelesning
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.HashMap
;
import
java.util.Map
;
public
class
Observable
{
Map
<
String
,
Double
>
stocks
=
new
HashMap
<>();
Collection
<
IObserver
>
observers
=
new
ArrayList
<>();;
public
void
updateStock
(
String
name
,
double
value
)
{
stocks
.
put
(
name
,
stocks
.
getOrDefault
(
name
,
0.0
)
+
value
);
System
.
out
.
println
(
name
+
" oppdater"
);
notifyObservers
(
name
);
}
private
void
notifyObservers
(
String
name
)
{
for
(
IObserver
iobserver
:
observers
)
{
iobserver
.
update
(
this
,
name
);
}
}
public
void
addObserver
(
IObserver
observer
)
{
this
.
observers
.
add
(
observer
);
}
public
void
removeObserver
(
IObserver
observer
)
{
this
.
observers
.
remove
(
observer
);
}
public
double
getStockValue
(
String
name
)
{
return
stocks
.
get
(
name
);
}
}
Prev
1
2
3
4
Next
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