Commit e2b0e32b authored by jack.ellis's avatar jack.ellis

10/12

parent c899adb3
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.io.*;
import java.util.Scanner;
public class AssessmentPartFour { public class AssessmentPartFour {
List<String> morseCode = new ArrayList<String>(); List<String> morseCode = new ArrayList<String>(); //
public int loadMorseFromFile(String filename) public int loadMorseFromFile(String filename)
{ {
File file = new File(filename);
morseCode.clear();
Scanner sc = null;
try {
sc = new Scanner(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String lineFromFile;
while (sc.hasNextLine()) {
lineFromFile = sc.nextLine();
morseCode.add(lineFromFile);
return 0; }
return morseCode.size();
} }
public String translateMorse() public String translateMorse(String filename)
{ {
return "";
File file = new File(filename);
morseCode.clear();
String[] morse = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..","/"};
String[] alphabet = {"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"," "};
Scanner sc = null;
try {
sc = new Scanner(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
String lineFromFile;
while (sc.hasNextLine()) {
lineFromFile = sc.nextLine();
morseCode.add(lineFromFile);
}
///////////////////////////////////
String message="";
for (int i = 0; i < morseCode.size();i++) {
for (int x = 0; x < alphabet.length; x++) {
// System.out.println("["+morseCode.get(i)+ "] :: ["+ morse[x] + "]");
if (morseCode.get(i).equals(morse[x])) {
morseCode.set( i, alphabet[x] );
message+= alphabet[x];
}
}
}
///////////////////////////////////
System.out.println(message);
return message;
}
} }
...@@ -27,6 +27,7 @@ class AssessmentPartFourTest { ...@@ -27,6 +27,7 @@ class AssessmentPartFourTest {
}) })
void testEnryptedCharacter(String filename, int count) { void testEnryptedCharacter(String filename, int count) {
assertEquals(count,test.loadMorseFromFile(filename)); assertEquals(count,test.loadMorseFromFile(filename));
test.morseCode.clear();
} }
@ParameterizedTest @ParameterizedTest
...@@ -38,14 +39,17 @@ class AssessmentPartFourTest { ...@@ -38,14 +39,17 @@ class AssessmentPartFourTest {
"test4.txt,13,yippee did it" "test4.txt,13,yippee did it"
}) })
void testEncryptedString(String filename, int count, String message) { void testEncryptedString(String filename, int count, String message) {
test.morseCode.clear();
int cc = test.loadMorseFromFile(filename); int cc = test.loadMorseFromFile(filename);
if (cc==count) if (cc==count)
{ {
assertEquals(message, test.translateMorse()); assertEquals(message, test.translateMorse(filename));
} }
else else
{ {
fail("File failed to load"); fail("File failed to load");
} }
test.morseCode.clear();
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment