2 Replies - 1308 Views - Last Post: 17 March 2013 - 03:36 AM Rate Topic: -----

#1 DJ2334   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 08-February 13

Equals override question

Posted 16 March 2013 - 11:08 PM

I'm supposed to override the equals method in the product class so that it checks to see if two products contain the same data, but it's still adding another value to the product count even when I put in the same product twice in the output.

What my output is currently displaying:

Quote

Welcome to the Product Selector

Enter product code: java

Code: java
Description: Murach's Beginning Java
Price: $49.50
Author: Andrea Steelman

Product count: 1

Continue? (y/n): y

Enter product code: java

Code: java
Description: Murach's Beginning Java
Price: $49.50
Author: Andrea Steelman

Product count: 2

Continue? (y/n): y

Enter product code: mcb2

Code: mcb2
Description: Murach's Mainframe COBOL
Price: $59.50
Author: Mike Murach

Product count: 3

Continue? (y/n): n


What I want it to display:

Quote

Welcome to the Product Selector

Enter product code: java

Code: java
Description: Murach's Beginning Java
Price: $49.50
Author: Andrea Steelman

Product count: 1

Continue? (y/n): y

Enter product code: java

Code: java
Description: Murach's Beginning Java
Price: $49.50
Author: Andrea Steelman

Product count: 1

Continue? (y/n): y

Enter product code: mcb2

Code: mcb2
Description: Murach's Mainframe COBOL
Price: $59.50
Author: Mike Murach

Product count: 2

Continue? (y/n): n


This is the code that I'm supposed to configure, so it can tell if two products have the same data (Can be found in product class below):
    @Override
    public boolean equals(Object object)
    {
        if (object instanceof Product)
        {
            Product product2 = (Product) object;
            if
            (
                code.equals(product2.getCode()) &&
                description.equals(product2.getDescription()) &&
                price == product2.getPrice()
            )
            return true;
        }
        return false;
    }



Superclass:

package a190.pkg06;

import java.util.Scanner;

public class ProductApp
{
    public static void main(String[] args) 
    {
        // display a welcome message
        System.out.println("Welcome to the Product Selector");
        System.out.println();
        
        // perform 1 or more selections
        Scanner sc = new Scanner(System.in);
        String choice = "y";
        while (choice.equalsIgnoreCase("y"))
        {
            System.out.print("Enter product code: ");
            String productCode = sc.next();  // read the product code
            sc.nextLine();  // discard any other data entered on the line
            
            // get the Product object
            Product p = ProductDB.getProduct(productCode);
            
            // display the output
            System.out.println();
            if (p != null)
                System.out.println(p.getDisplayText());
            else
                System.out.println("No product matches this product code.\n");
            
            System.out.println("Product count: " + Product.getCount() + "\n");
            
            // see if the user wants to continue
            System.out.print("Continue? (y/n): ");
            choice = sc.nextLine();
            System.out.println();
        }
    }
}



Product class with the equals class that is overridden:

package a190.pkg06;

import java.text.NumberFormat;

public abstract class Product 
{
    private String code;
    private String description;
    private double price;
    protected static int count = 0;
    
    public Product() 
    {
        
    }
    
    public void setCode(String code)
    {
        this.code = code;
    }
    public String getCode()
    {
        return code;
    }
    
    public void setDescription(String description)
    {
        this.description = description;
    }
    public String getDescription()
    {
        return description;
    }
    
    public void setPrice(double price)
    {
        this.price = price;
    }
    public double getPrice()
    {
        return price;
    }
    public String getFormattedPrice()
    {
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        return currency.format(price);
    }
    
    // regular constructors and methods for instance variables
    
    @Override
    public String toString()
    {
        return "Code: " + code + "\n" +
               "Description: " + description + "\n" +
               "Price: " + this.getFormattedPrice() + "\n";
    }

    @Override
    public boolean equals(Object object)
    {
        if (object instanceof Product)
        {
            Product product2 = (Product) object;
            if
            (
                code.equals(product2.getCode()) &&
                description.equals(product2.getDescription()) &&
                price == product2.getPrice()
            )
            return true;
        }
        return false;
    }
    public static int getCount()
    {
        return count;
    }
    abstract String getDisplayText();  // an abstract method
}



Other classes:

package a190.pkg06;

public class ProductDB
{
  public static Product getProduct(String productCode)
  {   
      Product p = null;
      {
          Book b = new Book();

          switch (productCode)
          {         
              case "java":
                  b.setCode(productCode);
                  b.setDescription("Murach's Beginning Java");
                  b.setPrice(49.50);
                  b.setAuthor("Andrea Steelman");
                  p.count++;
                  p = b; // set Product variable equal to the Book object
                  break;
              case "jsps":
                  b.setCode(productCode);
                  b.setDescription("Murach's Java Servlets and JSP");
                  b.setPrice(49.50);
                  b.setAuthor("Andrea Steelman");
                  p.count++;
                  p = b; // set Product variable equal to the Book object
                  break;
              case "mcb2":
                  b.setCode(productCode);
                  b.setDescription("Murach's Mainframe COBOL");
                  b.setPrice(59.50);
                  b.setAuthor("Mike Murach");
                  p.count++;
                  p = b; // set Product variable equal to the Book object
                  break;
          }
      }
      return p;
  }   
}



package a190.pkg06;

public class Book extends Product 
{
    private String author;
    
    public Book()
    {
        super();
        author = "";
    }
    
    public void setAuthor(String author)
    {
        this.author = author;
    }
    public String getAuthor()
    {
        return author;
    }
    
    // regular constructor and methods for the Book class
    
    @Override
    public String getDisplayText()  // implement the abstract method
    {
        return super.toString() +
                "Author: " + author + "\n";
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Equals override question

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Equals override question

Posted 17 March 2013 - 03:16 AM

Point to where you actually use the equals() method. I expect to see it in ProductApp, but I don't.
Was This Post Helpful? 0
  • +
  • -

#3 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3687
  • View blog
  • Posts: 16,917
  • Joined: 20-September 08

Re: Equals override question

Posted 17 March 2013 - 03:36 AM

There's not much point in having a product code if it's not sufficient in itself to establish uniqueness
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1