Created
November 20, 2012 23:42
-
-
Save jobscry/4122049 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//Course.java | |
//This domain object represents a course. The data attributes are | |
//all package-private to allow access to them in the CourseService class. | |
package domain; | |
public class Course implements java.io.Serializable { | |
int objectID; | |
String semester; | |
String number; | |
String title; | |
//The default constructor is public to be accessible from JSP View pages. | |
public Course() {this(-1,"","","");} | |
//The full constructor is package-private to prevent misuse. | |
//The CourseServer method createCourse should be used to create | |
//a new Course object. | |
Course(int oID,String s,String n,String t) { | |
objectID=oID; | |
semester=s; | |
number=n; | |
title=t; | |
} | |
public String getSemester() {return semester;} | |
public String getNumber() {return number;} | |
public String getTitle() {return title;} | |
public void setObjectID(int id) {objectID=id;} | |
public int getObjectID() {return objectID;} | |
} | |
//Student.java | |
//This domain object represents a Student. The data attributes are | |
//all package-private to allow access to them in the RegisterService class. | |
package domain; | |
public class Student implements java.io.Serializable { | |
int objectID; | |
String name; | |
String address; | |
String city; | |
String state; | |
String zipCode; | |
//The default constructor is public to be accessible from JSP View pages. | |
public Student() {this(-1,"","","","","");} | |
//The full constructor is package-private to prevent misuse. The | |
//RegisterServer method createStudent should be used to create | |
//a new Student object. | |
Student(int oID,String n,String a,String c,String s,String zc){ | |
objectID=oID; | |
name=n; | |
address=a; | |
city=c; | |
state=s; | |
zipCode=zc; | |
} | |
public String getName() {return name;} | |
public String getAddress() {return address;} | |
public String getCity() {return city;} | |
public String getState() {return state;} | |
public String getZipCode() {return zipCode;} | |
public void setObjectID(int id) {objectID=id;} | |
public int getObjectID() {return objectID;} | |
} | |
//RegisterService.java | |
//This object performs course registration services – it acts as a | |
//Facade into the business logic of registering a Student for a Course. | |
//NOTE: Ideally, the storage access must be decoupled in a separate class | |
//as we will demonstrate in module 4. | |
package domain; | |
import java.util.*; | |
import java.io.*; | |
public class RegisterService { | |
public RegisterService() {} | |
//This method finds a specified Course object | |
public Course getCourse(String semester, String number, String path) { | |
//Delegate to the course service | |
CourseService courseSvc=new CourseService(); | |
return courseSvc.getCourse(semester, number, path); | |
} | |
public Student createStudent(String name,String address, | |
String city,String state,String zipCode) { | |
return new Student(-1,name,address,city,state,zipCode);} | |
public void register(Course course, Student student, String path) | |
throws IOException { | |
BufferedWriter buffer; | |
try { | |
//Store the Student object into Students.txt file | |
buffer=new BufferedWriter(new FileWriter(path+"Students.txt",true)); | |
buffer.write(student.getName()+" "+ student.getAddress()+ | |
" "+student.getCity()+" "+ | |
student.getState()+" "+student.getZipCode()); | |
buffer.newLine(); | |
buffer.close(); | |
buffer=new BufferedWriter( | |
new FileWriter(path+"Registrations.txt",true)); | |
buffer.write(student.getName()+" "+course.getTitle()); | |
buffer.newLine(); | |
buffer.close(); | |
} | |
catch (IOException exception) {} | |
} | |
} | |
//CourseService.java | |
//This object performs Course services, such as retrieving a Course object | |
// from the Courses.txt file, or creating a new Course object. | |
//NOTE: Ideally, the storage access must be decoupled in a separate class | |
//as we will demonstrate in module 4. | |
package domain; | |
import java.util.*; | |
import java.io.*; | |
public class CourseService { | |
//This method finds a specified Course object in the Courses.txt file. | |
public Course getCourse(String semester, String number, String path) { | |
Course course=null; | |
String sem; | |
String num; | |
String ttl; | |
try { | |
Scanner scanner=new Scanner(new FileReader(path+"Courses.txt")); | |
while (scanner.hasNext()){ | |
sem=scanner.next(); | |
num=scanner.next(); | |
ttl=scanner.nextLine(); | |
if (sem.equals(semester) && num.equals(number)) { | |
course=new Course(-1,sem,num,ttl); | |
break; | |
} | |
} | |
} | |
catch (FileNotFoundException exception) {} | |
return course; | |
} | |
//This method adds a new Course object to the Courses.txt file. | |
public Course createCourse(String semester, String number, | |
String title, String path) { | |
Course course=new Course(-1,semester,number,title); | |
BufferedWriter buffer; | |
try { | |
buffer=new BufferedWriter(new FileWriter(path+"Courses.txt",true)); | |
buffer.write(semester+" "+number+" "+title); | |
buffer.newLine(); | |
buffer.close(); | |
} | |
catch (IOException exception) {} | |
return course; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment