Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 26 create utils class #29

Merged
merged 1 commit into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

out/*
9 changes: 9 additions & 0 deletions .idea/libraries/jade2.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion Car Routing.iml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,26 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="jade" level="project" />
<orderEntry type="library" name="org.junit.jupiter:junit-jupiter:5.4.2" level="project" />
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/jade.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="library" name="jade" level="project" />
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="file://$MODULE_DIR$/lib" />
</CLASSES>
<JAVADOC />
<SOURCES />
<jarDirectory url="file://$MODULE_DIR$/lib" recursive="false" />
</library>
</orderEntry>
</component>
</module>
40 changes: 40 additions & 0 deletions src/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Utils {

public static Scanner getFileReference(final String filePath) {

File f;
Scanner fileReference;

try {
f = new File(filePath);

if(f.exists()) {
System.out.println(filePath + " file opened.");
}
else {
System.out.println(filePath + " doesn't exist.");
return null;
}
} catch(NullPointerException | SecurityException e) {
System.out.println(filePath + "couldn't be opened.");
System.out.println(e.getMessage());
return null;
}

try {
fileReference= new Scanner(f);
} catch(FileNotFoundException e) {
System.out.println(filePath + "couldn't be found.");
System.out.println(e.getMessage());
return null;
}

return fileReference;
}
}
26 changes: 26 additions & 0 deletions test/java/utils/UtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package utils;

import org.junit.jupiter.api.Test;

import java.util.Scanner;

import static org.junit.jupiter.api.Assertions.*;

class UtilsTest {

@Test
void getFileReference() {
Scanner file = Utils.getFileReference("test/resources/fileTest.txt");

assertNotNull(file);

int nLines = 0;

while(file.hasNextLine()) {
nLines++;
System.out.println(file.nextLine());
}

assertEquals(2, nLines, "The number of lines should be 2");
}
}
2 changes: 2 additions & 0 deletions test/resources/fileTest.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hello world!
I'm in.