Created
October 7, 2013 01:31
-
-
Save cannonpalms/6861327 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Person { | |
private String name; | |
private String email; | |
private String birthday; | |
private String phone; | |
private String address; | |
private String medicalHistory; | |
public Person() { | |
} | |
public String getName() { | |
return name; | |
} | |
public String getEmail() { | |
return email; | |
} | |
public String getBirthday() { | |
return birthday; | |
} | |
public String getPhone() { | |
return phone; | |
} | |
public String getAddress() { | |
return address; | |
} | |
public String getMedicalHistory() { | |
return medicalHistory; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public void setEmail(String email) { | |
this.email = email; | |
} | |
public void setBirthday(String birthday) { | |
this.birthday = birthday; | |
} | |
public void setPhone(String phone) { | |
this.phone = phone; | |
} | |
public void setAddress(String address) { | |
this.address = address; | |
} | |
public void setMedicalHistory(String medicalHistory) { | |
this.medicalHistory = medicalHistory; | |
} | |
@Override | |
public String toString() { | |
String s = "name: " + name + "\n" | |
+ "email: " + email + "\n" | |
+ "birthday: " + birthday + "\n" | |
+ "phone: " + phone + "\n" | |
+ "address: " + address + "\n" | |
+ "medicalHistory: " + medicalHistory + "\n"; | |
return s; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.nio.charset.Charset; | |
import java.nio.file.Paths; | |
import java.util.ArrayList; | |
import java.util.HashSet; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Set; | |
public class RecordParser { | |
private File recordFile; | |
private Set<String> fieldsRead; | |
public RecordParser(File recordFile) { | |
this.recordFile = recordFile; | |
this.fieldsRead = new HashSet<String>(); | |
} | |
public List<Person> parseList() { | |
try { | |
List<String> lines = java.nio.file.Files.readAllLines( | |
Paths.get(recordFile.getPath()), | |
Charset.defaultCharset()); | |
return parseDataList(lines); | |
} catch (Exception e) { | |
} | |
return null; | |
} | |
private List<Person> parseDataList(List<String> lines) { | |
List<Person> people = new ArrayList<Person>(); | |
Iterator<String> i = lines.iterator(); | |
Person cp = new Person(); | |
while (i.hasNext()) { | |
String line = i.next(); | |
while (i.hasNext() && line.length() == 0) { | |
line = i.next(); | |
} | |
if (line.startsWith("name")) { | |
if (fieldsRead.contains("name")) { | |
people.add(cp); | |
cp = new Person(); | |
fieldsRead.clear(); | |
} | |
int t = startPos(line); | |
cp.setName(line.substring(t)); | |
fieldsRead.add("name"); | |
} else if (line.startsWith("birthday")) { | |
if (fieldsRead.contains("birthday")) { | |
people.add(cp); | |
cp = new Person(); | |
fieldsRead.clear(); | |
} | |
int t = startPos(line); | |
cp.setBirthday(line.substring(t)); | |
fieldsRead.add("birthday"); | |
} else if (line.startsWith("phone")) { | |
if (fieldsRead.contains("phone")) { | |
people.add(cp); | |
cp = new Person(); | |
fieldsRead.clear(); | |
} | |
int t = startPos(line); | |
cp.setPhone(line.substring(t)); | |
fieldsRead.add("phone"); | |
} else if (line.startsWith("email")) { | |
if (fieldsRead.contains("email")) { | |
people.add(cp); | |
cp = new Person(); | |
fieldsRead.clear(); | |
} | |
int t = startPos(line); | |
cp.setEmail(line.substring(t)); | |
fieldsRead.add("email"); | |
} else if (line.startsWith("medicalHistory")) { | |
if (fieldsRead.contains("medicalHistory")) { | |
people.add(cp); | |
cp = new Person(); | |
fieldsRead.clear(); | |
} | |
int t = startPos(line); | |
cp.setMedicalHistory(line.substring(t)); | |
fieldsRead.add("medicalHistory"); | |
} else if (line.startsWith("address")) { | |
if (fieldsRead.contains("address")) { | |
people.add(cp); | |
cp = new Person(); | |
fieldsRead.clear(); | |
} | |
int t = startPos(line); | |
cp.setAddress(line.substring(t)); | |
fieldsRead.add("address"); | |
} | |
} | |
return people; | |
} | |
private static int startPos(String line) { | |
return line.lastIndexOf('\t') + 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment