From 9060d2da29b8312f43f3e4b0841caa24a0558a35 Mon Sep 17 00:00:00 2001 From: khalil2535 Date: Wed, 11 Apr 2018 22:01:20 +0300 Subject: [PATCH] Update Caesar.java --- ciphers/Caesar.java | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/ciphers/Caesar.java b/ciphers/Caesar.java index b9f8e3f4..d646f826 100644 --- a/ciphers/Caesar.java +++ b/ciphers/Caesar.java @@ -69,12 +69,12 @@ public class Caesar { final int length = encryptedMessage.length(); for (int i = 0; i < length; i++) { char current = encryptedMessage.charAt(i); - if (current >= 'A' && current <= 'Z') { + if (IsCapitalLatinLetter(current)) { current -= shift; decoded += (char) (current < 'A' ? current + 26 : current);// 26 = number of latin letters - } else if (current >= 'a' && current <= 'z') { + } else if (IsSmallLatinLetter(current)) { current -= shift; decoded += (char) (current < 'a' ? current + 26 : current);// 26 = number of latin letters @@ -86,6 +86,24 @@ public class Caesar { 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 @@ -99,11 +117,14 @@ public class Caesar { int shift = input.nextInt() % 26; System.out.println("(E)ncode or (D)ecode ?"); char choice = input.next().charAt(0); - if (choice == 'E' || choice == 'e') { - System.out.println("ENCODED MESSAGE IS \n" + encode(message, shift)); //send our function to handle - } - if (choice == 'D' || choice == 'd') { - System.out.println("DECODED MESSAGE IS \n" + decode(message, shift)); + switch (choice) { + case 'E': + case 'e': + System.out.println("ENCODED MESSAGE IS \n" + encode(message, shift)); //send our function to handle + break; + case 'D': + case 'd': + System.out.println("DECODED MESSAGE IS \n" + decode(message, shift)); } }