Second requirement is not being met..
Requirements:
• The IPhone class must override the public boolean equals(Object) method.
• The equals method must return true for two equal IPhone objects and false for different ones.
• The equals method must return false if null is passed to it.
it also says "Two different IPhones must not be equal."
The code:
package en.codegym.task.pro.task10.task1010;
import java.util.Objects;
/*
Two iPhones
*/
public class Iphone {
private String model;
private String color;
private int price;
public Iphone(String model, String color, int price) {
this.model = model;
this.color = color;
this.price = price;
}
public boolean equals(Object o){
if(this == o) return true;
if(o==null) return false;
if(!( o instanceof Iphone)) return false;
Iphone iphone = (Iphone) o;
if((iphone.model!=null) &&(this.model).equals(iphone.model))
{
if((iphone.color!=null)&&(this.color).equals(iphone.color)){
if((this.price)==(iphone.price))
return true;
}
}
return false;
}
//write your code here
public static void main(String[] args) {
Iphone iphone1 = new Iphone("X", "Black", 999);
Iphone iphone2 = new Iphone("X", "Black", 999);
System.out.println(iphone1.equals(iphone2));
}
}