In: Computer Science
I am trying to create a method in JAVA that takes in an ArrayList and sorts it by the requested "amenities" that a property has. So if someone wants a "pool" and "gym" it would show all members of the array that contain a "pool" and "gym".
It does not need to output the values in anyway, but it should return them so they can be output elsewhere.
Please try to use the stub class below. You can edit it obviously.
The code for the main class is not necessary. I am only really asking for the formula code to filterByAmenities. I do not want anything else. The code doesnt even necessarily have to work as is, but it could just work when provided data. It just needs to filter given the criteria.
public ArrayList filterByAmenities(boolean pool, boolean gym,
boolean pets, boolean laundry, boolean shuttle) {
return null;
}
Hope you are doing well. I am try to solve the problem as per your requirment. If you give me some sample input and output data, I will Edit the Solution. I wll be try to solve the problem as per requirment, in a simple way. Hope below solution give you correct Answer.
--------------------------------------
import java.util.ArrayList;
public class FilterData {
static ArrayList<FilterData> listData=new
ArrayList<FilterData>();
boolean pool;
boolean gym;
boolean pets;
boolean laundry;
boolean shuttle;
public FilterData(boolean pool, boolean gym, boolean
pets, boolean laundry, boolean shuttle) {
this.pool = pool;
this.gym = gym;
this.pets = pets;
this.laundry = laundry;
this.shuttle = shuttle;
}
public FilterData() {
// TODO Auto-generated constructor
stub
}
public boolean isPool() {
return pool;
}
public void setPool(boolean pool) {
this.pool = pool;
}
public boolean isGym() {
return gym;
}
public void setGym(boolean gym) {
this.gym = gym;
}
public boolean isPets() {
return pets;
}
public void setPets(boolean pets) {
this.pets = pets;
}
public boolean isLaundry() {
return laundry;
}
public void setLaundry(boolean laundry) {
this.laundry = laundry;
}
public boolean isShuttle() {
return shuttle;
}
public void setShuttle(boolean shuttle) {
this.shuttle = shuttle;
}
public static void main(String[] args) {
FilterData obj=new
FilterData();
FilterData data1=new
FilterData(true, true, false, false, true);
listData.add(data1);
FilterData data2=new
FilterData(false, true, true, true, true);
listData.add(data1);
ArrayList<FilterData>
filterData=obj.filterByAmenities(true, true, false, false,
true);
System.out.println("---Filter data
is---");
for(FilterData data:filterData)
{
System.out.println("Pool: "+data.isPool());
System.out.println("Gym: "+data.isGym());
System.out.println("Pool: "+data.isPets());
System.out.println("Pool: "+data.isLaundry());
System.out.println("Pool: "+data.isShuttle());
}
}
// filtering the data on basis of pool and gym
public ArrayList<FilterData>
filterByAmenities(boolean pool, boolean gym, boolean pets, boolean
laundry, boolean shuttle) {
ArrayList<FilterData>
listData1=new ArrayList<FilterData>();
for(FilterData data: listData)
{
// filtering the
data on the basis of pool ang gym
// we only
filtering pool and gym have true values
if(data.isGym()
&& data.isPool()) {
listData1.add(data);
}
}
return listData1;
}
}
----------------------
SAMPLE OUTPUT:
---Filter data is---
Pool: true
Gym: true
Pool: false
Pool: false
Pool: true
Pool: true
Gym: true
Pool: false
Pool: false
Pool: true
-----------------------
SUMMARY:
I have provided the solution as per your requirement, i hope
you're satisfied with the way i have approached. please dont
hesitate to give me a Like if you like it, i appreciate your like.
If you have any queries you can shoot them any time in comments
section. I will be glad to help you.
Thank you :)