Question: Background: Use Eclipse to develop and debug your assignment. Download and unzip the lab starter file. Import it into Eclipse. Assignment: Create a Java enum

Background: Use Eclipse to develop and debug your assignment. Download and unzip the lab starter file. Import it into Eclipse.

Assignment: Create a Java enum for HairColor. Valid hair colors are: BLACK, BROWN, BLOND, RED, BALD, GRAY, SANDY, WHITE, UNKNOWN

Create a Java enum for EyeColor. Valid eye colors are: BLACK, BLUE, BROWN, GREEN, HAZEL, MAROON, MULTICOLOR, PINK

Implement the Person class. This will be a person who was involved in a police incident (a crime). The Person class has the following fields:

  • String name
  • String role
  • Int age
  • HairColor hairColor
  • EyeColor eyeColor

Use Eclipse to generate a constructor (with all fields) and a complete set of accessors. You will need to create an appropriate toString() method.

Look at the data file: data.csv. This file contains multiple lines of data describing persons involved in a police incident. The columns (separated by commas) include name, role, age, hair color, eye color.

Implement the code in the Main class for the readData() method. This method will read each line in the file. It will use the split(,) method (of String) to break up the line of data. It will then create a Person object from the data and add it the list of persons. If a data string does not match an enum type (example eye color xyz), the code should skip the line of data and output an error message.

Your final output should look like the example below.

Example Output

Skipping field: Irene Adler,24,Blond,xyz

=== Police Incident Report ===

Name: Sherlock Holmes Role: Witness Age: 35 Hair Color: BROWN Eye Color: GREEN

Name: James Watson Role: Witness Age: 32 Hair Color: BROWN Eye Color: BLUE

Name: Sabastian Moran Role: Suspect Age: 33 Hair Color: BLACK Eye Color: BROWN

Name: James Moriarty Role: Suspect Age: 51 Hair Color: BROWN Eye Color: GREEN

Name: Gregory Lestrade Role: Officer Age: 45 Hair Color: BLOND Eye Color: BLUE

Name: Ronald Adair Role: Victim Age: 23 Hair Color: BLOND Eye Color: GREEN

Main.java:

import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; import java.util.Scanner;

/** * Purpose of program * @author StudentName * */ public class Main {

private static final String DATAFILE = "data.csv";

public static void main(String[] args) { List list = readData(DATAFILE); System.out.println("=== Police Incident Report ==="); for (Person person : list) { System.out.println(person); } }

public static List readData(String filenanme) { List list = new ArrayList<>(); File file = new File(filenanme); try (Scanner input = new Scanner(file);) { while (input.hasNext()) { // TODO - read a string from the file // Use split(",") to break up the line into an array of strings // Create a Person object from the data // Add the person object to the list of persons // Be sure to process IllegalArgumentException if one of the strings // does not match a enum type and output an error message } } catch (FileNotFoundException e) { System.out.println("Cannot read "+filenanme); } return list; }

}

Person.java:

public class Person { // TODO - implement this class }

data.csv file: (excel)

Sherlock Holmes Witness 35 Brown Green James Watson Witness 32 Brown Blue Sabastian Moran Suspect 33 Black Brown James Moriarty Suspect 51 Brown Green Gregory Lestrade Officer 45 Blond Blue Ronald Adair Victim 23 Blond Green Irene Adler 24 Blond xyz

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!