Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
k2022
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
k2022
Commits
69a60e3b
Commit
69a60e3b
authored
3 years ago
by
Vesa Lappalainen
Browse files
Options
Downloads
Patches
Plain Diff
arvohetu
parent
cd7f2b1a
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/live13/src/kanta/HetuTarkistus.java
+68
-0
68 additions, 0 deletions
luennot/live13/src/kanta/HetuTarkistus.java
luennot/live13/src/kerho/Jasen.java
+4
-2
4 additions, 2 deletions
luennot/live13/src/kerho/Jasen.java
with
72 additions
and
2 deletions
luennot/live13/src/kanta/HetuTarkistus.java
0 → 100644
+
68
−
0
View file @
69a60e3b
package
kanta
;
/**
* Luokka henkilötunnuksen tarkistamiseksi
* @author vesal
* @version 9.1.2011
*
*/
public
class
HetuTarkistus
{
/** Hetuun kelpaavat tarkistusmerkit järjestyksessä */
// 0123456789012345678901234567890
public
static
final
String
TARKISTUSMERKIT
=
"0123456789ABCDEFHJKLMNPRSTUVWXY"
;
/**
* Palauttaa mikä olisi hetun tarkistumerkki. Tuotava parametrinä
* laillista muotoa oleva hetu, josta mahdollisesti tarkistumerkki
* puuttuu.
* @param hetu tutkittava hetu
* @return hetun tarkistusmerkki
* @example
* <pre name="test">
* hetunTarkistusMerkki("121212-222") === 'N';
* hetunTarkistusMerkki("121212-222S") === 'N';
* hetunTarkistusMerkki("121212-222N") === 'N';
* hetunTarkistusMerkki("121212-231Y") === 'Y';
* hetunTarkistusMerkki("311212-2317") === '7';
* hetunTarkistusMerkki("311212-2317XY") === '7'; // vaikka on liikaa merkkejä
* hetunTarkistusMerkki("999999-9999XY") === 'F'; // vaikka on pvm väärin
* hetunTarkistusMerkki("12121A-222S") === 'N'; #THROWS NumberFormatException
* hetunTarkistusMerkki("12121A-22") === 'N'; #THROWS StringIndexOutOfBoundsException
* hetunTarkistusMerkki("121") === 'N'; #THROWS StringIndexOutOfBoundsException
* </pre>
*/
public
static
char
hetunTarkistusMerkki
(
String
hetu
)
{
String
pvm
=
hetu
.
substring
(
0
,
6
);
String
yksilo
=
hetu
.
substring
(
7
,
10
);
long
luku
=
Long
.
parseLong
(
pvm
+
yksilo
);
int
jakojaannos
=
(
int
)(
luku
%
31L
);
return
TARKISTUSMERKIT
.
charAt
(
jakojaannos
);
}
/**
* Arvotaan satunnainen kokonaisluku välille [ala,yla]
* @param ala arvonnan alaraja
* @param yla arvonnan yläraja
* @return satunnainen luku väliltä [ala,yla]
*/
public
static
int
rand
(
int
ala
,
int
yla
)
{
double
n
=
(
yla
-
ala
)*
Math
.
random
()
+
ala
;
return
(
int
)
Math
.
round
(
n
);
}
/**
* Arvotaan satunnainen henkilötunnus, joka täyttää hetun ehdot
* @return satunnainen laillinen henkilötunnus
*/
public
static
String
arvoHetu
()
{
String
apuhetu
=
String
.
format
(
"%02d"
,
rand
(
1
,
28
))
+
String
.
format
(
"%02d"
,
rand
(
1
,
12
))
+
String
.
format
(
"%02d"
,
rand
(
0
,
99
))
+
"-"
+
String
.
format
(
"%03d"
,
rand
(
1
,
999
));
return
apuhetu
+
hetunTarkistusMerkki
(
apuhetu
);
}
}
This diff is collapsed.
Click to expand it.
luennot/live13/src/kerho/Jasen.java
+
4
−
2
View file @
69a60e3b
...
...
@@ -3,6 +3,8 @@ package kerho;
import
java.io.OutputStream
;
import
java.io.PrintStream
;
import
kanta.HetuTarkistus
;
/**
* Kopioi CRC-kortin sisältö
* @author vesal
...
...
@@ -96,8 +98,8 @@ public class Jasen {
*/
public
void
vastaaAkuAnkka
()
{
nimi
=
"Ankka Aku "
;
hetu
=
"1212112-121A"
;
katuosoite
=
"Paratiisitie
13"
;
hetu
=
HetuTarkistus
.
arvoHetu
()
;
katuosoite
=
"Paratiisitie
"
+
HetuTarkistus
.
rand
(
1
,
40
)
;
postinumero
=
"12345"
;
postiosoite
=
"ANKKALINNA"
;
kotipuhelin
=
"12-1234"
;
...
...
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