Jump to content
razvandragos29

Cnp generator[Java]

Recommended Posts

Posted

Salut am realizat un generator de cnp nu stiu daca este "bun" pentru ceea ce am eu nevoie este de ajuns .Acesta este codul

 import java.text.DecimalFormat;

import java.util.Calendar;

import java.util.GregorianCalendar;

import java.util.Random;

import java.util.Scanner;

public class Cnp {

public static void main(String args[]) {

int sexPersoana = 0;

Scanner introduAn = new Scanner(System.in);

System.out.println("Introduceti anul: ");

int an = introduAn.nextInt();

Scanner introduLuna = new Scanner(System.in);

System.out.println("Introduceti luna: ");

int luna = introduLuna.nextInt() - 1;

Scanner introduZiua = new Scanner(System.in);

System.out.println("Introduceti ziua : ");

int ziua = introduZiua.nextInt();

System.out.println("Anul , luna si ziua au fost introduse ");

System.out.println();

Calendar ziuaNasterii = new GregorianCalendar();

ziuaNasterii.set(an, luna, ziua);

System.out.println("Ziua nasterii este " + ziuaNasterii.getTime());

Calendar cal1900 = new GregorianCalendar();

cal1900.set(1900, 0, 1);

cal1900.clear(cal1900.MILLISECOND);

Calendar cal1999 = new GregorianCalendar();

cal1999.set(1999, 11, 31);

cal1999.clear(cal1999.MILLISECOND);

Calendar cal1800 = new GregorianCalendar();

cal1800.set(1800, 0, 1);

cal1800.clear(cal1800.MILLISECOND);

Calendar cal1899 = new GregorianCalendar();

cal1899.set(1899, 11, 31);

cal1899.clear(cal1899.MILLISECOND);

Calendar cal2000 = new GregorianCalendar();

cal2000.set(2000, 0, 1);

cal2000.clear(cal2000.MILLISECOND);

Calendar cal2999 = new GregorianCalendar();

cal2999.set(2999, 11, 31);

cal2999.clear(cal2999.MILLISECOND);

Scanner sex = new Scanner(System.in);

System.out.println("Introduceti sexul: ");

String sexul = sex.nextLine();

System.out.println();

if (ziuaNasterii.getTime().after(cal1900.getTime())

&& ziuaNasterii.getTime().before(cal1999.getTime())) {

switch (sexul) {

case "masculin":

sexPersoana = 1;

break;

case "feminin":

sexPersoana = 2;

break;

}

}

if (ziuaNasterii.getTime().after(cal1800.getTime())

&& ziuaNasterii.getTime().before(cal1899.getTime())) {

switch (sexul) {

case "masculin":

sexPersoana = 3;

break;

case "feminin":

sexPersoana = 4;

break;

}

}

if (ziuaNasterii.getTime().after(cal2000.getTime())

&& ziuaNasterii.getTime().before(cal2999.getTime())) {

switch (sexul) {

case "masculin":

sexPersoana = 5;

break;

case "feminin":

sexPersoana = 6;

break;

}

}

System.out.println("Sexul persoanei este " + sexPersoana);

DecimalFormat formater = new DecimalFormat("00");

int getAn = ziuaNasterii.get(ziuaNasterii.YEAR) % 100;

String getAnn = formater.format(getAn);

int getZiua = ziuaNasterii.get(ziuaNasterii.DAY_OF_MONTH);

String getZiuaa = formater.format(getZiua);

int getLuna = ziuaNasterii.get(ziuaNasterii.MONTH) + 1;

String getLunaa = formater.format(getLuna);

System.out.println("S : " + sexPersoana + " AA " + getAnn + " LL "

+ getLunaa + " ZZ " + getZiuaa);

Scanner introduJudet = new Scanner(System.in);

System.out.println("Introduceti judetul : ");

String judet = introduJudet.nextLine();

System.out.println();

int nrJudet = 0;

switch (judet) {

case "GALATI":

nrJudet = 17;

break;

case "BRAILA":

nrJudet = 9;

break;

}

String getJudet = formater.format(nrJudet);

System.out.println("S : " + sexPersoana + " AA " + getAnn + " LL "

+ getLunaa + " ZZ " + getZiuaa + " JJ " + getJudet);

Random random = new Random();

int min = 1;

int max = 999;

int nrGenerat = random.nextInt(max - min) + min;

DecimalFormat formatare = new DecimalFormat("000");

String getNNN = formatare.format(nrGenerat);

System.out.println("S : " + sexPersoana + " AA " + getAnn + " LL "

+ getLunaa + " ZZ " + getZiuaa + " JJ " + getJudet + " NNN "

+ getNNN);

System.out.println();

// // Inmultire pt cifra de control ///////

int S = sexPersoana * 2;

int AA1 = (getAn / 10) * 7;

int AA2 = (getAn % 10) * 9;

int LL1 = (getLuna / 10) * 1;

int LL2 = (getLuna % 10) * 4;

int ZZ1 = (getZiua / 10) * 6;

int ZZ2 = (getZiua % 10) * 3;

int JJ1 = (nrJudet / 10) * 5;

int JJ2 = (nrJudet % 10) * 8;

int NNN1 = (nrGenerat / 100) * 2;

int NNN2 = (nrGenerat / 10) * 7;

int NNN3 = (nrGenerat % 10) * 9;

int cifraControl = S + AA1 + AA2 + LL1 + LL2 + ZZ1 + ZZ2 +JJ1 + JJ2 + NNN1 + NNN2 + NNN3 ;

int cifraControlFinal;

int rest = cifraControl % 11;

System.out.println("restul este " +rest);

if(rest == 10) {

cifraControlFinal = 1;

}else {

cifraControlFinal = rest;

}

String controlFinal = Integer.toString(cifraControlFinal);

StringBuilder cnp = new StringBuilder("").append(sexPersoana)

.append(getAnn).append(getLunaa).append(getZiuaa)

.append(getJudet).append(getNNN).append(controlFinal);

System.out.println(cnp);

}

}

Daca aveti vreo metoda prin care sa imbunatesc acest generator astept parerile voastre .Este scris destul de dezordonat din pacate :(

  • Upvote 1
Posted (edited)

Uite unul si in C++ facut de modoran e destul de vechi.

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cmath>

using namespace std;

string calculate_cnp(string cnptemporar);
void IntToString(int i, string & s);
bool an_bisect(int year);

int main(int argc, char *argv[]){

srand( time(NULL) );

time_t rawtime;
char buffer [3];

time ( &rawtime );
strftime (buffer,3,"%y",localtime ( &rawtime ));
string s_an = buffer;
int year = atoi(s_an.c_str());

year += 100;
year -= 25;
s_an.clear();


int sex = rand() %2; sex++;
int an = rand() %10; an += year;
int luna = rand() %12; luna++;
int zi = 0;

switch(luna){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
zi = rand() %31; zi++;
break;
case 4:
case 6:
case 9:
case 11:
zi = rand() %30; zi++;
break;
case 2:
if(an_bisect(an)){
zi = rand() %29; zi++;
} else {
zi = rand() %28; zi++;
}
break;
default:
break;
}
int judet = rand() %52; judet++;

while(judet >= 47 && judet <= 50){
judet = rand() %52; judet++;
}

int registru = rand() %999; registru++;

string cnptemporar;
string buf;
IntToString(sex, buf);
cnptemporar += buf;
buf.clear();
IntToString(an, buf);
cnptemporar += buf;
buf.clear();
if(luna < 10){
buf = "0";
cnptemporar += buf;
buf.clear();
}
IntToString(luna, buf);
cnptemporar += buf;
buf.clear();

if(zi < 10){
buf = "0";
cnptemporar += buf;
buf.clear();
}
IntToString(zi, buf);
cnptemporar += buf;
buf.clear();

if(judet < 10){
buf = "0";
cnptemporar += buf;
buf.clear();
}
IntToString(judet, buf);
cnptemporar += buf;
buf.clear();

if(registru < 10){
buf = "00";
cnptemporar += buf;
buf.clear();
} else if(registru < 100 && registru > 10){
buf = "0";
cnptemporar += buf;
buf.clear();
}
IntToString(registru, buf);
cnptemporar += buf;
buf.clear();

buf = calculate_cnp(cnptemporar);
printf("Generated CNP: %s\n", buf.c_str());

return 0;
}

string calculate_cnp(string cnptemporar){
int x = 0;
string sumaverificare("279146358279");
string s1;
string s2;
string buf;
string cnp = cnptemporar;
int i1 = 0;
int i2 = 0;
int rezadunare = 0;
while(x < 12){
s1 = sumaverificare.substr(x,1);
s2 = cnptemporar.substr(x,1);
i1 = atoi(s1.c_str());
i2 = atoi(s2.c_str());
s1.clear();
s2.clear();
rezadunare = rezadunare + (i1 * i2);
x++;
}

int rest = rezadunare / 11;

int cifra = rezadunare - (11 * rest);
if(cifra == 10){
cifra = 1;
}

//printf("cifra = %d\n",cifra);
IntToString(cifra, buf);
cnp += buf;
buf.clear();

return cnp;
}

void IntToString(int i, string & s)
{
s = "";
if (i == 0)
{
s = "0";
return;
}
if (i < 0)
{
s += '-';
i = -i;
}
int count = log10(i);
while (count >= 0)
{
s += ('0' + i/pow(10.0, count));
i -= static_cast<int>(i/pow(10.0,count)) * static_cast<int>(pow(10.0,count));
count--;
}
}


bool an_bisect(int year)
{
return year%4 == 0 && (year %100 != 0 || year%400 == 0);
}

si inca unul in PHP Source

<?php

/**
* Generare CNP ptr anumiti parametri varsta-sex-rezident
*
* @example generareCnp(1,19)
* @return cnp Cnp aleator valid
*/
function generareCnp($sex,$varsta,$rezident=false) {
$errorMsg = 'EROARE';
$sirCiudat ='279146358279';
$strain = ($sex==9) ? true:false;
$sexVector =array('1','2','9'); //ptr amuzament
//Verificarile datelor
if(!in_array($sex,$sexVector)) {
return $errorMsg;
}
if (!is_bool($rezident)) {
return $errorMsg;
}
$an=date(Y)-$varsta;
// Daca s-a nascut inainte de 1900 $sex = 3 sau 4
if ( $an < 1900 && !$rezident && !$strain) $sex = $sex + 2;
// Daca s-a nascut dupa 2000 $sex = 5 sau 6
if ( $an > 2000 && !$rezident && !$strain) $sex = $sex + 4;
//daca este rezident
if ($rezident) $sex = $sex+6;
$luna=rand(1,12);
$zi = rand(1,31);
//daca este un copil care s-a nascut anul acesta atunci avem grija ca data generata sa nu fie mai mare decat data de azi
if ($an == date('Y')) {
$luna = rand(1,date('m'));
$ziua = rand(1,date('d'));
}

$date = new DateTime($an.'-'.$luna.'-'.$zi);

$judet=rand(1,52);
$judet=($judet<10) ? '0'.$judet : $judet;
$control = rand(1,999);
if ($control<10) {
$control='00'.$control;
}
else if ($control<100) {
$control='0'.$control;
}
$cnp = $sex . $date->format("ymd") . $judet.$control;
$suma=0;
for($i=0;$i<=11;$i++) {
$suma+=$sirCiudat[$i]*$cnp[$i];
}
$rest = $suma%11;
$cifraControl = ($rest==10) ? 1 : $rest;
$cnp.=$cifraControl;
if (strlen($cnp)!=13 ) {
return $errorMsg;
}

else return $cnp;

}


for($i=0;$i<100;$i++) {
echo generareCnp(rand(1,2),rand(0,209))."\n";
}

?>

Edited by Aerosol

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...