In this program you will read in records from blood donors i

In this program, you will read in records from blood donors in a file, sort them based on one of the fields of the records using Java\'s built-in sorting routines, and then write the sorted list to the console as a table. Your program must include the following elements:

1. A class called \"BloodDonor\" to hold a donor\'s information.

2. Different comparators to compare the different attributes of donors.

Blood donor records will be specified in a file, one record per line. Each line of the file will have five comma-separated fields with no spaces between them (this is called a csv file). The fields will be:

idNum,

lastName,

firstName,

type,

donationTime

An example file is shown below:

74,Corvinas,Alexander,A+,18.1

1011,Intervenus,Lestat,O-,18.2

1111,Swan,Bella,B+,11.5

1,The First,Dracula,A+,12.7

The program must take two pieces of input, either as command line arguments, or by prompting the user for input:

1. The name of a file containing donor records.

2. The name of a field (attribute) of the records on which to sort, which must be one of id, first, last, type, time.

When run, your program must first print out your name and the program name, get input from the user (either from the command line arguments or using a scanner). Then it must print a formatted, sorted list of the records from the file, printed to the console. The donation time should be printed with 1 digit after the decimal point. If improper or not enough input is given, the program should either print out a usage statement and exit,or prompt the user for proper input.

An example run of the program should look something like the following:

java BloodSort donors.txt first

Blood donor sorting program by <your name>

ID num Last First       Type Time

74 Corvinas Alexander A+ 18.1

1111 Swan Bella B+ 11.5

1 The First Dracula A+ 12.7

1011 Intervenus   Lestat O- 18.2

Solution

import java.io.BufferedReader;

import java.io.FileReader;

import java.util.Collections;

import java.util.Comparator;

import java.util.Scanner;

public class BloodDonorMain {

   public static void main(String[] args) {

       Scanner in = new Scanner(System.in).useDelimiter(\"\\\ \");

       //String filename = \"/Users/Akhilaperumalla/Desktop/readme.txt\";

       java.util.List<String> records = new java.util.ArrayList<String>();

       java.util.List<BloodDonor> donors = new java.util.ArrayList<BloodDonor>();

      

       System.out.println(\"Please enter filename with full path\");

       String filename = in.next();

       try {

           BufferedReader reader = new BufferedReader(new FileReader(filename));

           String line;

           while ((line = reader.readLine()) != null) {

               records.add(line);

               // System.out.println(line);

               String[] temp = line.split(\",\");

               int idNum = Integer.parseInt(temp[0]);

               String lastName = temp[1];

               String firstName = temp[2];

               String type = temp[3];

               String donationTime = temp[4];

               donors.add(new BloodDonor(idNum, lastName, firstName, type, donationTime));

           }

           reader.close();

           System.out.println(\"Please select an option to sort\");

           System.out.println(\"1:ID\");

           System.out.println(\"2:LastName\");

           System.out.println(\"3:FirstName\");

           System.out.println(\"4:Time\");

           System.out.println(\"5:Type\");

           System.out.println(\"Please enter your option\");

           int option=in.nextInt();

           switch (option) {

           case 1:

               Collections.sort(donors, new BloodDonorID());

               System.out.println(\"Sorted list entries based on ID: \ \");

               for (BloodDonor e : donors) {

                   System.out.println(e.toString());

               }

               System.out.println();

               break;

           case 2:

               Collections.sort(donors, new BloodDonorLastName());

               System.out.println(\"Sorted list entries based on Last Name: \ \");

               for (BloodDonor e : donors) {

                   System.out.println(e.toString());

               }

               System.out.println();

               break;

           case 3:

               Collections.sort(donors, new BloodDonorFirstName());

               System.out.println(\"Sorted list entries based on First Name: \ \");

               for (BloodDonor e : donors) {

                   System.out.println(e.toString());

               }

               System.out.println();

               break;

           case 4:

               Collections.sort(donors, new BloodDonorTime());

               System.out.println(\"Sorted list entries based on Time: \ \");

               for (BloodDonor e : donors) {

                   System.out.println(e.toString());

               }

               System.out.println();

               break;

           case 5:

               Collections.sort(donors, new BloodDonorType());

               System.out.println(\"Sorted list entries based on Type: \ \");

               for (BloodDonor e : donors) {

                   System.out.println(e.toString());

               }

               System.out.println();

               break;

           }

       } catch (Exception e) {

           System.err.format(\"Exception occurred trying to read \'%s\'.\", filename);

           e.printStackTrace();

       }

   }

}

class BloodDonorID implements Comparator<BloodDonor> {

   @Override

   public int compare(BloodDonor e1, BloodDonor e2) {

       if (e1.getIdNum() > e2.getIdNum()) {

           return 1;

       } else {

           return -1;

       }

   }

}

class BloodDonorFirstName implements Comparator<BloodDonor> {

   @Override

   public int compare(BloodDonor e1, BloodDonor e2) {

       return e1.getFirstName().compareTo(e2.firstName);

   }

}

class BloodDonorLastName implements Comparator<BloodDonor> {

   @Override

   public int compare(BloodDonor e1, BloodDonor e2) {

       return e1.getLastName().compareTo(e2.lastName);

   }

}

class BloodDonorType implements Comparator<BloodDonor> {

   @Override

   public int compare(BloodDonor e1, BloodDonor e2) {

       return e1.getType().compareTo(e2.type);

   }

}

class BloodDonorTime implements Comparator<BloodDonor> {

   @Override

   public int compare(BloodDonor e1, BloodDonor e2) {

       return e1.getDonationTime().compareTo(e2.donationTime);

   }

}

class BloodDonor {

   int idNum;

   String lastName;

   String firstName;

   String type;

   String donationTime;

   public int getIdNum() {

       return idNum;

   }

   public void setIdNum(int idNum) {

       this.idNum = idNum;

   }

   public String getLastName() {

       return lastName;

   }

   public void setLastName(String lastName) {

       this.lastName = lastName;

   }

   public String getFirstName() {

       return firstName;

   }

   public void setFirstName(String firstName) {

       this.firstName = firstName;

   }

   public String getType() {

       return type;

   }

   public void setType(String type) {

       this.type = type;

   }

   public String getDonationTime() {

       return donationTime;

   }

   public void setDonationTime(String donationTime) {

       this.donationTime = donationTime;

   }

   public BloodDonor(int idNum, String lastName, String firstName, String type, String donationTime) {

       this.idNum = idNum;

       this.lastName = lastName;

       this.firstName = firstName;

       this.type = type;

       this.donationTime = donationTime;

   }

   @Override

   public String toString() {

       return \"BloodDonor [idNum=\" + idNum + \", lastName=\" + lastName + \", firstName=\" + firstName + \", type=\" + type

               + \", donationTime=\" + donationTime + \"]\";

   }

}

In this program, you will read in records from blood donors in a file, sort them based on one of the fields of the records using Java\'s built-in sorting routin
In this program, you will read in records from blood donors in a file, sort them based on one of the fields of the records using Java\'s built-in sorting routin
In this program, you will read in records from blood donors in a file, sort them based on one of the fields of the records using Java\'s built-in sorting routin
In this program, you will read in records from blood donors in a file, sort them based on one of the fields of the records using Java\'s built-in sorting routin
In this program, you will read in records from blood donors in a file, sort them based on one of the fields of the records using Java\'s built-in sorting routin
In this program, you will read in records from blood donors in a file, sort them based on one of the fields of the records using Java\'s built-in sorting routin
In this program, you will read in records from blood donors in a file, sort them based on one of the fields of the records using Java\'s built-in sorting routin

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site