// HouseTest5.java
//
// Tests compareTo method,
// assuming a valid constructor and toString method.
// Data file must contain at least two lines.
import java.util.StringTokenizer;
public class HouseTest5
{
public static void main(String[] args)
{
if ( args.length == 0 )
{
System.out.println("Filename of data file needed"
+ " as command-line argument");
System.exit(0);
} // if
TextFileInput in = new TextFileInput(args[0]);
String line = in.readLine();
if ( line == null )
{
System.out.println("Empty file.");
System.exit(1);
} // if
House house1 = makeHouse(line);
line = in.readLine();
if ( line == null )
{
System.out.println("Only one line in file.");
System.exit(1);
} // if
do
{
House house2 = makeHouse(line);
if ( house1 != null )
testCompareToMethod(house1, house2);
house1 = house2;
line = in.readLine();
} while ( line != null );
} // method main
private static void testCompareToMethod(House x,
House y)
{
try
{
System.out.println("For x = {" + x + "}");
System.out.println(" y = {" + y + "}");
System.out.println("x " + operator(x.compareTo(y)) + " y");
} // try
catch ( RuntimeException re )
{
re.printStackTrace();
} // catch
} // method testCompareMethods
private static String operator(int comparison)
{
return ( ( comparison < 0 )
? "<"
: ( ( comparison > 0 )
? ">"
: "==" ) );
} // method operator
private static House makeHouse(String line)
{
try {
System.out.println("Will now try to instantiate: " + line);
StringTokenizer st = new StringTokenizer(line, "|");
String zipCode = st.nextToken();
int price = Integer.parseInt(st.nextToken());
int numberOfRooms = Integer.parseInt(st.nextToken());
House house = new House(zipCode, price, numberOfRooms);
System.out.println("Instantiated: " + house);
return house;
} // try
catch ( RuntimeException re )
{
re.printStackTrace();
return null;
} // catch
} // method makeHouse
} // class HouseTest5