Skip to content

Vid completed MesoLab Strings #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.zipcoder</groupId>
<artifactId>mesolab-string</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

</project>
268 changes: 268 additions & 0 deletions src/main/java/com/zipcode/stringslab/DriversLicense.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
package com.zipcode.stringslab;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
* Create a DriversLicense class with fields found on a typical driver's license. Write a serializeToCSV method that
* returns a string of comma separated values for the fields in the Driver's License, and a static getCSVHeader method
* that produces the comma separated header for the CSV fields.
* <p>
* Created by vidyachandasekhar on 5/21/17.
*/


public class DriversLicense {

private String lastName;
private String firstName;
private String address;
private String state;
private String licenseNumber;
private String dateOfBirth;
private String dateOfIssue;
private String dateOfExpiry;
private String gender;
private String eyeColor;
private String height;
private String organDonor;
private String licenseClass;

private static DateFormat df = new SimpleDateFormat("MM/dd/yyyy");

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 getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getLicenseNumber() {
return licenseNumber;
}

public void setLicenseNumber(String licenseNumber) {
this.licenseNumber = licenseNumber;
}

public String getDateOfBirth() {
return dateOfBirth;
}

public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}

public String getDateOfIssue() {
return dateOfIssue;
}

public void setDateOfIssue(String dateOfIssue) {
this.dateOfIssue = dateOfIssue;
}

public String getDateOfExpiry() {
return dateOfExpiry;
}

public void setDateOfExpiry(String dateOfExpiry) {
this.dateOfExpiry = dateOfExpiry;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public String getEyeColor() {
return eyeColor;
}

public void setEyeColor(String eyeColor) {
this.eyeColor = eyeColor;
}

public String getHeight() {
return height;
}

public void setHeight(String height) {
this.height = height;
}

public String getOrganDonor() {
return organDonor;
}

public void setOrganDonor(String organDonor) {
this.organDonor = organDonor;
}

public String getLicenseClass() {
return licenseClass;
}

public void setLicenseClass(String licenseClass) {
this.licenseClass = licenseClass;
}

// a string of comma separated values for the fields in the Driver's License
public String serializeToCSV() {

StringBuilder stringBuilder = new StringBuilder();

stringBuilder.append(lastName);
stringBuilder.append(',');
stringBuilder.append(firstName);
stringBuilder.append(',');
stringBuilder.append(address);
stringBuilder.append(',');
stringBuilder.append(state);
stringBuilder.append(',');
stringBuilder.append(licenseNumber);
stringBuilder.append(',');
stringBuilder.append(dateOfBirth);
stringBuilder.append(',');
stringBuilder.append(dateOfIssue);
stringBuilder.append(',');
stringBuilder.append(dateOfExpiry);
stringBuilder.append(',');
stringBuilder.append(gender);
stringBuilder.append(',');
stringBuilder.append(eyeColor);
stringBuilder.append(',');
stringBuilder.append(height);
stringBuilder.append(',');
stringBuilder.append(organDonor);
stringBuilder.append(',');
stringBuilder.append(licenseClass);

return stringBuilder.toString();
}

//produces the comma separated header for the CSV fields
public static String getCSVHeader() {
return "LAST_NAME,FIRST_NAME,ADDRESS,STATE,LICENSE_NUMBER,DATE_OF_BIRTH,DATE_OF_ISSUE,DATE_OF_EXPIRY," +
"GENDER,EYES_COLOR,HEIGHT,ORGAN_DONOR,LICENSE_CLASS";
}

// takes a string representing the contents of a CSV file produced using getCSVHeader and serializeToCSV
// and reproduces a List of Driver's License objects that match the contents of the file.
public static List<DriversLicense> deserializeFromCSV(String licenseDataCSVString) {

BufferedReader bufferedReader = new BufferedReader(new StringReader(licenseDataCSVString));
List<DriversLicense> returnList = new ArrayList<>();
try {
String line = bufferedReader.readLine();
boolean isHeader = true;


while (line != null) {

if (isHeader) {
isHeader = false;
line = bufferedReader.readLine();
continue;
}

// use comma as separator
String[] licenseDataArray = line.split(",");

DriversLicense driversLicense = new DriversLicense();
driversLicense.setLastName(licenseDataArray[0]);
driversLicense.setFirstName(licenseDataArray[1]);
driversLicense.setAddress(licenseDataArray[2]);
driversLicense.setState(licenseDataArray[3]);
driversLicense.setLicenseNumber(licenseDataArray[4]);

driversLicense.setDateOfBirth(licenseDataArray[5]);
driversLicense.setDateOfIssue(licenseDataArray[6]);
driversLicense.setDateOfExpiry(licenseDataArray[7]);

driversLicense.setGender(licenseDataArray[8]);
driversLicense.setEyeColor(licenseDataArray[9]);
driversLicense.setHeight(licenseDataArray[10]);
driversLicense.setOrganDonor(licenseDataArray[11]);
driversLicense.setLicenseClass(licenseDataArray[12]);

returnList.add(driversLicense);
line = bufferedReader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error occurred while processing the CSV file");
}
return returnList;

}
/*
*
Part 2: Add methods for serializing to and deserializing from JSON. Once you have these methods, add new serialize
and deserialize methods that take an additional string listing the desired format (CSV or JSON) and produce
the desired result by delegating to the existing serialization and deserialization methods. These methods
should throw an appropriate exception if an unsupported format is requested. */

public String serializeToJSON() {
String jsonValue = null;
ObjectMapper objectMapper = new ObjectMapper();
try {
jsonValue = objectMapper.writeValueAsString(this);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return jsonValue;
}

public static List<DriversLicense> deserializeFromJSON(String licenseDataJsonString) {
ObjectMapper objectMapper = new ObjectMapper();
List<DriversLicense> listCar = null;
try {
listCar = objectMapper.readValue(licenseDataJsonString, new TypeReference<List<DriversLicense>>() {
});
} catch (IOException e) {
e.printStackTrace();
}

return listCar;

}

}
Loading