Large diffs are not rendered by default.

@@ -4,6 +4,7 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.file.Files;
@@ -18,25 +19,29 @@
* @TODO
* This is a collection class for storing all the elements
* Basically an in depth periodic table
* 1.)Create a lookup method to search the element by its atomic index number
* 2.)Create a lookup method to search the element by its name
* 3.)Create a locater(where) method to find all elements with similar fields
*/
public class ElementCollection extends ArrayList<Element> {
ElementCollection location = new ElementCollection();
ArrayList<String> periodicTable = new ArrayList<>();

//method to find the element by its Atomic index
public ElementCollection() {
}

//method to find the element by its Atomic index number
public Element findByAtomicNumber(int atomic_number) {
for (Element element : this){
if (element.getNumber() == atomic_number){
return element;
for (Element element : this){ //for each element within the table
if (element.getNumber() == atomic_number){ //if the outcome of my getter is equal to that of the element
return element; //then return the element
}
}
return null;
return null; //should never reach null as long as a valid element is called
}

//method to find the element by its name
public Element findByName(String name) {
for (Element element : this){
if (element.getName().equals(name)){
for (Element element : this){ //searching by name instead of number now
if (element.getName().equalsIgnoreCase(name)){ //same as above method but ignoring the casing of element name
return element;
}
}
@@ -49,10 +54,9 @@ public Element findByName(String name) {
// return findByName(name);
// }
}
//method to find where
//i really dont know wtf this method is supposed to do
public ElementCollection where(String fieldName, Object value) {

return location;
return null;
}
}
// private static ElementCollection elementCollection = new ElementCollection();
@@ -1,21 +1,30 @@
package parsing_json;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Arrays;


/**
* @TODO
* The generate method in this class should load the JSON text (periodic_table.json) into
* an ElementColletion
* Then return the Collection
* an ElementColletion (my period table array)
* Then return the Collection (periodic table)
*/

public class ElementCollectionInitializer {
public static ElementCollection generate() {

public static ElementCollection generate() throws FileNotFoundException {
Gson gson = new Gson();
JsonReader jsonReader = new JsonReader(new FileReader("/Users/kieranthomas/Dev/JavaAssessment3/src/main/resources/periodic_table.json"));
ElementCollection periodicTable = new ElementCollection();
Element[] table = gson.fromJson(jsonReader, Element[].class);
periodicTable.addAll(Arrays.asList(table));
return periodicTable;
}
}
// ClassLoader classLoader = ClassLoader.getSystemClassLoader();
// File file = new File(classLoader.getResource(this.fileName).getFile());
// StringBuilder sb = new StringBuilder("");
@@ -28,7 +37,4 @@ public static ElementCollection generate() {
// }catch (IOException e){
// e.printStackTrace();
// }
// return sb.toString();
return null;
}
}
// return sb.toString();
@@ -1,15 +1,15 @@
//package parsing_json;
//
//import org.junit.Test;
//
//import java.io.IOException;
//
//import static org.junit.Assert.*;
//
//public class ElementCollectionInitializerTest {
//
// @Test
// public void generate() throws IOException {
// assertEquals(119, ElementCollectionInitializer.generate().size());
// }
//}
package parsing_json;

import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.*;

public class ElementCollectionInitializerTest {

@Test
public void generate() throws IOException {
assertEquals(119, ElementCollectionInitializer.generate().size());
}
}
@@ -73,61 +73,61 @@ public void findByName() {
assertEquals(expected.getName(), this.elements.findByName("Hydrogen").getName());

}
//
// @Test
// public void where() {
// ElementCollection expected = new ElementCollection() {{add(new Element(
// "Hydrogen",
// "colorless gas",
// 1.008,
// 20.271,
// "diatomic nonmetal",
// "colorless",
// 0.08988,
// "Henry Cavendish",
// 13.99,
// 28.836,
// "Antoine Lavoisier",
// 1,
// 1,
// "Gas",
// "https://en.wikipedia.org/wiki/Hydrogen",
// "https://en.wikipedia.org/wiki/File:Hydrogen_Spectra.jpg",
// "Hydrogen is a chemical element with chemical symbol H and atomic number 1. With an atomic weight of 1.00794 u, hydrogen is the lightest element on the periodic table. Its monatomic form (H) is the most abundant chemical substance in the Universe, constituting roughly 75% of all baryonic mass.",
// "H",
// 1,
// 1,
// new ArrayList<Integer>() {{add(1);}}
// ));
// add(new Element( "Helium",
// "colorless gas, exhibiting a red-orange glow when placed in a high-voltage electric field",
// 4.0026022,
// 4.222,
// "noble gas",
// null,
// 0.1786,
// "Pierre Janssen",
// 0.95,
// Double.NaN,
// null,
// 2,
// 1,
// "Gas",
// "https://en.wikipedia.org/wiki/Helium",
// "https://en.wikipedia.org/wiki/File:Helium_spectrum.jpg",
// "Helium is a chemical element with symbol He and atomic number 2. It is a colorless, odorless, tasteless, non-toxic, inert, monatomic gas that heads the noble gas group in the periodic table. Its boiling and melting points are the lowest among all the elements.",
// "He",
// 18,
// 1,
// new ArrayList<Integer>() {{add(2);}}
// ));}};
//
// assertEquals(expected.get(0).getName(), this.elements.where("name", "Hydrogen").get(0).getName());
// assertEquals(1, this.elements.where("name", "Hydrogen").size());
//
// assertEquals(expected.get(1).getName(), this.elements.where("number", 2).get(0).getName());
// assertEquals(1, this.elements.where("number", 2).size());
//
// assertEquals(12, this.elements.where("phase", "Gas").size());
// }

@Test
public void where() {
ElementCollection expected = new ElementCollection() {{add(new Element(
"Hydrogen",
"colorless gas",
1.008,
20.271,
"diatomic nonmetal",
"colorless",
0.08988,
"Henry Cavendish",
13.99,
28.836,
"Antoine Lavoisier",
1,
1,
"Gas",
"https://en.wikipedia.org/wiki/Hydrogen",
"https://en.wikipedia.org/wiki/File:Hydrogen_Spectra.jpg",
"Hydrogen is a chemical element with chemical symbol H and atomic number 1. With an atomic weight of 1.00794 u, hydrogen is the lightest element on the periodic table. Its monatomic form (H) is the most abundant chemical substance in the Universe, constituting roughly 75% of all baryonic mass.",
"H",
1,
1,
new ArrayList<Integer>() {{add(1);}}
));
add(new Element( "Helium",
"colorless gas, exhibiting a red-orange glow when placed in a high-voltage electric field",
4.0026022,
4.222,
"noble gas",
null,
0.1786,
"Pierre Janssen",
0.95,
Double.NaN,
null,
2,
1,
"Gas",
"https://en.wikipedia.org/wiki/Helium",
"https://en.wikipedia.org/wiki/File:Helium_spectrum.jpg",
"Helium is a chemical element with symbol He and atomic number 2. It is a colorless, odorless, tasteless, non-toxic, inert, monatomic gas that heads the noble gas group in the periodic table. Its boiling and melting points are the lowest among all the elements.",
"He",
18,
1,
new ArrayList<Integer>() {{add(2);}}
));}};

assertEquals(expected.get(0).getName(), this.elements.where("name", "Hydrogen").get(0).getName());
assertEquals(1, this.elements.where("name", "Hydrogen").size());

assertEquals(expected.get(1).getName(), this.elements.where("number", 2).get(0).getName());
assertEquals(1, this.elements.where("number", 2).size());

assertEquals(12, this.elements.where("phase", "Gas").size());
}
}