-
Notifications
You must be signed in to change notification settings - Fork 7
/
IdGenerator.java
69 lines (55 loc) · 1.69 KB
/
IdGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.Scanner;
public class IdGenerator {
private static Scanner input;
public static void main(String[] args) {
input = new Scanner(System.in);
System.out.print("Enter 1st Name : ");
String firstName = input.nextLine();
System.out.print("Enter 2nd Name : ");
String lastName = input.nextLine();
System.out.print("Enter the PIN : ");
int pin = input.nextInt();
System.out.print("Enter Index Number : ");
int number = input.nextInt();
System.out.println("System Generated Id -> " + idGenatorAlgorithm(firstName, lastName, pin, number));
}
private static String idGenatorAlgorithm(String firstName, String lastName, int pin, int number) {
String shortName = "";
String longName = "";
String pinNo = pin + "";
char rl;
char lr;
String result;
if(firstName.length() < lastName.length()) {
shortName = toggleCaseCharacters(firstName);
longName = toggleCaseCharacters(lastName);
}
else if (firstName.length() > lastName.length()) {
shortName = toggleCaseCharacters(lastName);
longName = toggleCaseCharacters(firstName);
}
else if (firstName.length() == lastName.length()) {
shortName = toggleCaseCharacters(firstName);
longName = toggleCaseCharacters(lastName);
}
rl = pinNo.charAt(number-1);
lr = pinNo.charAt(pinNo.length() - number);
result = longName.charAt(0) + shortName + rl + lr;
return result;
}
private static String toggleCaseCharacters(String name) {
String newName = "";
for(int i=0; i<name.length(); i++) {
char ch = name.charAt(i);
if(ch>= 65 && ch<= 90) {
ch += 32;
newName += ch;
}
else if (ch>= 97 && ch<= 122) {
ch -= 32;
newName += ch;
}
}
return newName;
}
}