package abstract_and_interface_example;
import java.io.*;
public class Engine implements Saveable{
public static final int DEF_NUMCYLINDERS = 4;
public static final int DEF_CAPACITY = 1300;
private int numCylinders;
private int capacity;
public Engine(int numCylinders, int capacity) {
this.numCylinders = numCylinders;
this.capacity = capacity;
}
public Engine() {
this(DEF_NUMCYLINDERS, DEF_CAPACITY);
}
public int getNumCylinders() {
return this.numCylinders;
}
public int getCapacity() {
return this.capacity;
}
public void setNumCylinders(int numCylinders) {
this.numCylinders = numCylinders;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
// The following two methods ar needed in order for this class to implement
// the Saveable interface.
public void writeToBinaryFile(DataOutputStream theFile) throws IOException{
theFile.writeInt(numCylinders);
theFile.writeInt(capacity);
}
public void readFromBinaryFile(DataInputStream theFile) throws IOException {
numCylinders = theFile.readInt();
capacity = theFile.readInt();
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
public String toString() {
StringBuilder toStringBuilder = new StringBuilder();
toStringBuilder.append("[");
toStringBuilder.append(super.toString());
toStringBuilder.append(",numCylinders: ");
toStringBuilder.append(numCylinders);
toStringBuilder.append(",capacity: ");
toStringBuilder.append(capacity);
toStringBuilder.append("]");
return toStringBuilder.toString();
}
public void readFromTextFile(BufferedReader fi) throws IOException {
this.numCylinders =Integer.parseInt(fi.readLine());
this.capacity = Integer.parseInt(fi.readLine());
}
public void writeToTextFile(PrintWriter fo) throws IOException {
fo.println(this.numCylinders);
fo.println(this.capacity);
}
}