Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

2 questions, please help!

hello all, i just have a couple of questions (im also posting some code)

1. if you would, please take a look at my code and let me know if my compareTo method is the best way to compare what i am trying to compare. i am basically trying to make a program that will read in 3 text files, and then build 3 lists based on the info in the text files. well the class i am posting (Order) is just one of the three main classes that will be used to sort through and determine the alphabetical order the information will be in. so please take a look and tell me if it looks okay.

2. also, i am using jGrasp as my editor and i am trying to create a workbench so i can test my compareTo method. i tried to create 2 instances so i could compare them, but it keeps throwing an error when i try to enter a mock Date. the error says something along the lines of: int cannot be converted to type java.util.Date. i was just wondering why it is throwing this error?

well please take a look and please help! thanks in advance!

import java.util.Comparator;
import java.util.Date;

public class Order implements Comparable <Order>
{
private String lastName;
private String firstName;
private String title;
private Date dateOfPurchase;

public Order(String firstName, String lastName, String title, Date dateOfPurchase)
{
this.lastName = lastName;
this.firstName = firstName;
this.title = title;
this.dateOfPurchase = dateOfPurchase;
}

public void setLastName(String lastName)
{
this.lastName = lastName;
}

public String getLastName()
{
return lastName;
}

public void setFirstName(String firstName)
{
this.firstName = firstName;
}

public String getFirstName()
{
return firstName;
}

public void setTitle(String title)
{
this.title = title;
}

public String getTitle()
{
return title;
}

public void setDateOfPurchase(Date dateOfPurchase)
{
this.dateOfPurchase = dateOfPurchase;
}

public Date getDateofPurchase()
{
return dateOfPurchase;
}

public int compareTo(Order anOrder)
{
int result;

result = this.dateOfPurchase.compareTo(anOrder.dateOfPurchase);

if (result == 0)
{
result = this.lastName.compareTo(anOrder.lastName);
}
else
{
if(result ==0)
{
result = this.firstName.compareTo(anOrder.firstName);
}
else
{
result = this.title.compareTo(anOrder.title);
}
}

return result;

}

public boolean equals(Order anOrder)
{

boolean result;

result = (this.dateOfPurchase.equals(anOrder.dateOfPurchase) && this.lastName.equals(anOrder.lastName)
&& this.firstName.equals(anOrder.firstName) && this.title.equals(anOrder.title));

return result;
}
[3624 byte] By [chr1zis] at [2007-11-11 6:43:09]
# 1 Re: 2 questions, please help!
One thing to note with the code:

public int compareTo(Order anOrder)
{
int result;

result = this.dateOfPurchase.compareTo(anOrder.dateOfPurcha se);

if (result == 0)
{
result = this.lastName.compareTo(anOrder.lastName);
}
else
{
if(result ==0) // This line is always false and so is redundent
{
result = this.firstName.compareTo(anOrder.firstName);
}
else
{
result = this.title.compareTo(anOrder.title);
}
}

return result;

}

Hope this helps.
evlich at 2007-11-11 22:40:27 >