Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
k2024
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tie
ohj2
esimerkit
k2024
Commits
32526cdc
Commit
32526cdc
authored
1 year ago
by
Vesa Lappalainen
Browse files
Options
Downloads
Patches
Plain Diff
kerhoon lisatty Harrastukset
parent
b77873a2
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
luennot/live17/src/kerho/Kerho.java
+69
-0
69 additions, 0 deletions
luennot/live17/src/kerho/Kerho.java
luennot/live17/src/kerho/test/KerhoTest.java
+75
-0
75 additions, 0 deletions
luennot/live17/src/kerho/test/KerhoTest.java
with
144 additions
and
0 deletions
luennot/live17/src/kerho/Kerho.java
+
69
−
0
View file @
32526cdc
package
kerho
;
package
kerho
;
import
java.util.List
;
/**
/**
* Kerho-luokka, joka huolehtii jäsenistöstä. Pääosin kaikki metodit
* Kerho-luokka, joka huolehtii jäsenistöstä. Pääosin kaikki metodit
...
@@ -12,6 +13,7 @@ package kerho;
...
@@ -12,6 +13,7 @@ package kerho;
*/
*/
public
class
Kerho
{
public
class
Kerho
{
private
final
Jasenet
jasenet
=
new
Jasenet
();
private
final
Jasenet
jasenet
=
new
Jasenet
();
private
final
Harrastukset
harrastukset
=
new
Harrastukset
();
/**
/**
...
@@ -61,7 +63,52 @@ public class Kerho {
...
@@ -61,7 +63,52 @@ public class Kerho {
jasenet
.
lisaa
(
jasen
);
jasenet
.
lisaa
(
jasen
);
}
}
/**
* Listään uusi harrastus kerhoon
* @param har lisättävä harrastus
*/
public
void
lisaa
(
Harrastus
har
)
{
harrastukset
.
lisaa
(
har
);
}
/**
* Haetaan kaikki jäsen harrastukset
* @param jasen jäsen jolle harrastuksia haetaan
* @return tietorakenne jossa viiteet löydetteyihin harrastuksiin
* @example
* <pre name="test">
* #import java.util.*;
*
* Kerho kerho = new Kerho();
* Jasen aku1 = new Jasen(), aku2 = new Jasen(), aku3 = new Jasen();
* aku1.rekisteroi(); aku2.rekisteroi(); aku3.rekisteroi();
* int id1 = aku1.getTunnusNro();
* int id2 = aku2.getTunnusNro();
* Harrastus pitsi11 = new Harrastus(id1); kerho.lisaa(pitsi11);
* Harrastus pitsi12 = new Harrastus(id1); kerho.lisaa(pitsi12);
* Harrastus pitsi21 = new Harrastus(id2); kerho.lisaa(pitsi21);
* Harrastus pitsi22 = new Harrastus(id2); kerho.lisaa(pitsi22);
* Harrastus pitsi23 = new Harrastus(id2); kerho.lisaa(pitsi23);
*
* List<Harrastus> loytyneet;
* loytyneet = kerho.annaHarrastukset(aku3);
* loytyneet.size() === 0;
* loytyneet = kerho.annaHarrastukset(aku1);
* loytyneet.size() === 2;
* loytyneet.get(0) == pitsi11 === true;
* loytyneet.get(1) == pitsi12 === true;
* loytyneet = kerho.annaHarrastukset(aku2);
* loytyneet.size() === 3;
* loytyneet.get(0) == pitsi21 === true;
* </pre>
*/
public
List
<
Harrastus
>
annaHarrastukset
(
Jasen
jasen
)
{
return
harrastukset
.
annaHarrastukset
(
jasen
.
getTunnusNro
());
}
/**
/**
* Palauttaa i:n jäsenen
* Palauttaa i:n jäsenen
* @param i monesko jäsen palautetaan
* @param i monesko jäsen palautetaan
...
@@ -112,12 +159,34 @@ public class Kerho {
...
@@ -112,12 +159,34 @@ public class Kerho {
kerho
.
lisaa
(
aku1
);
kerho
.
lisaa
(
aku1
);
kerho
.
lisaa
(
aku2
);
kerho
.
lisaa
(
aku2
);
Harrastus
pitsi1
=
new
Harrastus
();
pitsi1
.
vastaaPitsinNyplays
(
aku2
.
getTunnusNro
());
Harrastus
pitsi2
=
new
Harrastus
();
pitsi2
.
vastaaPitsinNyplays
(
aku1
.
getTunnusNro
());
Harrastus
pitsi3
=
new
Harrastus
();
pitsi3
.
vastaaPitsinNyplays
(
aku2
.
getTunnusNro
());
Harrastus
pitsi4
=
new
Harrastus
();
pitsi4
.
vastaaPitsinNyplays
(
aku2
.
getTunnusNro
());
kerho
.
lisaa
(
pitsi1
);
kerho
.
lisaa
(
pitsi2
);
kerho
.
lisaa
(
pitsi3
);
kerho
.
lisaa
(
pitsi4
);
System
.
out
.
println
(
"============= Kerhon testi ================="
);
System
.
out
.
println
(
"============= Kerhon testi ================="
);
for
(
int
i
=
0
;
i
<
kerho
.
getJasenia
();
i
++)
{
for
(
int
i
=
0
;
i
<
kerho
.
getJasenia
();
i
++)
{
Jasen
jasen
=
kerho
.
annaJasen
(
i
);
Jasen
jasen
=
kerho
.
annaJasen
(
i
);
System
.
out
.
println
(
"Jäsen paikassa: "
+
i
);
System
.
out
.
println
(
"Jäsen paikassa: "
+
i
);
jasen
.
tulosta
(
System
.
out
);
jasen
.
tulosta
(
System
.
out
);
List
<
Harrastus
>
harrastukset
=
kerho
.
annaHarrastukset
(
jasen
);
for
(
Harrastus
har
:
harrastukset
)
{
System
.
out
.
print
(
har
.
getJasenNro
()
+
" "
);
har
.
tulosta
(
System
.
out
);
}
System
.
out
.
println
();
}
}
}
catch
(
SailoException
ex
)
{
}
catch
(
SailoException
ex
)
{
...
...
This diff is collapsed.
Click to expand it.
luennot/live17/src/kerho/test/KerhoTest.java
0 → 100644
+
75
−
0
View file @
32526cdc
package
kerho.test
;
// Generated by ComTest BEGIN
import
java.util.*
;
import
static
org
.
junit
.
Assert
.*;
import
org.junit.*
;
import
kerho.*
;
// Generated by ComTest END
/**
* Test class made by ComTest
* @version 2024.03.04 11:00:24 // Generated by ComTest
*
*/
@SuppressWarnings
({
"all"
})
public
class
KerhoTest
{
// Generated by ComTest BEGIN
/**
* testLisaa43
* @throws SailoException when error
*/
@Test
public
void
testLisaa43
()
throws
SailoException
{
// Kerho: 43
Kerho
kerho
=
new
Kerho
();
Jasen
aku1
=
new
Jasen
(),
aku2
=
new
Jasen
();
aku1
.
rekisteroi
();
aku2
.
rekisteroi
();
assertEquals
(
"From: Kerho line: 48"
,
0
,
kerho
.
getJasenia
());
kerho
.
lisaa
(
aku1
);
assertEquals
(
"From: Kerho line: 49"
,
1
,
kerho
.
getJasenia
());
kerho
.
lisaa
(
aku2
);
assertEquals
(
"From: Kerho line: 50"
,
2
,
kerho
.
getJasenia
());
kerho
.
lisaa
(
aku1
);
assertEquals
(
"From: Kerho line: 51"
,
3
,
kerho
.
getJasenia
());
assertEquals
(
"From: Kerho line: 52"
,
3
,
kerho
.
getJasenia
());
assertEquals
(
"From: Kerho line: 53"
,
aku1
,
kerho
.
annaJasen
(
0
));
assertEquals
(
"From: Kerho line: 54"
,
aku2
,
kerho
.
annaJasen
(
1
));
assertEquals
(
"From: Kerho line: 55"
,
aku1
,
kerho
.
annaJasen
(
2
));
try
{
assertEquals
(
"From: Kerho line: 56"
,
aku1
,
kerho
.
annaJasen
(
3
));
fail
(
"Kerho: 56 Did not throw IndexOutOfBoundsException"
);
}
catch
(
IndexOutOfBoundsException
_e_
){
_e_
.
getMessage
();
}
kerho
.
lisaa
(
aku1
);
assertEquals
(
"From: Kerho line: 57"
,
4
,
kerho
.
getJasenia
());
kerho
.
lisaa
(
aku1
);
assertEquals
(
"From: Kerho line: 58"
,
5
,
kerho
.
getJasenia
());
try
{
kerho
.
lisaa
(
aku1
);
fail
(
"Kerho: 59 Did not throw SailoException"
);
}
catch
(
SailoException
_e_
){
_e_
.
getMessage
();
}
}
// Generated by ComTest END
// Generated by ComTest BEGIN
/** testAnnaHarrastukset81 */
@Test
public
void
testAnnaHarrastukset81
()
{
// Kerho: 81
Kerho
kerho
=
new
Kerho
();
Jasen
aku1
=
new
Jasen
(),
aku2
=
new
Jasen
(),
aku3
=
new
Jasen
();
aku1
.
rekisteroi
();
aku2
.
rekisteroi
();
aku3
.
rekisteroi
();
int
id1
=
aku1
.
getTunnusNro
();
int
id2
=
aku2
.
getTunnusNro
();
Harrastus
pitsi11
=
new
Harrastus
(
id1
);
kerho
.
lisaa
(
pitsi11
);
Harrastus
pitsi12
=
new
Harrastus
(
id1
);
kerho
.
lisaa
(
pitsi12
);
Harrastus
pitsi21
=
new
Harrastus
(
id2
);
kerho
.
lisaa
(
pitsi21
);
Harrastus
pitsi22
=
new
Harrastus
(
id2
);
kerho
.
lisaa
(
pitsi22
);
Harrastus
pitsi23
=
new
Harrastus
(
id2
);
kerho
.
lisaa
(
pitsi23
);
List
<
Harrastus
>
loytyneet
;
loytyneet
=
kerho
.
annaHarrastukset
(
aku3
);
assertEquals
(
"From: Kerho line: 97"
,
0
,
loytyneet
.
size
());
loytyneet
=
kerho
.
annaHarrastukset
(
aku1
);
assertEquals
(
"From: Kerho line: 99"
,
2
,
loytyneet
.
size
());
assertEquals
(
"From: Kerho line: 100"
,
true
,
loytyneet
.
get
(
0
)
==
pitsi11
);
assertEquals
(
"From: Kerho line: 101"
,
true
,
loytyneet
.
get
(
1
)
==
pitsi12
);
loytyneet
=
kerho
.
annaHarrastukset
(
aku2
);
assertEquals
(
"From: Kerho line: 103"
,
3
,
loytyneet
.
size
());
assertEquals
(
"From: Kerho line: 104"
,
true
,
loytyneet
.
get
(
0
)
==
pitsi21
);
}
// Generated by ComTest END
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment