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

CP Project Submission #4

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="/Users/chelseapatterson/Downloads/json-simple-1.1.1 2.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>candidate-technical-assessment-exercise</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
2 changes: 2 additions & 0 deletions .settings/org.eclipse.ltk.core.refactoring.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
org.eclipse.ltk.core.refactoring.enable.project.refactoring.history=false
28 changes: 28 additions & 0 deletions Instructions for Program
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
The program was made on the IDE Eclipse.

There is an embedded JSON file with people that have an ID and name in JSON format.

You will find a package name "exercise" in the src folder.

In the package there are three classes: ReadJSONFile. returnData. returnDataTest.

ReadJSONFile: In this class, the JSON file is read and parsed.
This class also takes in user input. 0 is to exit the program. 1 to use the program to search for a person.
If the user enters 1, the user will be prompted to either enter a name or ID of the person to search for.
This will implement the next class: returnData.

returnData: In this class, user input will be cleaned and compared to data in the JSON file. If they match, it will
rturn the JSON Object. The user will be asked if they would like to continue or exit.

Finally, the last class is the returnDataTest. You will find 24 tests that have been passed by the program.
Of the 24 test cases, they can be chunked into a couple of cateogories:

Testing the search for each person by....
1. Name
2. ID
3. Name with white space and weird casing
4. ID with white space
5. Without their full name
6. Non-existing names
7. Non-existing ID

22 changes: 22 additions & 0 deletions file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"People": [
{
"ID": 1,
"Name": "Bruce Wayne"
},

{
"ID": 2,
"Name": "Meryl Streep"
},

{
"ID": 3,
"Name": "Tom Hanks"
},
{
"ID": 4,
"Name": "Jennifer Aniston"
}
]
}
134 changes: 134 additions & 0 deletions src/exercise/ReadJSONFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package exercise;

import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.util.Scanner;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class ReadJSONFile {

// no constructor

// methods
/**
* Reads the JSONFile.
* Stores JSON data as an array.
* Controls the program.
* @param args
* @throws IOException
* @throws ParseException
* @throws org.json.simple.parser.ParseException
*/
public static void main(String[] args) throws IOException, ParseException, org.json.simple.parser.ParseException {

// initialize parser
JSONParser jsonparser = new JSONParser();

// load JSON file
FileReader reader = new FileReader("file.json");

// parse JSON file
Object obj = jsonparser.parse(reader);

// convert java object into JSON object
JSONObject info =(JSONObject)obj;

// extract array
JSONArray array = (JSONArray)info.get("People");

// take user input
Scanner input = new Scanner(System.in);

// while askAgain returns true, continuing searching data
while (askAgain(input) == true) {

personIdOrName(input, array);
}

// when askAgain returns false, exit the program
System.out.println("Thank you, goodbye.");
input.close();
}

/**
* Asks for input from user.
* If a 0, exits the program.
* If a 1, starts the program.
* Doesn't allow for any other input.
* @return true if user wants to search, false if exit.
*/
public static boolean askAgain(Scanner input2) {

// prints the user a message
// scans in
//Scanner input = new Scanner(System.in);

// sets the number
int number = 0;

// run once and loop until input is valid
do
{
// prints the user a message
System.out.println("Please enter 0 to exit or 1 to search: ");

// prevent an invalid input
while (!input2.hasNextInt()) {
System.out.println("");
System.out.println("Please enter 0 to exit or 1 to search: ");
input2.next();
}

// set the number
number = input2.nextInt();

// if the number is outside the range, loop again
if (number < 0 || number > 1) {
System.out.println("");
//System.out.println("Please enter 0 to exit or 1 to search: ");
}


} while (number < 0 || number > 1);

// if number is 0, exit the program
if (number == 0) {
return false;
}

// else continue
return true;

}


/**
* Asks for input from the user of the person to search for.
* Will except an ID or Name.
* Converts both to a string to be used in the DataReturn class.
* @param num
* @param array
*/
public static void personIdOrName (Scanner input3, JSONArray array) {


// prints the user a message
System.out.println("Please enter the ID or name to search: ");

// takes a string input
//Scanner input = new Scanner(System.in);

// stores the input in a string variable
String search = input3.next();


// call the obtainIdOrName method in DataReturn class
returnData.obtainIdOrName(search, array);

}

}
72 changes: 72 additions & 0 deletions src/exercise/returnData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package exercise;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class returnData {

// instance variable
static JSONObject jsonreturn;

// no constructor

// methods

/**
* Takes a string from scanner method and cleans it.
* Iterates over the JSON array.
* Converts each iteration to a string that is lower case.
* Compares it to the string parameter.
* If the same, stores it as a JSONObject and calls return() to print it.
* Otherwise, continues the iteration.
* If completes the iteration and none is found, prints a a message.
* @param str from scanner
* @param array from JSON file
* @return
*/
public static boolean obtainIdOrName (String str, JSONArray array) {

// trim and lower case the provided string
String newStr = str.trim().toLowerCase();

// iterate over the array
for (int i=0; i<array.size(); i++) {

// create an object of the array
JSONObject people = (JSONObject) array.get(i);

// turns the object into a string that is all lower case
String person = people.toString().toLowerCase();

// if string contains name searched for
if (person.contains(newStr)) {

jsonreturn = people;
result();

return true;
}
}

// otherwise, print this error message
System.out.println("Person could not be found");
System.out.println("");
return false;
}

/**
* This methods main goal is to print the JSONObject.
* Separated from the above method to help with testing.
* @return the JSONOBject
*/
public static JSONObject result() {

System.out.println("");
System.out.println(jsonreturn);
System.out.println("");

return jsonreturn;

}

}
Loading