My problem is that I have lots of If-Else statements. A LOT. It gets hard to read it, and I sometimes get output I don't want.
I have to compare 5 animals names, weights, and ages. As well as outputting and checking if they are same animal and display which are younger, older, weigh less, and weigh more. It works, sort of, but it is bad code design
Code:
if(petOne.getName().equalsIgnoreCase(petTwo.getName()) && petThree.getName().equalsIgnoreCase(petFour.getName()) && petFour.getName().equalsIgnoreCase(petFive.getName()) )
{
System.out.println("All five dogs have the same name.");
}
else if(petOne.getWeight() == petTwo.getWeight() && petThree.getWeight() == petFour.getWeight() && petFour.getName() == petFive.getName())
{
System.out.println("All five dogs have the same weight");
}
else if(petOne.getAge() == petTwo.getAge() && petThree.getAge() == petFour.getAge() && petFour.getAge() == petFive.getAge())
{
System.out.println("All five dogs are the same age.");
}
//Below check if any pets are the same
//1
if(petOne.toString().equals(petTwo.toString()))
{
System.out.println(petOne.toString()+" and "+petTwo.toString()+" are the same");
}
else if(petOne.toString().equals(petThree.toString()))
{
System.out.println(petOne.toString()+" and "+petThree.toString()+" are the same");
}
else if(petOne.toString().equals(petFour.toString()))
{
System.out.println(petOne.toString()+" and "+petFour.toString()+ "are the same");
}
else if(petOne.toString().equals(petFive.toString()))
{
System.out.println(petOne.toString()+" and "+petFive.toString()+" are the same");
}
//2
if(petTwo.toString().equals(petOne.toString()))
{
System.out.println(petTwo.toString()+" and "+petOne.toString()+" are the sae");
}
else if(petTwo.toString().equals(petThree.toString()))
{
System.out.println(petTwo.toString()+" and "+petThree.toString()+" are the same");
}
else if(petTwo.toString().equals(petFour.toString()))
{
System.out.println(petTwo.toString()+" and "+petFour.toString()+" are the same");
}
else if(petTwo.toString().equals(petFive.toString()))
{
System.out.println(petTwo.toString()+" and "+petFive.toString()+" are the same");
}
//3
if(petThree.toString().equals(petOne.toString()))
{
System.out.println(petThree.toString()+" and "+petOne.toString()+" are the same");
}
else if(petThree.toString().equals(petTwo.toString()))
{
System.out.println(petThree.toString()+" and "+petTwo.toString()+" are the same");
}
else if(petThree.toString().equals(petFour.toString()))
{
System.out.println(petThree.toString()+" and "+petFour.toString()+" are the same");
}
else if(petThree.toString().equals(petFive.toString()))
{
System.out.println(petThree.toString()+" and "+petFive.toString()+" are the same");
}
//4
if(petFour.toString().equals(petOne.toString()))
{
System.out.println(petFour.toString()+" and "+petOne.toString()+" are the same");
}
else if(petFour.toString().equals(petTwo.toString()))
{
System.out.println(petFour.toString()+" and "+petTwo.toString()+" are the same");
}
else if(petFour.toString().equals(petThree.toString()))
{
System.out.println(petFour.toString()+" and "+petThree.toString()+" are the same");
}
else if(petFour.toString().equals(petFive.toString()))
{
System.out.println(petFour.toString()+" and "+petFour.toString()+" are the same");
}
//5
if(petFive.toString().equals(petOne.toString()))
{
System.out.println(petFive.toString()+" and "+petOne.toString()+" are the same");
}
else if(petFive.toString().equals(petTwo.toString()))
{
System.out.println(petFive.toString()+" and "+petTwo.toString()+" are the same");
}
else if(petFive.toString().equals(petThree.toString()))
{
System.out.println(petFive.toString()+" and "+petThree.toString()+" are the same");
}
else if(petFive.toString().equals(petFour.toString()))
{
System.out.println(petFive.toString()+" and "+petFour.toString()+" are the same");
}
//Below caculates which pet is the youngest
if(petOne.getAge() < petTwo.getAge() && petOne.getAge() < petThree.getAge() && petOne.getAge() < petFour.getAge() && petOne.getAge() < petFive.getAge())
{
System.out.println(petOne.getName()+" is the youngest pet.");
}
else if(petTwo.getAge() < petOne.getAge() && petTwo.getAge() < petThree.getAge() && petTwo.getAge() < petFour.getAge() && petTwo.getAge() < petFive.getAge())
{
System.out.println(petTwo.getName()+" is the youngest pet.");
}
else if(petThree.getAge() < petOne.getAge() && petThree.getAge() < petTwo.getAge() && petThree.getAge() < petFour.getAge() && petThree.getAge() < petFive.getAge())
{
System.out.println(petThree.getName()+" is the youngest pet.");
}
else if(petFour.getAge() < petOne.getAge() && petFour.getAge() < petTwo.getAge() && petFour.getAge() < petThree.getAge() && petFour.getAge() < petFive.getAge())
{
System.out.println(petFour.getName()+" is the youngest pet.");
}
else if(petFive.getAge() < petOne.getAge() && petFive.getAge() < petTwo.getAge() && petFive.getAge() < petThree.getAge() && petFive.getAge() < petFour.getAge())
{
System.out.println(petFive.getName()+" is the youngest pet.");
}
//Below caculates which pet is the oldest
if(petOne.getAge() > petTwo.getAge() && petOne.getAge() > petThree.getAge() && petOne.getAge() > petFour.getAge() && petOne.getAge() > petFive.getAge())
{
System.out.println(petOne.getName()+" is the oldest pet.");
}
else if(petTwo.getAge() > petOne.getAge() && petTwo.getAge() > petThree.getAge() && petTwo.getAge() > petFour.getAge() && petTwo.getAge() > petFive.getAge())
{
System.out.println(petTwo.getName()+" is the oldest pet.");
}
else if(petThree.getAge() > petOne.getAge() && petThree.getAge() > petTwo.getAge() && petThree.getAge() > petFour.getAge() && petThree.getAge() > petFive.getAge())
{
System.out.println(petThree.getName()+" is the oldest pet.");
}
else if(petFour.getAge() > petOne.getAge() && petFour.getAge() > petTwo.getAge() && petFour.getAge() > petThree.getAge() && petFour.getAge() > petFive.getAge())
{
System.out.println(petFour.getName()+" is the oldest pet.");
}
else if(petFive.getAge() > petOne.getAge() && petFive.getAge() > petTwo.getAge() && petFive.getAge() > petThree.getAge() && petFive.getAge() > petFour.getAge())
{
System.out.println(petFive.getName()+" is the oldest pet.");
}
//Below caculates which pet is the smallest
if(petOne.getWeight() < petTwo.getWeight() && petOne.getWeight() < petThree.getWeight() && petOne.getWeight() < petFour.getWeight() && petOne.getWeight() < petFive.getWeight())
{
System.out.println(petOne.getName()+" is the smallest pet.");
}
else if(petTwo.getWeight() < petOne.getWeight() && petTwo.getWeight() < petThree.getWeight() && petTwo.getWeight() < petFour.getWeight() && petTwo.getWeight() < petFive.getWeight())
{
System.out.println(petTwo.getName()+" is the smallest pet.");
}
else if(petThree.getWeight() < petOne.getWeight() && petThree.getWeight() < petTwo.getWeight() && petThree.getWeight() < petFour.getWeight() && petThree.getAge() < petFive.getWeight())
{
System.out.println(petThree.getName()+" is the smallest pet.");
}
else if(petFour.getWeight() < petOne.getWeight() && petFour.getWeight() < petTwo.getWeight() && petFour.getWeight() < petThree.getWeight() && petFour.getWeight() < petFive.getWeight())
{
System.out.println(petFour.getName()+" is the smallest pet.");
}
else if(petFive.getWeight() < petOne.getWeight() && petFive.getWeight() < petTwo.getWeight() && petFive.getWeight() < petThree.getWeight() && petFive.getWeight() < petFour.getWeight())
{
System.out.println(petFive.getName()+" is the smallest pet.");
}
//Below caculates to see which pet is the largest
if(petOne.getWeight() > petTwo.getWeight() && petOne.getWeight() > petThree.getWeight() && petOne.getWeight() > petFour.getWeight() && petOne.getWeight() > petFive.getWeight())
{
System.out.println(petOne.getName()+" is the largest pet.");
}
else if(petTwo.getWeight() > petOne.getWeight() && petTwo.getWeight() > petThree.getWeight() && petTwo.getWeight() > petFour.getWeight() && petTwo.getWeight() > petFive.getWeight())
{
System.out.println(petTwo.getName()+" is the largest pet.");
}
else if(petThree.getWeight() > petOne.getWeight() && petThree.getWeight() > petTwo.getWeight() && petThree.getWeight() > petFour.getWeight() && petThree.getAge() > petFive.getWeight())
{
System.out.println(petThree.getName()+" is the largest pet.");
}
else if(petFour.getWeight() > petOne.getWeight() && petFour.getWeight() > petTwo.getWeight() && petFour.getWeight() > petThree.getWeight() && petFour.getWeight() > petFive.getWeight())
{
System.out.println(petFour.getName()+" is the largest pet.");
}
else if(petFive.getWeight() > petOne.getWeight() && petFive.getWeight() > petTwo.getWeight() && petFive.getWeight() > petThree.getWeight() && petFive.getWeight() > petFour.getWeight())
{
System.out.println(petFive.getName()+" is the largest pet.");
}