Create a project called \"Problem2\" in the workspace described above and use it
ID: 3554355 • Letter: C
Question
Create a project called "Problem2" in the workspace described above and use it for all the code for this problem. Implement the class and interface shown in the diagram below. The save function should cause the object to append all of its fields to a text file by calling a custom to string function, with each field separated from the others by a comma (i.e, you are creating a CSV file). Create code in your main function to test your code by creating 4 customers and then saving them. Use customers. csv as output file name.Explanation / Answer
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class Customer implements Saveable{
private int customerId;
private String name;
private String address;
public Customer(int customerId, String name, String address) {
this.customerId = customerId;
this.name = name;
this.address = address;
}
@Override
public String toString() {
return customerId + "," + name + "," + address;
}
@Override
public void save() throws IOException {
Writer writer = new FileWriter("customers.csv", true);
writer.write(this.toString());
writer.write(" ");
writer.flush();
writer.close();
}
}
import java.io.IOException;
public interface Saveable {
public void save() throws IOException;
}
import java.io.IOException;
public class Problem2 {
public static void main(String[] args) throws IOException{
Customer bob = new Customer(1, "Bob", "1st Street");
bob.save();
Customer john = new Customer(2, "John", "2nd Street");
john.save();
Customer rick = new Customer(3, "Rick", "3rd Street");
rick.save();
Customer bill = new Customer(4, "Bill", "4th Street");
bill.save();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.