forked from hacker85/JavaLessons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterLesson.java
More file actions
128 lines (108 loc) · 2.92 KB
/
Copy pathFilterLesson.java
File metadata and controls
128 lines (108 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package patterns.structural;
import java.util.ArrayList;
import java.util.List;
public class FilterLesson {
public static void main(String[] args) {
CarF carF = new CarF(150, "green", 2);
CarF carF2 = new CarF(200, "red", 2);
CarF carF3 = new CarF(250, "black", 4);
List<CarF> cars = new ArrayList<>();
cars.add(carF);
cars.add(carF2);
cars.add(carF3);
// cars = new SpeedFilter().filter(cars);
// cars = new DorsFilter().filter(cars);
OrFilter andFilter = new OrFilter(new SpeedFilter(), new DorsFilter());
cars = andFilter.filter(cars);
for (CarF car : cars) {
System.out.println(car.getMaxSpeed());
}
}
}
interface CarFilter {
List<CarF> filter(List<CarF> cars);
}
class SpeedFilter implements CarFilter {
@Override
public List<CarF> filter(List<CarF> cars) {
List<CarF> list = new ArrayList<>();
for(CarF car : cars) {
if(car.getMaxSpeed() > 180) {
list.add(car);
}
}
return list;
}
}
class DorsFilter implements CarFilter {
@Override
public List<CarF> filter(List<CarF> cars) {
List<CarF> list = new ArrayList<>();
for(CarF car : cars) {
if(car.getDors() > 2) {
list.add(car);
}
}
return list;
}
}
class AndFilter implements CarFilter {
CarFilter filterOne;
CarFilter filterTwo;
public AndFilter(CarFilter filterOne, CarFilter filterTwo) {
this.filterOne = filterOne;
this.filterTwo = filterTwo;
}
@Override
public List<CarF> filter(List<CarF> cars) {
List<CarF> list = filterOne.filter(cars);
return filterTwo.filter(list);
}
}
class OrFilter implements CarFilter {
CarFilter filterOne;
CarFilter filterTwo;
public OrFilter(CarFilter filterOne, CarFilter filterTwo) {
this.filterOne = filterOne;
this.filterTwo = filterTwo;
}
@Override
public List<CarF> filter(List<CarF> cars) {
List<CarF> list = filterOne.filter(cars);
List<CarF> list2 = filterTwo.filter(cars);
for (CarF car : list2) {
if(!list.contains(car)){
list.add(car);
}
}
return list;
}
}
class CarF {
public CarF(int maxSpeed, String color, int dors) {
this.maxSpeed = maxSpeed;
this.color = color;
this.dors = dors;
}
private int maxSpeed;
private String color;
private int dors;
public int getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getDors() {
return dors;
}
public void setDors(int dors) {
this.dors = dors;
}
}