TheRightGuy

how?

Mar 31st, 2022 (edited)
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. public class MorseCodeApp {
  2.  
  3.     public static void main(String[] args) {
  4.         System.out.println("Enter your text to convert to morse code and play it ");
  5.         InputHandler inputHandler = new InputHandler();
  6.         inputHandler.loopTroughInput();
  7.     }
  8. }
  9.  
  10. -------------
  11. public class InputHandler {
  12.     private final Map<String, String> letterToMorseCode = Map.ofEntries(
  13.             Map.entry("A", ".-"),
  14.             Map.entry("B", "-..."),
  15.             Map.entry("C", "-.-."),
  16.             Map.entry("D", "-.."),
  17.             Map.entry("E", "."),
  18.             Map.entry("F", "..-."),
  19.             Map.entry("G", "--."),
  20.             Map.entry("H", "...."),
  21.             Map.entry("I", ".."),
  22.             Map.entry("J", ".---"),
  23.             Map.entry("K", "-.-"),
  24.             Map.entry("L", ".-.."),
  25.             Map.entry("M", "--"),
  26.             Map.entry("N", "-."),
  27.             Map.entry("O", "---"),
  28.             Map.entry("P", ".--."),
  29.             Map.entry("Q", "--.-"),
  30.             Map.entry("R", ".-."),
  31.             Map.entry("S", "..."),
  32.             Map.entry("T", "-"),
  33.             Map.entry("U", "..-"),
  34.             Map.entry("V", "...-"),
  35.             Map.entry("W", ".--"),
  36.             Map.entry("X", "-..-"),
  37.             Map.entry("Y", "-.--"),
  38.             Map.entry("Z", "--.."));
  39.  
  40.     void loopTroughInput() {
  41.         AudioHandler audioHandler = new AudioHandler();
  42.         Scanner input = new Scanner(System.in);
  43.         String userInput = input.nextLine().toUpperCase();
  44.         Matcher matcher = Pattern.compile("([a-zA-Z])").matcher(userInput);
  45.         while (matcher.find()) {
  46.             getMatchingValue(matcher.group(1));
  47.             audioHandler.pathForAudio(matcher.group(1));
  48.         }
  49.     }
  50.  
  51.     private void getMatchingValue(String key) {
  52.         letterToMorseCode.entrySet().stream().filter(map -> map.getKey().equals(key)).map(Map.Entry::getValue).forEach(morse -> System.out.print(morse + " "));
  53.     }
  54. }
  55. --------------------
  56. public class AudioHandler {
  57.  
  58.     void pathForAudio(String letter) {
  59.         String filePath = "src/main/resources/" + letter + ".wav";
  60.         audioSetUp(filePath);
  61.         breakBetweenLetters();
  62.     }
  63.  
  64.     private void breakBetweenLetters() {
  65.         try {
  66.             Thread.sleep(1500);
  67.         } catch (InterruptedException e) {
  68.             System.out.println("Something is wrong with the thread");
  69.         }
  70.     }
  71.  
  72.     private void audioSetUp(String filePath) {
  73.         try {
  74.             AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(Paths.get(filePath).toFile());
  75.             Clip clip = AudioSystem.getClip();
  76.             clip.open(audioInputStream);
  77.             clip.start();
  78.         } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
  79.             System.out.println("That file is either invalid or there was a mistake during the playing of audio");
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment