在我的代码示例中,您将获得大量代码。
然而,问题在于restoreCourse方法(在代码的最后),我试图将所有数据(已经从StorageManager检索到)恢复到我的字符串数组(名为lineBuffer)然后使用该数据用于恢复类Course的对象并在方法结束时返回它。
public class Course {
private String name;
private int courseID;
private String signUpCode;
private String courseGuide;
private String courseScheme;
private String courseGrade;
private String courseSpeed;
private String courseCredit;
public Course(String name,int courseID,String signUpCode,
String courseGuide,String courseScheme,String courseSpeed,String
courseCredit){
this.name = name;
this.courseID = courseID;
this.signUpCode= signUpCode;
this.courseGuide = courseGuide;
this.courseScheme = courseScheme;
this.courseSpeed = courseSpeed;
this.courseCredit = courseCredit;
this.courseGrade = "";
}
public int getCourseID(){
return courseID;
}
public String getCourseName(){
return name;
}
public String getSignUpCode(){
return signUpCode;
}
public String getCourseScheme(){
return courseScheme;
}
public String getCourseGuide(){
return courseGuide;
}
public String getCourseGrade(){
return courseGrade;
}
public String getCourseSpeed(){
return courseSpeed;
}
public String getCourseCredit(){
return courseCredit;
}
@Override
public String toString() {
StringBuffer msg = new StringBuffer();
msg.append (courseID);
msg.append ("-");
msg.append (name);
msg.append ("-");
msg.append (signUpCode);
msg.append ("-");
msg.append (courseGuide);
msg.append ("-");
msg.append (courseScheme);
msg.append ("-");
msg.append (courseGrade);
msg.append ("-");
msg.append (courseSpeed);
msg.append ("-");
msg.append (courseCredit);
msg.append ("\n");
return msg.toString();
}
}
public class FileCourseDAO implements CourseDAO {
public void saveCourse(Course course){
StringBuffer dataBuffer = new StringBuffer();
dataBuffer.append (course.getCourseID());
dataBuffer.append ("-");
dataBuffer.append (course.getCourseName());
dataBuffer.append ("-");
dataBuffer.append (course.getSignUpCode());
dataBuffer.append ("-");
dataBuffer.append (course.getCourseGuide());
dataBuffer.append ("-");
dataBuffer.append (course.getCourseScheme());
dataBuffer.append ("-");
dataBuffer.append (course.getCourseGrade());
dataBuffer.append ("-");
dataBuffer.append (course.getCourseSpeed());
dataBuffer.append ("-");
dataBuffer.append (course.getCourseCredit());
dataBuffer.append ("\n");
StorageManager packer = StorageManager.getInstance();
try {
packer.storeStringData (dataBuffer.toString().trim());
} catch (IOException e){
System.out.println ("Storage manager Error:");
e.printStackTrace ();
}
}
public Course restoreCourse(int courseID){
StorageManager packer = StorageManager.getInstance();
if (!packer.storeExists ()){
System.out.println ("No file with data.");
return;
}
try {
String[] lineBuffer = null;
List<String> dataBuffer = packer.readData();
for (String dataLine : dataBuffer){
lineBuffer = dataLine.split(" ");
}
Course course = new Course ();
} catch (IOException e) {
System.out.println ("Error while reading data file.");
}
return course;
}
}
答案 0 :(得分:0)
我想恢复对象(我保存在方法&#34; saveCourse&#34;作为文本文件)并返回对象,使用方法&#34; restoreCourse&#34;。
saveCourse
中序列化了由-
分隔的课程字段;因此,在拆分该字符串时,您必须使用dataLine.split("-")
而不是dataLine.split(" ")
。Course
类的实例并在不初始化的情况下返回它。拆分后,使用这些数据初始化课程对象并将其返回给调用者。 以下代码可能有效:
public Course restoreCourse(int courseID){
StorageManager packer = StorageManager.getInstance();
if (!packer.storeExists ()){
System.out.println ("No file with data.");
return;
}
Course course = null;
try {
String[] lineBuffer = null;
List<String> dataBuffer = packer.readData();
for (String dataLine : dataBuffer){
lineBuffer = dataLine.split("-");
if (lineBuffer.length > 7 && courseID == lineBuffer[0]) {
course = new Course(lineBuffer[1],lineBuffer[0],lineBuffer[2], lineBuffer[3],lineBuffer[4],lineBuffer[6],lineBuffer[7] );
}
}
} catch (IOException e) {
System.out.println ("Error while reading data file.");
}
return course;
}
注意:
注意构造函数参数的顺序和saveCourse()
方法中字段的顺序(它们的顺序不一样);我试图匹配它们但检查自己是否按照正确的顺序