This is my assignment
Starting with last week's Property project, build an array of Properties in your main(). Add static methods to the Property class:
void ArraySort(Property[] property_list, String key_field)
This will sort the array, property_list, based on the argument, key_field, passed in. These are the legal choices for key_field:
"byPrice"
"byNumRooms"
"byDaysOnMarket"
any other String should cause nothing to be sorted, and a silent return results.
void PrintArray(String title, Property property_list[])
This will print the array, property_list, with a header line that contains the String title. No changes to the array should be attempted.
Additional static class finals such as PRICE, DAYS, etc. should be defined to hold the strings "byPrice", "byDaysOnMarket", etc., so that you do not have to use the literals "byPrice", etc. anywhere in your code. This will allow a future programmer to change each string by modifying these constants in one place and one place, only, namely in their static definitions.
For your test main(), create a an array of five or more Properties, then sort and list them, passing different sort keys each time
HINT: For those of you who are rusty on sorting (a topic from CIS 27a), you can review this topic in Week 3, which is open to you now. You will have to create a private instance method to the Property class, compareTo() with the signature.
int compareTo(Property prop, String key_field)
and have this method return a number that is < 0, = 0 or > 0 depending on whether the calling object, this, is <, = or > the parameter prop (based on the key_field criterion, of course).
I can't figure FloatLargestToTop and CompareTo part.
This is my code so far
// CIS 27B Assignment 1A Instructor's Solution
import javax.swing.*;
import java.util.Arrays;
public class Foothill
{
public static void main(String[] args)
{
Property[] Properties ;
Properties = new Property[5] ;
Properties[1]= new Property(Property.SFD, 999000, 2, 10, "Sheridan");
Properties[2]=new Property(Property.CND, 650000, 2, 14, "100 El Camino");
Properties[3]=new Property(Property.SFD, 999000, 2, 10, "Sheridan");
Properties[4]=new Property();
}
}
class Property
{
// class constants
public final static int RNT = 0;
public final static int CND = 1;
public final static int SFD = 2;
public final static int MAX_ROOMS = 20;
static final int MAX_PRICE = 10000000;
public final static int CONVERSION_FACTOR = 300;
public final static int MAX_ADDY_LENGTH = 500;
public final static int MAX_DAYS_ON_MARKET = 365 * 5; // 5 years
public final static String UNDEFINED = "[ undefined ]"; // for undefined strings
static final String PRICE = "ByPrice";
// private members
private int type;
private int price;
private int bedrooms;
private int days_on_market;
private String address;
static void ArraySort(Property[] property_list, String PRICE)
{
for (int k = 0; k < property_list.length; k++)
if ( !FloatLargestToTop(property_list, property_list.length-1-k) )
return;
}
private static boolean FloatLargestToTop(Property[] data, int top)
{
}
private int compareTo(Property prop, String PRICE)
{
return 0;
}
static void PrintArray(String title, Property property_list[])
{
}
// constructors
public Property()
{
InitDefaults();
}
public Property(int type, int price, int bedrooms,
int days_on_market, String address)
{
// set defaults by calling sibling constructor in case of bad parameters
this();
// use the mutators to filter bad data - do not repeat here
SetType(type);
SetPrice(price);
SetBedrooms(bedrooms);
SetDaysOnMarket(days_on_market);
SetAddress(address);
}
// mutators
public boolean SetType(int type)
{
switch (type)
{
case RNT: case CND: case SFD:
this.type = type;
return true;
default:
return false;
}
}
public boolean SetPrice(int price)
{
if ( price > 0 && price <= MAX_PRICE)
{
this.price = price;
return true;
}
else
return false;
}
public boolean SetBedrooms(int bedrooms)
{
if (bedrooms > 0 && bedrooms < MAX_ROOMS)
{
this.bedrooms = bedrooms;
return true;
}
else
return false;
}
public boolean SetDaysOnMarket(int days_on_market)
{
if (days_on_market > 0 && days_on_market < MAX_DAYS_ON_MARKET)
{
this.days_on_market = days_on_market;
return true;
}
else
return false;
}
public boolean SetAddress(String address)
{
if (address.length() > 0 && address.length() < MAX_ADDY_LENGTH)
{
this.address = address;
return true;
}
else
return false;
}
// accessors
public int GetType()
{
return type;
}
public int GetPrice()
{
return price;
}
public int GetBedrooms()
{
return bedrooms;
}
public int GetDaysOnMarket()
{
return days_on_market;
}
public String GetAddress()
{
return address;
}
// Stringizers
public String StringizeType()
{
switch (type)
{
case RNT:
return "Rental";
case CND:
return "Condo";
case SFD:
return "Single Family Dwelling";
default:
return "";
}
}
public void ConvertToRental()
{
if (type != RNT)
{
type = RNT;
days_on_market = 1;
price = price / CONVERSION_FACTOR;
}
}
public void PrintListing()
{
String return_string = "\n--- Listing ---\n";
return_string = return_string + "Price: " + price + "\n";
return_string = return_string + address + "\n";
return_string = return_string + StringizeType() + "\n";
return_string = return_string + "Number of Bedrooms: " + bedrooms + "\n";
return_string = return_string + "Days On Market: " + days_on_market + "\n";
System.out.println(return_string);
}
void InitDefaults()
{
type = SFD;
price = bedrooms = days_on_market = 0;
address = UNDEFINED;
}
}