@@ -9,59 +9,44 @@ public void setPattern(KeyPattern pattern) {
99 this .pattern = pattern ;
1010 }
1111
12- public void generate () {
12+ public String generate () {
1313 String [] sections = pattern .getKey_pattern ().split ("-" );
1414
1515 int security_numbers = (int ) (Math .ceil (sections .length / 2.0 ));
1616 int maxAscii = 0 ;
1717 int ascii_multiplier = 0 ;
18+
1819 for (String section : sections ) {
1920 maxAscii += section .length ();
2021 for (int i = 0 ; i < section .length (); i ++){
2122 int ascii_char = (int ) section .charAt (i );
22- if (ascii_char > ascii_multiplier ){
23- ascii_multiplier = ascii_char ;
24- }
23+ if (ascii_char > ascii_multiplier ) ascii_multiplier = ascii_char ;
2524 }
2625 }
2726 maxAscii *= ascii_multiplier ;
28- try {
29- if (pattern .getAscii_count () > maxAscii ) throw new AsciiException (maxAscii , pattern .getAscii_count ());
30-
31- }catch (AsciiException e ){
32- e .printStackTrace ();
33- }
27+ try {if (pattern .getAscii_count () > maxAscii ) throw new AsciiException (maxAscii , pattern .getAscii_count ());}catch (AsciiException e ){e .printStackTrace ();}
3428
3529 String key = "" ;
3630 while (true ) {
3731 int char_counter = 0 ;
3832 int counter = 0 ;
3933 for (String section : sections ) {
4034 counter ++;
41- for (int i = 0 ; i < section .length (); i ++) {
42- key += pattern .getAlphabet_pattern ().charAt ((int ) Math .round (Math .random () * (pattern .getAlphabet_pattern ().length () - 1 )));
43- }
44- if (counter != sections .length )
45- key += "-" ;
35+ for (int i = 0 ; i < section .length (); i ++) key += pattern .getAlphabet_pattern ().charAt ((int ) Math .round (Math .random () * (pattern .getAlphabet_pattern ().length () - 1 )));
36+
37+ if (counter != sections .length ) key += "-" ;
4638 }
4739 counter = 0 ;
4840
49- for (int i = 0 ; i < key .length (); i ++) {
50- if (key .charAt (i ) == pattern .getControl_number ()) {
51- char_counter ++;
52- }
53- }
41+ for (int i = 0 ; i < key .length (); i ++) if (key .charAt (i ) == pattern .getControl_number ()) char_counter ++;
5442
5543 if (security_numbers <= char_counter ) {
5644 int ascii_count = 0 ;
5745 for (int i = 0 ; i < key .length (); i ++) {
58- if (key .charAt (i ) != '-' ) {
59- ascii_count += (int ) key .charAt (i );
60- }
46+ if (key .charAt (i ) != '-' ) ascii_count += (int ) key .charAt (i );
6147 }
6248 if (ascii_count == pattern .getAscii_count ()) {
63- System .out .println (key );
64- break ;
49+ return key ;
6550 }
6651 }
6752 key = "" ;
0 commit comments