Advertisement
TheRightGuy

MorseCode

Aug 31st, 2022
815
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.20 KB | None | 0 0
  1. public class ApplicationStarter {
  2.  
  3.     public static void main(String[] args) {
  4.         System.out.println("Enter your text to convert to morse code and play it ");
  5.         MorseCodeCharacterHandler morseCodeCharacterHandler = new MorseCodeCharacterHandler();
  6.         morseCodeCharacterHandler.start();
  7.     }
  8. }
  9. --------------------------------------------
  10. public class MorseCodeCharacterHandler {
  11.  
  12.     public void start() {
  13.         loopTroughInput();
  14.     }
  15.  
  16.     void loopTroughInput() {
  17.         AudioHandler audioHandler = new AudioHandler();
  18.         String userInput = getUserInput();
  19.         Matcher matcher = Pattern.compile("([A-Z\\d,&])").matcher(userInput);
  20.  
  21.         while( matcher.find() ) {
  22.             System.out.println(MorseCode.getEnum(matcher.group(1)));
  23.             audioHandler.pathForAudio(matcher.group(1));
  24.         }
  25.     }
  26.  
  27.     private String getUserInput() {
  28.         Scanner input = new Scanner(System.in);
  29.         return input.nextLine().toUpperCase().strip();
  30.     }
  31.  
  32. }
  33. --------------------------------------------
  34. public class AudioHandler {
  35.  
  36.     void pathForAudio(String character) {
  37.         String filePath = "src/main/resources/" + character + ".wav";
  38.         audioSetUp(filePath);
  39.         breakBetweenCharacters();
  40.     }
  41.  
  42.     private void breakBetweenCharacters() {
  43.         try {
  44.             Thread.sleep(1200);
  45.         } catch ( InterruptedException e ) {
  46.             System.out.println(e.getMessage());
  47.             System.out.println(e.getLocalizedMessage());
  48.         }
  49.     }
  50.  
  51.     private void audioSetUp(String filePath) {
  52.         try {
  53.             AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(Paths.get(filePath).toFile());
  54.             Clip clip = AudioSystem.getClip();
  55.             clip.open(audioInputStream);
  56.             clip.start();
  57.         } catch ( UnsupportedAudioFileException | IOException | LineUnavailableException e ) {
  58.             System.out.println(e.getMessage());
  59.             System.out.println(e.getLocalizedMessage());
  60.         }
  61.     }
  62. }
  63. --------------------------------------------
  64. public enum MorseCode {
  65.  
  66.     A(".-"),
  67.     B("-..."),
  68.     C("-.-."),
  69.     D("-.."),
  70.     E("."),
  71.     F("..-."),
  72.     G("--."),
  73.     H("...."),
  74.     I(".."),
  75.     J(".---"),
  76.     K("-.-"),
  77.     L(".-.."),
  78.     M("--"),
  79.     N("-."),
  80.     O("---"),
  81.     P(".--."),
  82.     Q("--.-"),
  83.     R(".-."),
  84.     S("..."),
  85.     T("-"),
  86.     U("..-"),
  87.     V("...-"),
  88.     W(".--"),
  89.     X("-..-"),
  90.     Y("-.--"),
  91.     Z("--.."),
  92.     ONE(".----"),
  93.     TWO("..---"),
  94.     THREE("...--"),
  95.     FOUR("....-"),
  96.     FIVE("....."),
  97.     SIX("-...."),
  98.     SEVEN("--..."),
  99.     EIGHT("---.."),
  100.     NINE("----."),
  101.     ZERO("-----"),
  102.     AND(".-...");
  103.     private static final Map<String, String> letterToMorseCode = Map.ofEntries(Map.entry("1", "ONE"),
  104.                                                                                Map.entry("2", "TWO"),
  105.                                                                                Map.entry("3", "THREE"),
  106.                                                                                Map.entry("4", "FOUR"),
  107.                                                                                Map.entry("5", "FIVE"),
  108.                                                                                Map.entry("6", "SIX"),
  109.                                                                                Map.entry("7", "SEVEN"),
  110.                                                                                Map.entry("8", "EIGHT"),
  111.                                                                                Map.entry("9", "NINE"),
  112.                                                                                Map.entry("0", "ZERO"),
  113.                                                                                Map.entry("&", "AND"));
  114.  
  115.     private final String morseCode;
  116.  
  117.  
  118.     MorseCode(String morseCode) {
  119.         this.morseCode = morseCode;
  120.     }
  121.  
  122.     public static MorseCode getEnum(String s) {
  123.         if ( letterToMorseCode.containsKey(s) ) {
  124.             return valueOf(letterToMorseCode.get(s));
  125.         }
  126.         return valueOf(s);
  127.     }
  128.  
  129.     public String getMorseCode() {
  130.         return morseCode;
  131.     }
  132. }
  133.  
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement