My Arraylist contains 9 persons. Each person has a name value of type String, an age value of type int and a value of type Enum (TV, SMARTPHONE, CAR). I must create implement a method that will find the name according to the product name.
If arrayListName contains SMARTPHONE, return all names with a smartphone.
public void showByProduct(Product SMARTPHONE) {
public void showByProductName(Product SMARTPHONE) {
if (arrayListName.contains(SMARTPHONE))
System.out.println("found"+nameofowner);
else {
System.out.println("not found");
}
}
You would need to iterate over your
List, and for each Person, check if it’sproductfield has the value of the one that you passed in the method.Additionally, you would need to have one more
Listlocal to your method, in which you would add thePersonwhich matches theProduct Name, and then at the end return thatList.And the place where you call this method, store the return value in a
Listreference: –Note that, you can compare two
enumvalues using==. As anenumis asingleton.P.S: – Don’t have your parameter name as
SMARTPHONE. It’s an enum value. And also as per Java Naming Convention, variables name should start with lowercase alphabets, and followcamelCasingthereon.