Blood is a scarce resource everywhere. Blood banks keep records of which donors
ID: 3824615 • Letter: B
Question
Blood is a scarce resource everywhere. Blood banks keep records of which donors have which blood type. U.S. presidents travel with a supply of their own blood kept in a miniature operating room on Air Force One in case that plane comes under attack. This program processes a presidential bloodbank database. Your solution must read a list of entries consisting of a blood type followed by the presidents who share that type.
The file type2pres.txt represents a mapping of a blood type to the list of presidents who have that blood type. The tokens on each line are comma delimited. The file type2pres.txt contains:
You can easily split a comma delimited line into an array of tokens like this:
You can even use the Arrays.asList() method to put those tokens diretly into an ArrayList. This method converts a plain array to a List which is suitable to initialize an ArrayList.
STEP #1: Generate a map from bloodtype (the key) to a list of presidents (the value). Print to the screen sorted by bloodtype, and list the presidents alphabetically on the line. SEE OUTPUT.
STEP #2: Generate an inverse mapping where each president's name is a unique key and his bloodtype is the value. Print that map sorted alphabetically by president. SEE OUTPUT.
Starter File: BloodBank.java
SAMPLE OUTPUT CAPTURE
Command Prompt CNUs erst im Desktop an java an A+ B. H. Obama G. W. Bush J. F. Kennedy A. Lincoln G.A. Ford L. B. Johnson U. S. Grant W. J. Clint on B+ F. D. Roosevelt J. A. Garfield T. L. Hoffman C. A. Arthur G.Cleveland R.M.Nixon A. Lincoln B. H. alma A+ C. A. Arthur F. D. Roosevelt B+ G. A. Ford G. Cleveland G. W. Bush A+ J. A. Garfield B+ J. F. Kennedy A+ L. B. Johnson R. M. Nixon T. L. Hoffman B+ U.S. Grant W. J. Clinton C: Users tim Desktop anExplanation / Answer
JAVA :
import java.util.*;
import java.io.*;
public class Main{
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(new File("P:/type2pres.txt")));
HashMap<String,List<String>> hm = new HashMap();
HashMap<String,String> ihm = new HashMap();
int ind = 0;
String t;
//LOop through all the lines
while((t = br.readLine())!=null){
//Ignore the first string before comma, as it is the blook group
String str[] = t.substring(t.indexOf(',')+1).split(",");
//Convert the names into list
List l = Arrays.asList(str);
//Sort them
Collections.sort(l);
//Add it to the map
hm.put(t.substring(0,t.indexOf(',')),l);
//Put the president name and blood group
str = t.trim().split(",");
for(int i=1;i<str.length;i++){
ihm.put(str[i], str[0]);
}
}
//Sort the blood groups
Set<String> keys = hm.keySet();
List<String> key = new ArrayList();
for(String s : keys){
key.add(s);
}
Collections.sort(key);
for(String s : key){
System.out.println(s+" "+hm.get(s));
}
//Sort the president names
keys = ihm.keySet();
key = new ArrayList();
for(String s : keys){
key.add(s);
}
Collections.sort(key);
for(String s : key){
System.out.println(s+" "+ihm.get(s));
}
}
}
OUTPUT :
A+ [B.H.Obama, G.W.Bush, J.F.Kennedy]
A- [A.Lincoln, G.A.Ford, L.B.Johnson, U.S.Grant, W.J.Clinton]
B+ [F.D.Roosevelt, J.A.Garfield, T.L.Hoffman]
B- []
O [C.A.Arthur, G.Cleveland, R.M.Nixon]
A.Lincoln A-
B.H.Obama A+
C.A.Arthur O
F.D.Roosevelt B+
G.A.Ford A-
G.Cleveland O
G.W.Bush A+
J.A.Garfield B+
J.F.Kennedy A+
L.B.Johnson A-
R.M.Nixon O
T.L.Hoffman B+
U.S.Grant A-
W.J.Clinton A-