Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java硬件的形成方法_Java_Equals - Fatal编程技术网

java硬件的形成方法

java硬件的形成方法,java,equals,Java,Equals,我想知道我是否正确地写了这个equals方法。即使两个对象Item和otherObject不相等,我的程序也会一直打印出“equal”。我有三个equals方法,当一次运行一个时,似乎没有一个方法是正确的。三种equals方法位于彼此的后面,以供参考。我的主要任务在最后 import static java.lang.System.*; import java.text.DecimalFormat; import java.util.Arrays; import java.util.S

我想知道我是否正确地写了这个equals方法。即使两个对象
Item
otherObject
不相等,我的程序也会一直打印出“equal”。我有三个equals方法,当一次运行一个时,似乎没有一个方法是正确的。三种equals方法位于彼此的后面,以供参考。我的主要任务在最后

 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
导入静态java.lang.System.*;
导入java.text.DecimalFormat;
导入java.util.array;
导入java.util.Scanner;
导入java.math.*;
公共类项目{
DecimalFormat df=新的DecimalFormat(#.00”);
//项目的属性
静态双现金=59.00;
静态双和=0.00;
私人优先权;
私有字符串名称;
私人双价;
静态布尔值=false;
//默认构造函数
公共项目(){
优先级=-1;//用默认值填充
价格=0.00;
name=“还没有名字”;
}
公共项(int优先级、字符串名称、双倍价格){//具有所有3个参数的构造函数
优先权=优先权;
this.name=名称;
这个价格=价格;
}
public int getPriority(){
返回优先级;
}
公共无效设置优先级(整数优先级){
//优先级必须介于1和7之间
如果(优先级>0&&priority=0.00){
if(价目表[j].getPriority()){
温度=列表[j-1];
列表[j-1]=列表[j];
列表[j]=温度;
}
}//min=j;
System.out.println(列表[i]);
}
}//主要
}//类项目

如果您希望覆盖
对象#等于(对象)
,则您的方法签名需要

public boolean equals(Object [some identifer])
否则将重载该方法

一个简单的技巧是注释要用
@Override
覆盖的方法。任何像样的IDE都会告诉您该方法是否没有覆盖任何内容


否则,您似乎没有将
otherObject
name
设置为
anItem.name
,因此

this.name.equals(otherObject.name)
将返回
false

if("foo".equals("bar"));
{System.out.println("\nequal");}
这打印的
也相等

如果语句结束得太早,那么总是执行下一条语句

您需要删除
if

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

我认为你的方法应该是:

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

这很简单,因为
name
字段是私有的。

还值得测试在两个项目上调用
setName(null)
时会发生什么,然后测试是否相等;)@迈克:哦,不,不要那样做!操作,检查
null
引用。请在问题中只保留重要代码。