2013-10-11 30 views
-1

我在想如果我正確地寫這個equals方法。即使兩個對象ItemotherObject不相等,我的程序仍會保持「平等」打印。我有三個等號的方法,而且一次只運行一個方法似乎沒有任何問題。三個等同的方法正好位於彼此之後以供參考。我的主要是在最後。形成equals方法java hw

import static java.lang.System.*; 

import java.text.DecimalFormat; 
import java.util.Arrays; 
import java.util.Scanner; 
import java.math.*; 
public class Item { 
    DecimalFormat df = new DecimalFormat("#.00"); 

    //the properties of an Item 
    static double cash=59.00; 
    static double sum=0.00; 
    private int priority; 
    private String name; 
    private double price; 
    static boolean value= false; 

    //default constructer 
    public Item() { 
     priority = -1; //fill with default values 
     price = 0.00; 
     name = "No name yet"; 
    } 

    public Item(int priority, String name, double price) {//constructor with all 3  arguments 
     this.priority = priority; 
     this.name = name;   
     this.price = price; 
    } 

    public int getPriority() { 
     return priority; 
    } 

    public void setPriority(int priority) { 
     //priority must be between 1 and 7 
     if (priority > 0 && priority <= 7) { 

      this.priority = priority; 
     } else { 

      System.err.println("Error, enter 1 through 7"); 

     } 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 


     this.name = name; 
    } 

    public double getPrice() { 
     return price; 

    } 

    public void setPrice(double price) { 

     //price between 0 and 100 inclusive 
     if (price >= 0.00) { 
      if (price <= 100.00) { 
       this.price = price; 
       cash = cash-=price; 
       sum=sum+=price; 

      } else { 

       System.err.println("Error: price to high"); 
      } 
     } else { 
      System.err.println("Error: price to low"); 
     } 
    } 

     public boolean equals(Item otherObject) { 
     return this.name.equals(otherObject.name); 



    } 

     /*public boolean equals(Item otherObject) { 
      if(this.getPriority()==(otherObject.getPriority())); 

       return true; 
     } */   

    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + ((name == null) ? 0 : name.hashCode()); 
     result = prime * result + priority; 
     return result; 
    } 


    /*@Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (!(obj instanceof Item)) 
      return false; 
     Item other = (Item) obj; 
     if (name == null) { 
      if (other.name != null) 
       return false; 
     } else if (!name.equals(other.name)) 
      return false; 
     if (priority != other.priority) 
      return false; 
     return true; 
    }*/ 

    @Override 
    public String toString() { 
     StringBuilder builder = new StringBuilder(); 
     builder.append("Item [Price= ").append(getPrice()).append(", "); 
     if (getName() != null) { 
      builder.append("Name= ").append(getName()).append(", "); 
     } 
     builder.append("Priority= ").append(getPriority()).append("]"); 
     return builder.toString(); 
    } 

    public static void main (String[] args) { 

     Item[] list = new Item[2]; 
     Scanner keyboard = new Scanner(System.in); 


     for (int i = 1; i <= list.length; i++) { 

      if(cash==59) 
      { 
       System.out.println("You have 59 dollars"); 
      } 

      Item otherObject=new Item(); 
      Item anItem = new Item(); // new item object created 7 times 

      System.out.println("Enter an item you want to add to your list " + i); 
      anItem.setName(keyboard.next()); 

      System.out.println("Enter a price " + i); 
      anItem.setPrice(keyboard.nextDouble()); 

      System.out.println("Enter the priority of the item " + i); 
      anItem.setPriority(keyboard.nextInt()); 

      list[i-1] = anItem; 

      System.out.println("Cash left "+cash); 
      System.out.println("Sum of Items "+sum); 
      System.out.println(Arrays.toString(list)); 

      if (anItem.equals(otherObject)); //--------------- This is printing out each time. Is it comparing default constructors? 
      {System.out.println("\nequal");} 

    } 
     if(sum>59) 
     {System.err.println("Error, you ran out of money\t\t"); 

     } 
    // int a; 
    //int b; 
    //a=list[0].getPriority(); 
    // b=list[1].getPriority(); 
    //System.out.println(a +" here"); 
    // System.out.println(b +" here"); 

    //final int[] arraySort = { a, b,}; 
    Item temp; 

    for (int i = 0; i < list.length; i++) { 

     //min = i; 
     for (int j = 1; j < (list.length - i); j++) { 
      if (list[j-1].getPriority() > list[j].getPriority()) { 
       temp = list[j - 1]; 
       list[j - 1] = list[j]; 
       list[j] = temp; 

      } 
     } //min = j; 
     System.out.println(list[i]); 
    } 

    } //main  
    }// class Item 
+1

請僅在您的問題中保留重要的代碼。 –

回答

2

如果您正在尋找覆蓋Object#equals(Object),你的方法簽名必須

public boolean equals(Object [some identifer]) 

否則你重載方法。

簡單的訣竅是用@Override註解意圖覆蓋的方法。任何體面的IDE都會告訴你,如果這種方法沒有壓倒一切。


否則,它似乎並不像你所設定的otherObjectname到的anItem.name因此

this.name.equals(otherObject.name) 

將返回false

+0

當您在兩個項目上調用'setName(null)'時,測試會發生什麼,然後測試其是否相等;值得一試。 –

+1

@mike哦,不,不要那樣做! OP,檢查'null'引用。 –

5
if("foo".equals("bar")); 
{System.out.println("\nequal");} 

這樣打印equal呢!

您過早結束if聲明,所以下一個聲明總是執行!

您需要的if

if (anItem.equals(otherObject)) 
{System.out.println("\nequal");} 
0

後刪除;我覺得你的equals方法應該是:

public boolean equals(Item otherObject) { 
     return this.name.equals(otherObject.getName()); 
    } 

簡單,因爲name領域是私人的。