在我的人员类中,我应该有一个toCSV()
方法,该方法将为我的字段返回一个字符串。
copy
方法,它接受Person p
并复制所有数据。 copy
方法。 clone
方法,该方法返回一个Person
及其数据副本。 我根据我的要求编写了方法,但是我仍然对所要查找的内容感到困惑。
我觉得每种方法的代码都非常多余,而不是问题所在。
我尚未解决克隆方法,因为我认为上述三种方法不正确。
public class Person {
private String firstName;
private String lastName;
private String address;
private String city;
private String state;
private String zipCode;
public Person (String firstName, String lastName, String address, String city, String state, String zipCode) {
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return this.city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
@Override
public String toString() {
return "FirstName: " + firstName + "\nLastName: " + lastName + "\nAddress: " + address + "\nCity: " + city + "\nState: " + state + "\nZipCode: " + zipCode;
}
public void toCSV() throws FileNotFoundException {
PrintWriter file = new PrintWriter(new File("Customer.csv"));
file.append(this.firstName);
file.append(",");
file.append(this.lastName);
file.append(",");
file.append(this.address);
file.append(",");
file.append(this.city);
file.append(",");
file.append(this.state);
file.append(",");
file.append(this.zipCode);
}
public void copy(Person p) throws FileNotFoundException {
PrintWriter file = new PrintWriter(new File("Customer.csv"));
file.append(p.firstName);
file.append(p.lastName);
file.append(p.address);
file.append(p.city);
file.append(p.state);
file.append(p.zipCode);
}
public void copy(String firstName, String lastName, String address, String city, String state, String zipCode) throws FileNotFoundException {
PrintWriter file = new PrintWriter(new File("Customer.csv"));
file.append(firstName);
file.append(lastName);
file.append(address);
file.append(city);
file.append(state);
file.append(zipCode);
}
说实话,我不确定确切的结果。
我了解的是它应该返回一个输入行为firstName,lastName,address,city,state,zipcode
的文件,但我还没有运行它,因为我还缺少很多其他内容。
但是,我认为我的问题是每种方法都将创建自己的新文件,并且我认为它们都应在同一Customer.csv
下。
我知道这很模糊,但是我只需要一些帮助,以查看我的代码是否在正确的轨道上,或者它完全是多余的,并且无法按预期工作。
答案 0 :(得分:-1)
public void toCSV( File customerFile) throws FileNotFoundException {
doFileOp(customerFile, new Person (this.firstName, this.lastName, this.address, this.city, this.state, this.zipCode));
}
public void copy(Person p, File customerFile) throws FileNotFoundException {
doFileOp(customerFile, p);
}
public void copy(String firstName, String lastName, String address, String city, String state, String zipCode, File customrFile) throws FileNotFoundException {
doFileOp(customerFile, new Person (firstName, lastName, address, city, state, zipCode));
}
private void doFileOp(File customerFile, Person p) {
PrintWriter file = new PrintWriter(customerFile);
customerFile.append(p.firstName); file.append(",");
customerFile.append(p.lastName); file.append(",");
customerFile.append(p.address); file.append(",");
customerFile.append(p.city); file.append(",");
customerFile.append(p.state); file.append(",");
customerFile.append(p.zipCode); file.append(",");
}