Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.37 KB | None | 0 0
  1. package pl.sda.gc;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.HashMap;
  6. import java.util.List;
  7.  
  8. public class CheckboardMap {
  9.  
  10.     public static void main(String[] args) {
  11.         CheckboardMap checkboardMap = new CheckboardMap();
  12.         String encoded = checkboardMap.encodeText("Ala ma kota");
  13.  
  14.         String decoded = checkboardMap.decodeText(encoded);
  15.         System.out.println(decoded);
  16.     }
  17.  
  18.     private HashMap<Character, Integer> encodeMap;
  19.     private HashMap<Integer, Character> decodeMap;
  20.  
  21.     public CheckboardMap() {
  22.         this.encodeMap = new HashMap<Character, Integer>() {{
  23.             put('q', 0);
  24.             put('w', 1);
  25.             put('e', 3);
  26.             put('t', 6);
  27.             put('y', 7);
  28.             put('u', 8);
  29.             put('i', 9);
  30.             put('o', 40);
  31.             put('p', 41);
  32.             put('a', 42);
  33.             put('s', 43);
  34.             put('d', 44);
  35.             put('f', 45);
  36.             put('g', 46);
  37.             put('h', 47);
  38.             put('j', 48);
  39.             put('k', 49);
  40.             put('l', 20);
  41.             put('z', 21);
  42.             put('x', 22);
  43.             put('c', 23);
  44.             put('v', 24);
  45.             put('b', 25);
  46.             put('n', 26);
  47.             put('m', 27);
  48.             put('ą', 28);
  49.             put('ć', 29);
  50.             put('ę', 50);
  51.             put('ł', 51);
  52.             put('ń', 52);
  53.             put('ó', 53);
  54.             put('ś', 54);
  55.             put('ż', 55);
  56.             put('ź', 56);
  57.             put('.', 57);
  58.             put(',', 58);
  59.             put('r', 59);
  60.         }};
  61.  
  62.         this.decodeMap = new HashMap<Integer, Character>() {{
  63.             put(0, 'q');
  64.             put(1, 'w');
  65.             put(3, 'e');
  66.             put(6, 't');
  67.             put(7, 'y');
  68.             put(8, 'u');
  69.             put(9, 'i');
  70.             put(40, 'o');
  71.             put(41, 'p');
  72.             put(42, 'a');
  73.             put(43, 's');
  74.             put(44, 'd');
  75.             put(45, 'f');
  76.             put(46, 'g');
  77.             put(47, 'h');
  78.             put(48, 'j');
  79.             put(49, 'k');
  80.             put(20, 'l');
  81.             put(21, 'z');
  82.             put(22, 'x');
  83.             put(23, 'c');
  84.             put(24, 'v');
  85.             put(25, 'b');
  86.             put(26, 'n');
  87.             put(27, 'm');
  88.             put(28, 'ą');
  89.             put(29, 'ć');
  90.             put(50, 'ę');
  91.             put(51, 'ł');
  92.             put(52, 'ń');
  93.             put(53, 'ó');
  94.             put(54, 'ś');
  95.             put(55, 'ż');
  96.             put(56, 'ź');
  97.             put(57, '.');
  98.             put(58, ',');
  99.             put(59, 'r');
  100.         }};
  101.     }
  102.  
  103.     public String encodeText(String toEncode) {
  104.         ArrayList<Integer> codeKey = new ArrayList<>();
  105.         codeKey.add(2);
  106.         codeKey.add(3);
  107.  
  108.         ArrayList<Integer> digits = countDigits(toEncode);
  109.  
  110.         int j = 0;
  111.         for (int i = 0; i < digits.size(); i++) {
  112.             Integer digit = digits.get(i);
  113.             int digitAndCodeKey = digit + codeKey.get(j);
  114.  
  115.             if (digitAndCodeKey >= 10) {
  116.                 digits.set(i, digitAndCodeKey % 10);
  117.             } else {
  118.                 digits.set(i, digitAndCodeKey);
  119.             }
  120.  
  121.             if (j == 1) {
  122.                 j = 0;
  123.             } else {
  124.                 j++;
  125.             }
  126.         }
  127.  
  128.         for (int i : digits) {
  129.             System.out.println(i);
  130.         }
  131.  
  132.         digits.add(codeKey.get(0));
  133.         digits.add(codeKey.get(1));
  134.  
  135.         return listIntToStrig(digits);
  136.     }
  137.  
  138.     private ArrayList<Integer> countDigits(String toEncode) {
  139.         ArrayList<Integer> digits = new ArrayList<>();
  140.         toEncode = toEncode.toLowerCase();
  141.         for (int i = 0; i < toEncode.length(); i++) {
  142.             Integer encoded = encodeMap.get(toEncode.charAt(i));
  143.             if (encoded != null) {
  144.                 if (encoded < 10) {
  145.                     digits.add(encoded);
  146.                 } else {
  147.                     digits.add(encoded / 10);
  148.                     digits.add(encoded % 10);
  149.                 }
  150.             } else {
  151.                 digits.add(toEncode.charAt(i));//literke dodajemy do inta? :<
  152.             }
  153.         }
  154.         return digits;
  155.     }
  156.  
  157.     public String listIntToStrig(ArrayList<Integer> intList) {
  158.         StringBuilder result = new StringBuilder();
  159.         for (int i = 0; i < intList.size(); i++) {
  160.             result.append(intList.get(i));
  161.         }
  162.         return result.toString();
  163.     }
  164.  
  165.     public String decodeText(String toDecode) {
  166.         ArrayList<Character> decoded = new ArrayList<>();
  167.         ArrayList<Integer> toDecodeInt = new ArrayList<>();
  168.  
  169.         for (int i = 0; i < toDecode.length() - 2; i += 2) {
  170.             int temp = toDecode.charAt(i) - '0';
  171.             int temp2 = toDecode.charAt(i + 1) - '0';
  172.             int toArray = temp * 10 + temp2;
  173.             toDecodeInt.add(toArray);
  174.         }
  175.         Integer lastKey = toDecode.charAt(toDecode.length() - 1) - '0';
  176.         Integer firstKey = toDecode.charAt(toDecode.length() - 2) - '0';
  177.  
  178.         for (Integer i : toDecodeInt) {
  179.             System.out.println(i);
  180.         }
  181.  
  182.         ArrayList<Integer> codeKey = new ArrayList<>();
  183.         codeKey.add(firstKey);
  184.         codeKey.add(lastKey);
  185.  
  186.         int j = 0;
  187.         for (int i = 0; i < toDecodeInt.size() - 2; i++) {
  188.             System.out.println(toDecodeInt.get(i));
  189.             //if(toDecodeInt.get(i)-)
  190.  
  191.             toDecodeInt.set(i, toDecodeInt.get(i) / codeKey.get(j));
  192.             System.out.println(toDecodeInt.get(i));
  193.         }
  194.  
  195.         for (int i = 0; i < toDecodeInt.size() - 3; i++) {
  196.             if (toDecodeInt.get(i) == 0 || toDecodeInt.get(i) == 1 || toDecodeInt.get(i) == 2) {
  197.                 decoded.add(decodeMap.get(i));
  198.             } else {
  199.                 int int1 = toDecodeInt.get(i);
  200.                 int int2 = toDecodeInt.get(i + 1);
  201.                 int result = int1 * 10 + int2;
  202.                 decoded.add(decodeMap.get(result));
  203.                 i++;
  204.             }
  205.         }
  206.         return listCharToString(decoded);
  207.     }
  208.  
  209.     public String listCharToString(ArrayList<Character> charList) {
  210.         StringBuilder result = new StringBuilder();
  211.         for (Character character : charList) {
  212.             result.append(character);
  213.         }
  214.         return result.toString();
  215.  
  216.     }
  217.  
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement