java/CaesarovaŠifra/src/cz/frantovo/příklady/Caesar.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 21 Nov 2017 00:20:27 +0100
changeset 54 2a22f9959ea3
parent 19 c681a3a6cbac
permissions -rw-r--r--
přepis-cli-parametrů: první verze
     1 package cz.frantovo.příklady;
     2 
     3 /**
     4  * This program is free software: you can redistribute it and/or modify
     5  * it under the terms of the GNU General Public License as published by
     6  * the Free Software Foundation, either version 3 of the License, or
     7  * (at your option) any later version.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    16  */
    17 import java.awt.GridLayout;
    18 import java.awt.HeadlessException;
    19 import java.awt.event.ActionEvent;
    20 import java.awt.event.ActionListener;
    21 import java.util.ArrayList;
    22 import java.util.Collections;
    23 import java.util.List;
    24 import javax.swing.JButton;
    25 import javax.swing.JFrame;
    26 import javax.swing.JLabel;
    27 import javax.swing.JTextField;
    28 
    29 /**
    30  * @author Ing. František Kučera (frantovo.cz)
    31  */
    32 public class Caesar extends JFrame {
    33 
    34 	private JTextField prostýText = new JTextField();
    35 	private JTextField šifrovanýText = new JTextField();
    36 	private JButton šifruj = new JButton("Šifruj");
    37 	private JButton dešifruj = new JButton("Dešifruj");
    38 	private static final char[] ABECEDA_ZNAKY = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
    39 	private static final List<Character> ABECEDA;
    40 	private static final int SKOK = 3;
    41 
    42 	static {
    43 		List<Character> abeceda = new ArrayList<>();
    44 		for (char z : ABECEDA_ZNAKY) {
    45 			abeceda.add(z);
    46 		}
    47 		ABECEDA = Collections.unmodifiableList(abeceda);
    48 	}
    49 
    50 	public static void main(String[] args) {
    51 		Caesar c = new Caesar();
    52 		c.setVisible(true);
    53 	}
    54 
    55 	public Caesar() throws HeadlessException {
    56 		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    57 		setTitle("Caesarova šifra");
    58 		setLocationRelativeTo(null);
    59 		setLayout(new GridLayout(3, 2));
    60 		
    61 		add(new JLabel("Prostý text"));
    62 		add(prostýText);
    63 
    64 		add(new JLabel("Šifrovaný text"));
    65 		add(šifrovanýText);
    66 
    67 		add(šifruj);
    68 		add(dešifruj);
    69 
    70 		šifruj.addActionListener(new ActionListener() {
    71 			@Override
    72 			public void actionPerformed(ActionEvent e) {
    73 				šifrovanýText.setText(posuň(prostýText.getText(), SKOK));
    74 			}
    75 		});
    76 
    77 		dešifruj.addActionListener(new ActionListener() {
    78 			@Override
    79 			public void actionPerformed(ActionEvent e) {
    80 				prostýText.setText(posuň(šifrovanýText.getText(), -SKOK));
    81 			}
    82 		});
    83 
    84 		pack();
    85 	}
    86 
    87 	private static String posuň(String text, int oKolik) {
    88 		StringBuilder posunutý = new StringBuilder(text.length());
    89 
    90 		for (char z : text.toCharArray()) {
    91 			posunutý.append(posuň(z, oKolik));
    92 		}
    93 
    94 		return posunutý.toString();
    95 	}
    96 
    97 	private static char posuň(char znak, int oKolik) {
    98 		int původníPozice = ABECEDA.indexOf(znak);
    99 
   100 		if (původníPozice < 0) {
   101 			return znak;
   102 		} else {
   103 			int nováPozice = (původníPozice + oKolik) % ABECEDA_ZNAKY.length;
   104 			nováPozice = nováPozice < 0 ? ABECEDA_ZNAKY.length + nováPozice : nováPozice;
   105 			return ABECEDA.get(nováPozice);
   106 		}
   107 	}
   108 }