Как написать тест junit в java для моей азбуки Морзе - PullRequest
0 голосов
/ 12 марта 2020

Я новичок в java, и я немного тренируюсь и хотел бы знать, как я могу написать тестовый пример JUnit для своего кода, я застрял, потому что у меня есть только основной класс и большинство примеров Я проверил, было ли это о классе, но не о главном.

Как я могу решить эту проблему?

public class MorseCode {

    public static void main(String[] args)  throws IOException  {


        int option = 0;
        //String sentence include the both answers. 
        String sentence = "",answer = "",answer1 = "";

        //Character Array of the English Letters numbers and Symbols.
         char[] english = { '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',

                  '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.', ',', '?'};   

         //Array of String to hold the Morse Code value of Every English Letter,Number and Symbol.
         String[] morse = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", 
                    ".---", "-.-", ".-..", "--", "-.", "---", ".---.", "--.-", ".-.",
                    "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----",
                    "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.",
                    "-----", "--..--", ".-.-.-", "..--.." };  


        Scanner sc = new Scanner(System.in);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Welcome to MorseCode Program :) ");
        System.out.println("");
        do
        {
        System.out.println("Enter the Option Corresponding to the Task you want to Perform");
        System.out.println("1.Translate to Morse Code. \n2.Translate to English Language. \n3.Exit ");


        //Loop until you get an integer.
        while(!sc.hasNextInt())  
        { 
            System.out.println("ERROR!! Enter Digits Only. Try Again!! ");          
            sc.next();   
        }

        //Take input and check which case.
        option = sc.nextInt();
        switch(option)
        {

            case 1:
            {
                System.out.println("");
                System.out.println("Enter the Sentence that you want to Translate to the Morse Code \n");
                sentence = br.readLine();
                System.out.println("");
                sentence = sentence.toLowerCase();

                char[] morsec = sentence.toCharArray();
                for(int i = 0; i < morsec.length; i++)  
                {
                    for(int j = 0; j < english.length; j++)   
                    {
                        if(english[j] == morsec[i])  
                        {
                            answer = answer + morse[j] + " ";  
                        }  
                    }
                }
                System.out.println("The Morse Code Translation is: ");
                System.out.print("");
                System.out.println(answer);
                System.out.println("");
                break;
            }
            case 2:
            {
                System.out.println("");
                System.out.println("Enter the Morse Code and After Every Letter add Space in Between ");          
                sentence = br.readLine();
                System.out.println("");

                String[] morsec = sentence.split(" ");  
                for(int i = 0;i < morsec.length;i++)
                {
                    for(int j = 0;j < morse.length;j++)
                    {
                        if(morse[j].equals(morsec[i]))  
                        {
                            answer1 = answer1 + english[j];  
                        }
                    }
                }
                System.out.println("The English Language Translation is:");
                System.out.print("");
                System.out.println(answer1);
                System.out.println("");
                break;
            }
            case 3:
            {
                System.out.println("");
                System.out.println("Thank you For Using Morse Code Translator");          
                break;
            }
            default:
            {
                System.out.println("ERROR!! Invalid Option Entered "); 
                break;
            }
            }
        }
        while(option!=3);
 }
}
...