Answer:
import java.io.*;
import java.util.*;
class Pet {
String type;
String name;
double adoptcost;
Pet() {
this.adoptcost = 0.0;
this.name = "";
this.type = "";
}
Pet(String name, String type, double cost) {
this.adoptcost = cost;
this.name = name;
this.type = type;
}
public String toString() {
return "Name: " + this.name + "\\Type: " + this.type + "\\Cost: " + this.adoptcost + "\\";
}
public boolean equals(Object p) {
if (p == this) {
return true;
}
if (!(p instanceof Pet)) {
return false;
}
Pet a = (Pet) p;
return this.type.equals(a.type) && this.name.equals(a.name);
}
public double compareTo(Pet p) {
if (this.adoptcost == p.adoptcost)
return 0.0;
else
return (this.adoptcost - p.adoptcost);
}
public static void main(String args[]) throws IOException {
BufferedReader fr = new BufferedReader(new FileReader(new File("rescue.txt")));
String line, name, type, tmp;
double cost;
ArrayList<Pet> arr = new ArrayList<>();
while ((line = fr.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line, ",");
name = st.nextToken();
type = st.nextToken();
tmp = st.nextToken();
cost = Double.parseDouble(tmp);
arr.add(new Pet(name, type, cost));
}
for (Pet i : arr) {
System.out.println(i);
}
System.out.println("Equals check for 1st and 2nd pet object: " + arr.get(0).equals(arr.get(1)));
System.out.println("CompareTo check for 1st and 3rd pet object: " + arr.get(0).compareTo(arr.get(2)));
System.out.println("CompareTo check for 1st and 4th pet object: " + arr.get(0).equals(arr.get(3)));
fr.close();
}
}