Friday, December 28, 2012

StudentDAO with FileReader and FilerWriter


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.*;

public class StudentDAO {
public static String fileName = "//data//temp//SurveyData.txt";

public static void SaveStudentSurveyFormData(StudentBean postData) throws IOException{
Surveys surveys = GetSurveys();
surveys.data.add(postData);
SaveFile(new Gson().toJson(surveys));
}

public static StudentBean GetStudentSurveyData(String studentId) throws IOException{
Surveys surveys = GetSurveys();
for(StudentBean survey : surveys.data){
if(survey.getStudentId().equalsIgnoreCase(studentId)){
return survey;
}
}
return null;
}

public static Surveys GetSurveys() throws IOException{
Surveys surveys = null;
String fileText = ReadFile();
if(fileText.length() > 0){
surveys = new Gson().fromJson(fileText, Surveys.class);
}
else{
surveys = new Surveys();
surveys.data = new ArrayList<StudentBean>();
}
return surveys;
}

public static List<String> GetSurveyIds() throws IOException{
List<String> surveyIds = new ArrayList<String>();
Surveys surveys = GetSurveys();
for(StudentBean survey : surveys.data){
surveyIds.add(survey.getStudentId());
}
return surveyIds;
}

public static String ReadFile() throws IOException{
BufferedReader file = new BufferedReader(new FileReader(fileName));
String line;
StringBuffer fileText = new StringBuffer();
while((line=file.readLine()) != null){
fileText.append(line);
}
file.close();
return fileText.toString();
}

public static void SaveFile(String fileText) throws IOException{
FileWriter fWriter = new FileWriter(fileName,false);
BufferedWriter writer = new BufferedWriter(fWriter);
fWriter.append(fileText);
writer.flush();
writer.close();
}
}


import java.util.ArrayList;

public class Surveys {
public ArrayList<StudentBean> data;
}


public class StudentBean {
private String studentId;
private String firstName;
private String lastName;
private String street;
private String zip;
private String city;
private String state;
private String telephone;
private String email;
private String url;
private String dateOfSurvey;
private String graduationMonth;
private String graduationYear;
private String likes;
private String interests;
private String recommendation;
private String comments;

public StudentBean(String studentId, String firstName, String lastName, String street, String zip,
String city, String state, String telephone, String email, String url, String dateOfSurvey,
String graduationMonth, String graduationYear, String likes, String interests,
String recommendation, String comments){
this.studentId = studentId;
this.firstName = firstName;
this.lastName = lastName;
this.street = street;
this.zip = zip;
this.city = city;
this.state = state;
this.telephone = telephone;
this.email = email;
this.url = url;
this.dateOfSurvey = dateOfSurvey;
this.graduationMonth = graduationMonth;
this.graduationYear = graduationYear;
this.likes = likes;
this.interests = interests;
this.recommendation = recommendation;
this.comments = comments;
}

public String getStudentId(){
return this.studentId;
}

public String getFirstName(){
return this.firstName;
}

public String getLastName(){
return this.lastName;
}

public String getStreet(){
return this.street;
}

public String getZip(){
return this.zip;
}

public String getCity(){
return this.city;
}

public String getState(){
return this.state;
}

public String getTelephone(){
return this.telephone;
}

public String getEmail(){
return this.email;
}

public String getUrl(){
return this.url;
}

public String getDateOfSurvey(){
return this.dateOfSurvey;
}

public String getGraduationMonth(){
return this.graduationMonth;
}

public String getGraduationYear(){
return this.graduationYear;
}

public String getLikes(){
return this.likes;
}

public String getInterests(){
return this.interests;
}

public String getRecommendation(){
return this.recommendation;
}

public String getComments(){
return this.comments;
}
}


No comments: