-
Notifications
You must be signed in to change notification settings - Fork 47
Khramov 2tasks complete, Change pom.xml #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package com.javarush.khramov; | ||
|
|
||
| import java.util.HashMap; | ||
|
|
||
| public class Alphabet { | ||
|
|
||
| public final int hashEnAlphabet(String letter) { | ||
|
|
||
| HashMap<String, Integer> enAlphabet = new HashMap<>(); | ||
| enAlphabet.put("a", 1); enAlphabet.put("b", 2); enAlphabet.put("c", 3); | ||
| enAlphabet.put("d", 4); enAlphabet.put("e", 5); enAlphabet.put("f", 6); | ||
| enAlphabet.put("g", 7); enAlphabet.put("h", 8); enAlphabet.put("i", 9); | ||
| enAlphabet.put("j", 10); enAlphabet.put("k", 11); enAlphabet.put("l", 12); | ||
| enAlphabet.put("m", 13); enAlphabet.put("n", 14); enAlphabet.put("o", 15); | ||
| enAlphabet.put("p", 16); enAlphabet.put("q", 17); enAlphabet.put("r", 18); | ||
| enAlphabet.put("s", 19); enAlphabet.put("t", 20); enAlphabet.put("u", 21); | ||
| enAlphabet.put("v", 22); enAlphabet.put("w", 23); enAlphabet.put("x", 24); | ||
| enAlphabet.put("y", 25); enAlphabet.put("z", 26); | ||
|
|
||
| return enAlphabet.get(letter); | ||
| } | ||
|
|
||
| public final String letterEnAlphabet(int hash){ | ||
|
|
||
| HashMap<Integer, String> enAlphabet = new HashMap<>(); | ||
| enAlphabet.put(1, "a"); enAlphabet.put(2, "b"); enAlphabet.put(3, "c"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ну и так везде |
||
| enAlphabet.put(4, "d"); enAlphabet.put(5, "e"); enAlphabet.put(6, "f"); | ||
| enAlphabet.put(7, "g"); enAlphabet.put(8, "h"); enAlphabet.put(9, "i"); | ||
| enAlphabet.put(10, "j"); enAlphabet.put(11, "k"); enAlphabet.put(12, "l"); | ||
| enAlphabet.put(13, "m"); enAlphabet.put(14, "n"); enAlphabet.put(15, "o"); | ||
| enAlphabet.put(16, "p"); enAlphabet.put(17, "q"); enAlphabet.put(18, "r"); | ||
| enAlphabet.put(19, "s"); enAlphabet.put(20, "t"); enAlphabet.put(21, "u"); | ||
| enAlphabet.put(22, "v"); enAlphabet.put(23, "w"); enAlphabet.put(24, "x"); | ||
| enAlphabet.put(25, "y"); enAlphabet.put(26, "z"); | ||
|
|
||
| return enAlphabet.get(hash); | ||
| } | ||
|
|
||
| public final boolean booleanEnAlphabet(String letter) { | ||
|
|
||
| HashMap<String, Integer> enAlphabet = new HashMap<>(); | ||
| enAlphabet.put("a", 1); enAlphabet.put("b", 2); enAlphabet.put("c", 3); | ||
| enAlphabet.put("d", 4); enAlphabet.put("e", 5); enAlphabet.put("f", 6); | ||
| enAlphabet.put("g", 7); enAlphabet.put("h", 8); enAlphabet.put("i", 9); | ||
| enAlphabet.put("j", 10); enAlphabet.put("k", 11); enAlphabet.put("l", 12); | ||
| enAlphabet.put("m", 13); enAlphabet.put("n", 14); enAlphabet.put("o", 15); | ||
| enAlphabet.put("p", 16); enAlphabet.put("q", 17); enAlphabet.put("r", 18); | ||
| enAlphabet.put("s", 19); enAlphabet.put("t", 20); enAlphabet.put("u", 21); | ||
| enAlphabet.put("v", 22); enAlphabet.put("w", 23); enAlphabet.put("x", 24); | ||
| enAlphabet.put("y", 25); enAlphabet.put("z", 26); | ||
|
|
||
| return enAlphabet.containsKey(letter); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package com.javarush.khramov; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class CipherAndUnCipher { | ||
| FileManager fileManager = new FileManager(); | ||
| Alphabet alphabet = new Alphabet(); | ||
|
|
||
| public void cipherList(List<String> charList, int key, String encryptedFileAddress) { | ||
|
|
||
| List<String> cipherList = new ArrayList<>(); | ||
|
|
||
| for (var i = 0; i < charList.size(); i++) { | ||
| String x = charList.get(i).toLowerCase(); | ||
|
|
||
| if (alphabet.booleanEnAlphabet(x) == true) { | ||
| int a = alphabet.hashEnAlphabet(x); | ||
| if ((a + key) > 26) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Числам и строкам лучше присваивать понятные константные имена |
||
| cipherList.add(alphabet.letterEnAlphabet((a + key) - 26)); | ||
| } else { | ||
| cipherList.add(alphabet.letterEnAlphabet(a + key)); | ||
| } | ||
| } else { | ||
| cipherList.add(x); | ||
| } | ||
| } | ||
| fileManager.writeFile(cipherList, encryptedFileAddress); | ||
| } | ||
|
|
||
| public void unCipherList(List<String> charList, int key, String encryptedFileAddress) { | ||
| List<String> unCipherList = new ArrayList<>(); | ||
|
|
||
|
|
||
| for (var i = 0; i < charList.size(); i++) { | ||
| String x = charList.get(i).toLowerCase(); | ||
|
|
||
| if (alphabet.booleanEnAlphabet(x) == true) { | ||
| int a = alphabet.hashEnAlphabet(x); | ||
| if ((a - key) < 1) { | ||
| unCipherList.add(alphabet.letterEnAlphabet(26 + (a - key))); | ||
| } else { | ||
| unCipherList.add(alphabet.letterEnAlphabet(a - key)); | ||
| } | ||
| } else { | ||
| unCipherList.add(x); | ||
| } | ||
| } | ||
| fileManager.writeFile(unCipherList, encryptedFileAddress); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package com.javarush.khramov; | ||
|
|
||
| import java.io.BufferedWriter; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Paths; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class FileManager { | ||
|
|
||
| public List<String> readingFile(String fileAddress) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут всё хорошо но все же не очень понятно почему именно лист |
||
| List<String> charList = new ArrayList<>(); | ||
|
|
||
| try { | ||
| byte[] bytes = Files.readAllBytes(Paths.get(fileAddress)); | ||
|
|
||
| for (byte b : bytes) { | ||
| char character = (char) b; | ||
| charList.add(String.valueOf(character)); | ||
| } | ||
|
|
||
| System.out.println("Read characters: " + charList.size()); | ||
| } catch (IOException e) { | ||
| System.err.println("Error reading file: " + e.getMessage()); | ||
| } | ||
|
|
||
| return charList; | ||
| } | ||
|
|
||
| public void writeFile(List<String> list, String encryptedFileAddress) { | ||
|
|
||
| try (BufferedWriter writer = new BufferedWriter(new FileWriter(encryptedFileAddress))) { | ||
| for (String line : list) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ага вот понятно почему лист. Лучше всё-таки было оставить его обычным стрингом |
||
| writer.write(line); | ||
| } | ||
| System.out.println("ready! "); | ||
|
|
||
| } catch (IOException e) { | ||
| System.err.println("Error writing file: " + e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package com.javarush.khramov; | ||
|
|
||
| import java.io.InputStream; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
|
|
||
| public class MainApp { | ||
| public static void main(String[] args) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Чаще всего в методе main создают все компоненты приложения и/или собирают это приложение, а его запуск выполняется в каком-то понятном методе например старт |
||
| while (true) { | ||
| System.out.println("Welcome to the program!"); | ||
| System.out.println("Select mode:"); | ||
| System.out.println("1. Encrypt text;"); | ||
| System.out.println("2. Decipher the text;"); | ||
| System.out.println("3. Exit."); | ||
|
|
||
| InputStream stream = System.in; | ||
| Scanner console = new Scanner(stream); | ||
| byte input = console.nextByte(); | ||
|
|
||
| CipherAndUnCipher cipherAndUnCipher = new CipherAndUnCipher(); | ||
| FileManager fileManager = new FileManager(); | ||
|
|
||
|
|
||
| if (input == 1) { | ||
| String fileAddress = PathAndKey.fileAddress(); | ||
| String encryptedFileAddress = PathAndKey.encryptedFileAddress(); | ||
| int key = PathAndKey.key(); | ||
| List<String> list = fileManager.readingFile(fileAddress); | ||
|
|
||
| cipherAndUnCipher.cipherList(list, key, encryptedFileAddress); | ||
|
|
||
| } else if (input == 2) { | ||
| String fileAddress = PathAndKey.fileAddress(); | ||
| String encryptedFileAddress = PathAndKey.encryptedFileAddress(); | ||
| int key = PathAndKey.key(); | ||
| List<String> list = fileManager.readingFile(fileAddress); | ||
|
|
||
| cipherAndUnCipher.unCipherList(list, key, encryptedFileAddress); | ||
|
|
||
| } else if (input == 3) { | ||
| System.out.println("Good Bye!"); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package com.javarush.khramov; | ||
|
|
||
| import java.io.InputStream; | ||
| import java.util.Scanner; | ||
|
|
||
| public class PathAndKey { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если в классе все методы статические то ему делают приватный конструктор |
||
|
|
||
|
|
||
| public static String fileAddress() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Когда методы статические их трудно тестировать |
||
| InputStream stream = System.in; | ||
| Scanner console = new Scanner(stream); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не очень хорошая идея создавать сканер при каждом обращении к методу |
||
|
|
||
| System.out.println("Specify the path to the text file:"); | ||
| String fileAddress = console.nextLine(); | ||
|
|
||
| return fileAddress; | ||
| } | ||
|
|
||
|
|
||
| public static String encryptedFileAddress() { | ||
| InputStream stream = System.in; | ||
| Scanner console = new Scanner(stream); | ||
|
|
||
| System.out.println("Specify the path where to write the cipher:"); | ||
| String encryptedFileAddress = console.nextLine(); | ||
|
|
||
| return encryptedFileAddress; | ||
| } | ||
|
|
||
|
|
||
| public static int key() { | ||
| InputStream stream = System.in; | ||
| Scanner console = new Scanner(stream); | ||
|
|
||
| System.out.println("Enter the key for encoding:"); | ||
| int key; | ||
| try { | ||
| key = console.nextInt(); | ||
| } catch (Exception e) { | ||
| System.err.println("Error, check that the key is a number from 0 to 25."); | ||
| key = console.nextInt(); | ||
| } | ||
|
|
||
|
|
||
| while (true) { | ||
|
|
||
| if (key >= 0 && key <= 25) { | ||
| break; | ||
| } else if (key < 0) { | ||
| System.out.println("The key can't be negative."); | ||
| System.out.println("Enter new key for encoding:"); | ||
| key = console.nextInt(); | ||
| } else if (key > 25) { | ||
| System.out.println("The key can't be greater than 25."); | ||
| System.out.println("Enter new key for encoding:"); | ||
| key = console.nextInt(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| return key; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Тут лучше сделать строчку а потом циклом из неё заполнить карту