Skip to content

Commit 175def5

Browse files
authored
Update MonoAlphabetic.java
1 parent f021dbb commit 175def5

File tree

1 file changed

+56
-42
lines changed

1 file changed

+56
-42
lines changed
Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,92 @@
1+
package com.thealgorithms.ciphers;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.stream.Stream;
6+
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
10+
111
public final class MonoAlphabetic {
12+
13+
// Private constructor to prevent instantiation of utility class
214
private MonoAlphabetic() {
315
throw new UnsupportedOperationException("Utility class");
416
}
517

18+
// Encryption method
619
public static String encrypt(String data, String key) {
720
int idx;
821
char c;
9-
StringBuffer sb = new StringBuffer(data.toUpperCase());
22+
StringBuilder sb = new StringBuilder(data.toUpperCase());
1023

1124
for (int i = 0; i < sb.length(); i++) {
12-
idx = sb.charAt(i) - 65;
13-
c = key.charAt(idx);
14-
sb.setCharAt(i, c);
25+
idx = sb.charAt(i) - 65; // Subtract ASCII value of 'A' to get index
26+
c = key.charAt(idx); // Find the character at the corresponding key position
27+
sb.setCharAt(i, c); // Replace with the key character
1528
}
16-
return new String(sb);
29+
return sb.toString();
1730
}
1831

32+
// Decryption method
1933
public static String decrypt(String data, String key) {
2034
int idx;
2135
char c;
22-
StringBuffer sb = new StringBuffer(data.toUpperCase());
36+
StringBuilder sb = new StringBuilder(data.toUpperCase());
2337

2438
for (int i = 0; i < sb.length(); i++) {
25-
c = sb.charAt(i);
26-
idx = getIndex(c, key);
27-
c = (char) (idx + 65);
28-
sb.setCharAt(i, c);
39+
c = sb.charAt(i); // Get the character from encrypted data
40+
idx = getIndex(c, key); // Get the corresponding index from the key
41+
c = (char) (idx + 65); // Convert index back to character
42+
sb.setCharAt(i, c); // Replace with the original character
2943
}
30-
return new String(sb);
44+
return sb.toString();
3145
}
3246

47+
// Helper method to get index of a character in the key
3348
public static int getIndex(char c, String key) {
3449
for (int i = 0; i < key.length(); i++) {
3550
if (key.charAt(i) == c) {
36-
return i;
51+
return i; // Return the index if the character matches
3752
}
3853
}
39-
return -1;
54+
return -1; // Return -1 if character not found (should not happen for valid inputs)
4055
}
41-
}
42-
43-
// JUnit Tests for MonoAlphabetic Cipher
44-
import org.junit.jupiter.api.DisplayName;
45-
import org.junit.jupiter.params.ParameterizedTest;
46-
import org.junit.jupiter.params.provider.CsvSource;
47-
import static org.junit.jupiter.api.Assertions.*;
4856

49-
public class MonoAlphabeticTest {
57+
// *********** Unit Test Section **************
5058

51-
private final String key = "MNBVCXZLKJHGFDSAPOIUYTREWQ";
59+
// Method to provide test data for encryption
60+
private static Stream<Arguments> provideEncryptionData() {
61+
String key = "MNBVCXZLKJHGFDSAPOIUYTREWQ";
62+
return Stream.of(
63+
// Input data, key, expected encrypted output
64+
Arguments.of("HELLO", key, "GFSSD"),
65+
Arguments.of("JAVA", key, "MZSM")
66+
);
67+
}
5268

69+
// Test for encryption
5370
@ParameterizedTest
54-
@DisplayName("Encrypt Test with MonoAlphabetic Cipher")
55-
@CsvSource({
56-
"HELLO, DLZZI",
57-
"WORLD, XMFLD",
58-
"JAVA, HBWB",
59-
"OPENAI, IUPNMW"
60-
})
61-
void testEncrypt(String plainText, String expectedCipherText) {
62-
String encrypted = MonoAlphabetic.encrypt(plainText, key);
63-
assertEquals(expectedCipherText, encrypted, "Encryption failed for input: " + plainText);
71+
@MethodSource("provideEncryptionData")
72+
public void testEncrypt(String data, String key, String expected) {
73+
assertEquals(expected, MonoAlphabetic.encrypt(data, key));
74+
}
75+
76+
// Method to provide test data for decryption
77+
private static Stream<Arguments> provideDecryptionData() {
78+
String key = "MNBVCXZLKJHGFDSAPOIUYTREWQ";
79+
return Stream.of(
80+
// Encrypted data, key, expected decrypted output
81+
Arguments.of("GFSSD", key, "HELLO"),
82+
Arguments.of("MZSM", key, "JAVA")
83+
);
6484
}
6585

86+
// Test for decryption
6687
@ParameterizedTest
67-
@DisplayName("Decrypt Test with MonoAlphabetic Cipher")
68-
@CsvSource({
69-
"DLZZI, HELLO",
70-
"XMFLD, WORLD",
71-
"HBWB, JAVA",
72-
"IUPNMW, OPENAI"
73-
})
74-
void testDecrypt(String cipherText, String expectedPlainText) {
75-
String decrypted = MonoAlphabetic.decrypt(cipherText, key);
76-
assertEquals(expectedPlainText, decrypted, "Decryption failed for input: " + cipherText);
88+
@MethodSource("provideDecryptionData")
89+
public void testDecrypt(String data, String key, String expected) {
90+
assertEquals(expected, MonoAlphabetic.decrypt(data, key));
7791
}
7892
}

0 commit comments

Comments
 (0)