Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
ohj2
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
tornhale
ohj2
Commits
25c817aa
Commit
25c817aa
authored
2 weeks ago
by
tornhale
Browse files
Options
Downloads
Patches
Plain Diff
Kirjat-luokan poista-aliohjelmasta tehty parempi
parent
d110e8f6
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
src/Kirjalista/Kirjat.java
+7
-9
7 additions, 9 deletions
src/Kirjalista/Kirjat.java
src/Kirjalista/test/KirjatTest.java
+11
-7
11 additions, 7 deletions
src/Kirjalista/test/KirjatTest.java
with
18 additions
and
16 deletions
src/Kirjalista/Kirjat.java
+
7
−
9
View file @
25c817aa
...
...
@@ -37,6 +37,7 @@ import java.util.NoSuchElementException;
*
* int k2Id = k2.getId();
* kirjat.poista(k2Id);
* kirjat.poista(0); #THROWS NoSuchElementException
* kirjat.hae(k2Id); #THROWS NoSuchElementException
*
* </pre>
...
...
@@ -81,17 +82,14 @@ public class Kirjat {
* @throws NoSuchElementException Jos id:tä vastaavaa kirjaa ei löydy
*/
public
void
poista
(
int
id
)
throws
NoSuchElementException
{
for
(
int
i
=
0
;
i
<
alkiot
.
length
;
i
++)
{
if
(
alkiot
[
i
].
getId
()
==
id
)
{
for
(
int
j
=
i
;
j
<
alkiot
.
length
-
1
;
j
++)
{
alkiot
[
j
]
=
alkiot
[
j
+
1
];
}
alkiot
[
alkiot
.
length
-
1
]
=
null
;
this
.
lkm
--;
return
;
int
luku
,
kirj
;
for
(
luku
=
0
,
kirj
=
0
;
luku
<
this
.
lkm
;
luku
++)
{
if
(
this
.
alkiot
[
luku
].
getId
()
!=
id
)
{
this
.
alkiot
[
kirj
++]
=
this
.
alkiot
[
luku
];
}
}
throw
new
NoSuchElementException
(
"Kirjaa id:llä "
+
id
+
" ei löydy."
);
this
.
lkm
=
kirj
;
if
(
luku
==
kirj
)
throw
new
NoSuchElementException
(
"Kirjaa id:llä "
+
id
+
" ei löydy."
);
}
/**
...
...
This diff is collapsed.
Click to expand it.
src/Kirjalista/test/KirjatTest.java
+
11
−
7
View file @
25c817aa
...
...
@@ -8,7 +8,7 @@ import Kirjalista.*;
/**
* Test class made by ComTest
* @version 2025.03.1
3
1
9:42:50
// Generated by ComTest
* @version 2025.03.1
7
1
6:07:43
// Generated by ComTest
*
*/
@SuppressWarnings
({
"all"
})
...
...
@@ -47,30 +47,34 @@ public class KirjatTest {
int
k2Id
=
k2
.
getId
();
kirjat
.
poista
(
k2Id
);
try
{
kirjat
.
hae
(
k2Id
);
kirjat
.
poista
(
0
);
fail
(
"Kirjat: 40 Did not throw NoSuchElementException"
);
}
catch
(
NoSuchElementException
_e_
){
_e_
.
getMessage
();
}
try
{
kirjat
.
hae
(
k2Id
);
fail
(
"Kirjat: 41 Did not throw NoSuchElementException"
);
}
catch
(
NoSuchElementException
_e_
){
_e_
.
getMessage
();
}
}
// Generated by ComTest END
// Generated by ComTest BEGIN
/** testLisaa5
3
*/
/** testLisaa5
4
*/
@Test
public
void
testLisaa5
3
()
{
// Kirjat: 5
3
public
void
testLisaa5
4
()
{
// Kirjat: 5
4
Kirja
kirja
=
new
Kirja
();
Kirjat
kirjat
=
new
Kirjat
();
try
{
for
(
int
i
=
0
;
i
<
8
;
i
++)
{
kirjat
.
lisaa
(
kirja
);
assertEquals
(
"From: Kirjat line:
59
"
,
i
+
1
,
kirjat
.
getLkm
());
assertEquals
(
"From: Kirjat line:
60
"
,
i
+
1
,
kirjat
.
getLkm
());
}
assertEquals
(
"From: Kirjat line: 6
1
"
,
8
,
kirjat
.
getLkm
());
assertEquals
(
"From: Kirjat line: 6
2
"
,
8
,
kirjat
.
getLkm
());
}
catch
(
SailoException
e
)
{
System
.
out
.
println
(
"Comtest heittaa virheita, jos ei ole trycatchia"
);
}
try
{
kirjat
.
lisaa
(
kirja
);
fail
(
"Kirjat: 6
5
Did not throw SailoException"
);
fail
(
"Kirjat: 6
6
Did not throw SailoException"
);
}
catch
(
SailoException
_e_
){
_e_
.
getMessage
();
}
}
// 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