In: Computer Science
Write a class called CompoundManager that allows you to save all compounds in memory to a text or binary file and then read a list of compounds from a text or binary file and append these to those in memory, those that don’t already exist. In other words, if Ammonia is in memory and the system reads a file that contains Ammonia, then it is ignored, and it moves on to the next compound in the file. Please write the code so that it works with this existing code for the class ChemicalComp (that allows the you to create compounds. The elements can be specified in any order and this order should be preserved. For example, if the scientist supplies: “Ammonia”, “N”, “1”, “H”, “3”, then the formula is: NH3. Note that if the number of atoms is 1, then the 1 should not be used when the formula is requested.) below:
package abi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
//import java.util.Scanner;
public class ChemicalComp {
public static void main(String[] args) throws
IOException{
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
Map data = new HashMap();
while(true){
String
readinput=br.readLine();
if(readinput.equals(""))
break;
String input = readinput.replaceAll("\"",
"");
String array[]=input.split(", ");
String compound=array[0];
String formula="";
for(int i=1;i
if(!array[i].equals("1")){
formula+=array[i];
}
}
data.put(compound, formula);
}
if(!data.isEmpty()) {
@SuppressWarnings("rawtypes")
Iterator it =
data.entrySet().iterator();
while(it.hasNext()) {
@SuppressWarnings("rawtypes")
Map.Entry obj = (Entry)
it.next();
System.out.println(obj.getKey()+":"+obj.getValue());
}
}
}
}
How the code works above:
"Ammonia", "N", "1", "H", "3"
If the Enter key is pressed without any input is pressed then the
input getting process is terminated.
The compound and formula is stored using hashmap keyvalue pairs.
Please use the following elements as a test for the CompoundManager class code:
Water: H2O
Ammonia: NH3
Abamectin: C48H72O14
Ethenol: CH2CHOH
Sulfine: H2CSO
Please and Thank you I rate if the code is written and works correctly as stated. (If absolutely needed code above can be modified if that helps with the CompoundManager class if so please state modifications to ChemcialComp class. Both codes must still work properly however.)
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You.
ChemicalComp.java
package c8;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Map.Entry;
//import java.util.Scanner;
public class ChemicalComp {
public static void storeDataToFile(Map data, String
string){
try {
PrintWriter
outputFile = new PrintWriter(string);
@SuppressWarnings("rawtypes")
Iterator it =
data.entrySet().iterator();
while(it.hasNext()) {
@SuppressWarnings("rawtypes")
Map.Entry obj = (Entry) it.next();
outputFile.println(obj.getKey()+":"+obj.getValue());
}
outputFile.close();
System.out.println("Data exported to file successfully!!!");
}catch(Exception e){
System.out.println("Error while exporting data...");
}
}
public static void getDataFromFile(Map data, String
string){
File file = new File(string);
//reading data from this file
Scanner reader;
String line="";
try {
reader = new
Scanner(file);
String
datas[];
while(reader.hasNextLine()){
line = reader.nextLine();
datas = line.split(":");
data.put(datas[0], datas[1]);
}
System.out.println(data.size()+" Data loaded from file
successfully!!!");
} catch (FileNotFoundException e)
{
System.out.println("file not found....");
}
}
public static void main(String[] args) throws
IOException{
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
Map data = new HashMap();
getDataFromFile(data,"chemicalElem.txt");
System.out.println("Enter elements
below or empty to stop");
while(true){
String
readinput=br.readLine();
if(readinput.equals(""))
break;
String input =
readinput.replaceAll("\"", "");
String
array[]=input.split(",");
String
compound=array[0];
String
formula="";
for(int
i=1;i<array.length;i++)
if(!array[i].equals("1")){
formula+=array[i];
}
if(!data.containsKey(compound)){
data.put(compound, formula);
}else{
System.out.println("Error !!! "+compound+"
already exists in Map....");
}
}
if(!data.isEmpty()) {
@SuppressWarnings("rawtypes")
Iterator it =
data.entrySet().iterator();
while(it.hasNext()) {
@SuppressWarnings("rawtypes")
Map.Entry obj = (Entry) it.next();
System.out.println(obj.getKey()+":"+obj.getValue());
}
}
storeDataToFile(data,"chemicalElem.txt");
}
}
First time run:
second run
third run
no entry