STEP #1: 40 pts Read foodDrug2Category.txt into a Map where the key is the categ
ID: 3824387 • 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.
foodDrug2Category.txt
patient2FoodDrug
dontMix
Starter File: Drugs.java
Explanation / Answer
Code as per requirement:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
public class Drugs {
/** Maps to hold respective information from 3 files. TreeMap is chosen so that keys are sorted by default. **/
private static TreeMap<String, ArrayList<String>> foodDrugMap = new TreeMap<>();
private static TreeMap<String, ArrayList<String>> patientFoodDrugMap = new TreeMap<>();
private static TreeMap<String, ArrayList<String>> dontMixMap = new TreeMap<>();
public static void main(String[] args) {
try {
BufferedReader foodDrug2CategoryFile = new BufferedReader(new FileReader("foodDrug2Category.txt"));
populateMap(foodDrugMap, foodDrug2CategoryFile);
printMap(foodDrugMap);
System.out.println();
BufferedReader patient2FoodDrugFile = new BufferedReader(new FileReader("patient2FoodDrug.txt"));
populateMap(patientFoodDrugMap, patient2FoodDrugFile);
printMap(patientFoodDrugMap);
System.out.println();
BufferedReader dontMixFile = new BufferedReader(new FileReader("dontMix.txt"));
populateMap(dontMixMap, dontMixFile);
ArrayList<String> patientList = getPatientList();
// Print list.
System.out.println();
for (int i = 0; i < patientList.size(); i++) {
System.out.print(patientList.get(i) + " ");
}
} catch(FileNotFoundException f) {
}
}
/**
* Method to populate map from information obtained from file.
* @param map Map reference which will be populated.
* @param reader buffered reader reference to the file from where we need to read data.
*/
private static void populateMap(TreeMap<String, ArrayList<String>> map, BufferedReader reader) {
String line;
try {
// While we have more lines to read.
while ((line = reader.readLine()) != null) {
// Split using ',' as delimiter.
String[] list = line.split(",");
// First substring before , is always the key for map.
String key = list[0];
// Add rest of strings to arraylist.
ArrayList<String> drugList = new ArrayList<>();
for (int i = 1; i < list.length; i++) {
drugList.add(list[i]);
}
// Add key-value pair to map.
map.put(key, drugList);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Method to print key-value pairs of map. This will print the values of map in natural ordering of keys, and the values will be sorted too for each key.
* @param map Map for which key-value pairs needs to be printed.
*/
private static void printMap(TreeMap<String, ArrayList<String>> map) {
for (Map.Entry<String, ArrayList<String>> e : map.entrySet()) {
ArrayList<String> list = e.getValue();
// Sort list.
Collections.sort(list);
// Print key.
System.out.println();
System.out.print(e.getKey());
// Print values.
for (int i = 0; i < list.size(); i++) {
System.out.print(" " + list.get(i));
}
}
}
/**
* Method to get list of patients who are mixing food and drugs which are not allowed to be mixed.
* @return List of patients who are taking prohibited combinations of food/drugs.
*/
private static ArrayList<String> getPatientList() {
ArrayList<String> patientList = new ArrayList<>();
for (Map.Entry<String, ArrayList<String>> e : patientFoodDrugMap.entrySet()) {
// for a particular patient, obtain the list of food/drugs.
ArrayList<String> list = e.getValue();
// Compare list elements.
for (int i = 0; i < list.size(); i++) {
for (int j = i + 1; j < list.size(); j++) {
String elem1 = list.get(i);
String elem2 = list.get(j);
// Food items that contain elem1 and elem2.
String food1 = null;
String food2 = null;
// Search elem1 and elem2 in foodDrugMap.
for (Map.Entry<String, ArrayList<String>> entry : foodDrugMap.entrySet()) {
if (entry.getValue().contains(elem1)) {
food1 = entry.getKey();
}
if (entry.getValue().contains(elem2)) {
food2 = entry.getKey();
}
}
// If there's a mapping of food1,food2 or food2,food1 in dontMixMap, it means the patient is using prohibited combination.
if (dontMixMap.get(food1) != null && dontMixMap.get(food1).get(0).equals(food2) ||
dontMixMap.get(food2) != null && dontMixMap.get(food2).get(0).equals(food1)){
// We add this patient to our list.
patientList.add(e.getKey());
}
}
}
}
return patientList;
}
}
I have included relevant comments to make it easy to understand. I hope this will solve the above problem.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.