STEP #1: 40 pts Read foodDrug2Category.txt into a Map where the key is the categ
ID: 3824132 • Letter: S
Question
STEP #1: 40 pts
Read foodDrug2Category.txt
into a Map where the key is the category of drug such as THINNER, ANTI_INFLAM etc., and each key maps to a set of drug/food names that are of that type. Then print out the Map with nothing but space between words, and lines sorted vertically.
STEP #2: 40 pts
Read patient2FoodDrug:
into a Map where the key is the patient name and the value is the set of meds they are on and/or foods they eat. Print the map out just as in Phase #1 where the order of the lines is sorted and the list of mesd/foods is sorted.
STEP#3: 20 pts.
Anyway you can using the dontMix:
file and the maps you have already generated, print the set of names of those people who are using food/drugs that should not be mixed.
Explanation / Answer
import java.util.*;
import java.io.*;
public class Drugs
{
public static void main( String[] args ) throws Exception
{
BufferedReader foodDrug2CategoryFile = new BufferedReader( new FileReader( "foodDrug2Category.txt" ) );
BufferedReader patient2FoodDrugFile = new BufferedReader( new FileReader( "patient2FoodDrug.txt" ) );
BufferedReader dontMixFile = new BufferedReader( new FileReader( "dontMix.txt" ) );
Drugs drugs = new Drugs();
Map<String, ArrayList<String>> map_food = drugs.readDataFromFile(foodDrug2CategoryFile, 1);
Map<String, ArrayList<String>> map_patient = drugs.readDataFromFile(patient2FoodDrugFile, 2);
drugs.dontMix(dontMixFile, map_food, map_patient);
} // END MAIN
public Map readDataFromFile(BufferedReader br, int type) {
Map data = new HashMap();
try {
ArrayList<String> values;
String strLine;
while ((strLine = br.readLine()) != null) {
String[] tokens = strLine.split(",");
values = new ArrayList<String>();
for(int i=1; i<tokens.length; i++) {
values.add(tokens[i]);
}
Collections.sort(values);
data.put(tokens[0], values);
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
System.out.println(" -----------------------------------------");
if(type == 1) {
System.out.println("Drugs with drug/food names");
} else if(type == 2) {
System.out.println("Patient with med/foods they eat");
}
Map<String, ArrayList<String>> map = new TreeMap<String, ArrayList<String>>(data);
for (Map.Entry entry : map.entrySet()) {
System.out.print(" " + entry.getKey());
ArrayList<String> values = (ArrayList<String>)entry.getValue();
for(String val:values){
System.out.print(" " + val);
}
}
return map;
}
public void dontMix(BufferedReader br, Map<String, ArrayList<String>> map_food, Map<String, ArrayList<String>> map_patient) {
System.out.println(" -----------------------------------------");
System.out.println("Patients who do not mix drugs ");
try {
ArrayList<ArrayList<String>> values = new ArrayList<ArrayList<String>>();
String strLine;
while ((strLine = br.readLine()) != null) {
String[] tokens = strLine.split(",");
ArrayList<String> sub_values = new ArrayList<String>();
for(int i=0; i<tokens.length; i++) {
sub_values.add(tokens[i]);
}
values.add(sub_values);
}
for (Map.Entry entry : map_patient.entrySet()) {
boolean is_mix = false;
for(ArrayList<String> val_pairs:values){
int i = 0;
for(String val:val_pairs){
ArrayList<String> key_values = (ArrayList<String>)entry.getValue();
for(String key_val:key_values){
ArrayList<String> all_drugs = (ArrayList<String>)map_food.get(val);
if(all_drugs.contains(key_val)) {
i++;
if(i > 1) {
is_mix = true;
break;
}
}
}
if(is_mix) {
break;
}
}
if(is_mix) {
break;
}
}
if(is_mix) {
System.out.print(entry.getKey()+ " ");
}
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
} // END CLASS
/* output ::
-----------------------------------------
Drugs with drug/food names
ANTI_COAG asparagus broccoli cabbage endive soybeans
ANTI_INFLAM aspirin ibuprofen motrin
ED_MEDS cialis viagra
STATINS crestor lipex lipitor zocor
THINNERS coumadin plavix
-----------------------------------------
Patient with med/foods they eat
Bob_Jones asparagus lipitor
Ed_Smith plavix viagra
Jane_Doe lipitor plavix
John_Doe aspirin broccoli plavix
Tom_Smith cialis soybeans
-----------------------------------------
Patients who do not mix drugs
Ed_Smith John_Doe
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.