Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ApplicationStarter {
- public static void main(String[] args) {
- System.out.println("Enter your text to convert to morse code and play it ");
- MorseCodeCharacterHandler morseCodeCharacterHandler = new MorseCodeCharacterHandler();
- morseCodeCharacterHandler.start();
- }
- }
- --------------------------------------------
- public class MorseCodeCharacterHandler {
- public void start() {
- loopTroughInput();
- }
- void loopTroughInput() {
- AudioHandler audioHandler = new AudioHandler();
- String userInput = getUserInput();
- Matcher matcher = Pattern.compile("([A-Z\\d,&])").matcher(userInput);
- while( matcher.find() ) {
- System.out.println(MorseCode.getEnum(matcher.group(1)));
- audioHandler.pathForAudio(matcher.group(1));
- }
- }
- private String getUserInput() {
- Scanner input = new Scanner(System.in);
- return input.nextLine().toUpperCase().strip();
- }
- }
- --------------------------------------------
- public class AudioHandler {
- void pathForAudio(String character) {
- String filePath = "src/main/resources/" + character + ".wav";
- audioSetUp(filePath);
- breakBetweenCharacters();
- }
- private void breakBetweenCharacters() {
- try {
- Thread.sleep(1200);
- } catch ( InterruptedException e ) {
- System.out.println(e.getMessage());
- System.out.println(e.getLocalizedMessage());
- }
- }
- private void audioSetUp(String filePath) {
- try {
- AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(Paths.get(filePath).toFile());
- Clip clip = AudioSystem.getClip();
- clip.open(audioInputStream);
- clip.start();
- } catch ( UnsupportedAudioFileException | IOException | LineUnavailableException e ) {
- System.out.println(e.getMessage());
- System.out.println(e.getLocalizedMessage());
- }
- }
- }
- --------------------------------------------
- public enum MorseCode {
- A(".-"),
- B("-..."),
- C("-.-."),
- D("-.."),
- E("."),
- F("..-."),
- G("--."),
- H("...."),
- I(".."),
- J(".---"),
- K("-.-"),
- L(".-.."),
- M("--"),
- N("-."),
- O("---"),
- P(".--."),
- Q("--.-"),
- R(".-."),
- S("..."),
- T("-"),
- U("..-"),
- V("...-"),
- W(".--"),
- X("-..-"),
- Y("-.--"),
- Z("--.."),
- ONE(".----"),
- TWO("..---"),
- THREE("...--"),
- FOUR("....-"),
- FIVE("....."),
- SIX("-...."),
- SEVEN("--..."),
- EIGHT("---.."),
- NINE("----."),
- ZERO("-----"),
- AND(".-...");
- private static final Map<String, String> letterToMorseCode = Map.ofEntries(Map.entry("1", "ONE"),
- Map.entry("2", "TWO"),
- Map.entry("3", "THREE"),
- Map.entry("4", "FOUR"),
- Map.entry("5", "FIVE"),
- Map.entry("6", "SIX"),
- Map.entry("7", "SEVEN"),
- Map.entry("8", "EIGHT"),
- Map.entry("9", "NINE"),
- Map.entry("0", "ZERO"),
- Map.entry("&", "AND"));
- private final String morseCode;
- MorseCode(String morseCode) {
- this.morseCode = morseCode;
- }
- public static MorseCode getEnum(String s) {
- if ( letterToMorseCode.containsKey(s) ) {
- return valueOf(letterToMorseCode.get(s));
- }
- return valueOf(s);
- }
- public String getMorseCode() {
- return morseCode;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement