Update Caesar.java

This commit is contained in:
khalil2535 2018-04-11 22:01:20 +03:00
parent 8b243667d7
commit 9060d2da29

View File

@ -69,12 +69,12 @@ public class Caesar {
final int length = encryptedMessage.length(); final int length = encryptedMessage.length();
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
char current = encryptedMessage.charAt(i); char current = encryptedMessage.charAt(i);
if (current >= 'A' && current <= 'Z') { if (IsCapitalLatinLetter(current)) {
current -= shift; current -= shift;
decoded += (char) (current < 'A' ? current + 26 : current);// 26 = number of latin letters decoded += (char) (current < 'A' ? current + 26 : current);// 26 = number of latin letters
} else if (current >= 'a' && current <= 'z') { } else if (IsSmallLatinLetter(current)) {
current -= shift; current -= shift;
decoded += (char) (current < 'a' ? current + 26 : current);// 26 = number of latin letters decoded += (char) (current < 'a' ? current + 26 : current);// 26 = number of latin letters
@ -86,6 +86,24 @@ public class Caesar {
return decoded; return decoded;
} }
/**
*
* @param c
* @return true if character is capital Latin letter or false for others
*/
private static boolean IsCapitalLatinLetter(char c) {
return c >= 'A' && c <= 'Z';
}
/**
*
* @param c
* @return true if character is small Latin letter or false for others
*/
private static boolean IsSmallLatinLetter(char c) {
return c >= 'a' && c <= 'z';
}
/** /**
* *
* @deprecated TODO remove main and make JUnit Testing * @deprecated TODO remove main and make JUnit Testing
@ -99,11 +117,14 @@ public class Caesar {
int shift = input.nextInt() % 26; int shift = input.nextInt() % 26;
System.out.println("(E)ncode or (D)ecode ?"); System.out.println("(E)ncode or (D)ecode ?");
char choice = input.next().charAt(0); char choice = input.next().charAt(0);
if (choice == 'E' || choice == 'e') { switch (choice) {
System.out.println("ENCODED MESSAGE IS \n" + encode(message, shift)); //send our function to handle case 'E':
} case 'e':
if (choice == 'D' || choice == 'd') { System.out.println("ENCODED MESSAGE IS \n" + encode(message, shift)); //send our function to handle
System.out.println("DECODED MESSAGE IS \n" + decode(message, shift)); break;
case 'D':
case 'd':
System.out.println("DECODED MESSAGE IS \n" + decode(message, shift));
} }
} }