Blood is a scarce resource everywhere. Blood banks keep records of which donors
ID: 3821565 • 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
import java.io.*;
import java.util.*;
public class BloodBank
{
public static void main(String[] args) throws Exception
{
Scanner infile = new Scanner( new FileReader( "type2pres.txt" ) );
ArrayList<String> blood=new ArrayList<String>();
TreeMap<String,TreeSet<String>> map1=new TreeMap<String,TreeSet<String>> ();
TreeMap<String,String> map2=new TreeMap<String,String> ();
int key=0;
while(infile.hasNext())
{
String[] tokens = infile.nextLine().split(",");
ArrayList<String> p = new ArrayList<String>( Arrays.asList(tokens ));
blood.add(p.remove(0));
TreeSet<String> presidents = new TreeSet<String>(p);
map1.put(blood.get(key), presidents);
key++;
}
map2=Invert(map1);
//displaying
System.out.println("map1");
for (Map.Entry<String,TreeSet<String>> entry : map1.entrySet())
{
System.out.println(entry.getKey() + "/" + entry.getValue());
}
System.out.println("map2");
for (Map.Entry<String, String> entry : map2.entrySet())
{
System.out.println(entry.getKey() + "/" + entry.getValue());
}
} // MAIN
public static TreeMap<String,String> Invert( TreeMap<String,TreeSet<String>> map)
{
TreeMap<String,String> inv=new TreeMap<String,String> ();
for (Map.Entry<String,TreeSet<String>> entry : map.entrySet())
{
for(String president: entry.getValue())
{
inv.put(president, entry.getKey());
}
}
return inv;
}
} // BLOODBANK