0

Can somebody please help my try to understand how to add an equals method to my Person, Purchase, and Customer class? I am very new to Java and I can't figure out how to call the equals test from main and have it reference to the other classes. Any help would be great!!

// ****************************************
// **           Class Person             **
// **************************************** 

class Person {
	
  private String name;
  private String address;

  public Person( String n, String a ) {
  	name = n;
  	address = a;
  }
 
  public Person( String n ) {
  	name = n;
  	address = "Unknown";
  }
   
  public Person( Person p ) {
  	name = p.name;
  	address = p.address;
  }
  
  public String toString(){
  	return "\nName: " + name + "\nAddress: " + address;
  }

}  // end Person class

// ****************************************
// **          Class Purchase            **
// **************************************** 

class Purchase {
  private String item;
  private double amountSpent;
  
  public Purchase( String i, double a ) {
    item = i;
    amountSpent = a;
  }
  
  public String toString(){
  	return "\nItem: " + item + "\nCost: " + amountSpent;
  }  
  
}  // end Purchase

// ****************************************
// **          Class Customer            **
// **************************************** 

public class Customer extends Person {

  private Purchase myBuy;

// Constructor for aCust

  public Customer (String aname, String astreet, Purchase p){
  	super(aname, astreet); 	
  	myBuy = p;
  }
  
// Constructor for bCust  
  
  public Customer (String aname){
  	super(aname);
  	myBuy = new Purchase ("Unknown", 0.00); 	
  }  

// Constructor for cCust

  public Customer (String aname, String astreet){
  	super(aname, astreet); 
  	myBuy = new Purchase ("Unknown", 0.00); 	
  }  
  
// Constructor for dCust

  public Customer (Person per, Purchase p){
  	super(per); 
  	myBuy = p;	
  }  
  
// toString Constructor that return the toString's from Purchase
// and Person plus myBuy  
  
  public String toString(){
  	return super.toString() + myBuy.toString();
  } 

} // end Customer
import javax.swing.*;

  public class p3Drive {

   public static void main( String[] args ) {

// construct a Purchase object

     Purchase buy1 = new Purchase( "coffee pot", 34.56 );
     Purchase buy2 = new Purchase( "dishes", 54.98 );

     Person dude = new Person ("Minny Mouse","Disney Circle, Florida" );

// Instantiate a Customer object named aCust, by invoking a constructor that takes
// 3 arguments, a name string, address string  and  Purchase object
   
     Customer aCust = new Customer( "Matt Roberts","Flushing, Michigan", buy1 );

// Instantiate a Customer object named bCust, by invoking a constructor that takes
// only 1 argument, a name string (default values supplied for address and Purchase)
   
     Customer bCust = new Customer ( "A.E. Neuman");
     Customer cCust = new Customer ("A.E. Neuman","Mad Lane");
     Customer dCust = new Customer (dude, buy2);

//Invoke the Customer class toString() method for all customers

     String message = "My Customers are: \naCust: " + aCust + "\n\nbCust: "
                    + bCust.toString() + "\n\ncCust: " + cCust + "\n\ndCust: "+ dCust;

     JOptionPane.showMessageDialog(null,message,"Customer Output", JOptionPane.PLAIN_MESSAGE);

     message = "Are the two customers bCust and cCust are the same?\n ";

     boolean equalTest = bCust.equals(cCust);

     message = message + equalTest;

     JOptionPane.showMessageDialog(null,message,"Equals Test Result", JOptionPane.PLAIN_MESSAGE);

// Test customer equals() after assigning one customer to another

      dCust = aCust;
      
      message = "After assignment dCust = aCust; Are the two customers dCust and aCust are the same?\n";

      equalTest = dCust.equals(aCust);

      message = message + equalTest;

      JOptionPane.showMessageDialog(null,message,"Equals Test Result", JOptionPane.PLAIN_MESSAGE);

// Test equals for purchase objects.

      message = "The two purchase objects are: \n\nbuy1: " + buy1 + "\n\nbuy2: " + buy2+ "\n\n";

      JOptionPane.showMessageDialog(null,message,"Purchase Objects", JOptionPane.PLAIN_MESSAGE);
 
      message = "Are the two purchase objects equal\n";
      equalTest = buy1.equals(buy2);

      message = message + equalTest;

      JOptionPane.showMessageDialog(null,message,"Equals Test Result", JOptionPane.PLAIN_MESSAGE);

// Test equals for Person objects.

      message ="Is dcust equal to Person dude?\n";
      equalTest = dude.equals(dCust);
      message = message + equalTest;

      JOptionPane.showMessageDialog(null,message,"Equals Test Result", JOptionPane.PLAIN_MESSAGE);

      System.exit(0);
   }
}
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Ezzaral
0

At the most basic level, you override the equals() method and test whether the object properties meet the criteria that you consider them "equal". Here is a bit more information: http://java.sun.com/docs/books/tutorial/java/IandI/objectclass.html

In practical usage there are more considerations to overriding equals() than just comparing the objects' properties, such as also overriding hashCode(), dealing with distinctions of subclasses, etc., but first you need to understand what equals() is supposed to be doing and try to get a working basic implementation into your classes.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.