File size: 2,976 Bytes
095a473
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
The program has defined the Route class written in the previous exercise.

Write a method:

public static ArrayList<Route> routesInDirection(ArrayList<Route> routes, CardinalDirection direction)
which receives a list of route objects and a direction as parameters.


The method returns a new list containing those routes that go in the given direction.
The ORDER of the routes in the new list should be the SAME as in the list provided as a parameter.




import java.util.ArrayList;
import java.util.Random;

public class Test{
    public static void main(String[] args){
        final Random r = new Random();
        
   
        ArrayList<Route> al = new ArrayList<>();
        int count = r.nextInt(5) + 10;
        
        for (int i = 0; i < count; i++) {
            double length = r.nextInt(200) + 10;
            CardinalDirection direction = CardinalDirection.values()[r.nextInt(4)];
            al.add(new Route(length, direction));
        }
        
        System.out.println("All routes:");
        al.stream().forEach(rt -> System.out.println("" + rt));

        for (CardinalDirection direction : CardinalDirection.values()) {
            System.out.println("");
            System.out.println("Routes in the direction " + direction);
            
            ArrayList<Route> al2 = routesInDirection(al, direction);
            al2.stream().forEach(rt -> System.out.println("" + rt));
        }        
    }



    //add
    public static ArrayList<Route> routesInDirection(ArrayList<Route> routes, CardinalDirection direction) {
        // initialise empty ArrayList
        // approach 1 - works
        ArrayList<Route> filteredRoutes = new ArrayList<Route>();
        // approach 2 - works
        // in Java 7+
        // ArrayList<Route> filteredRoutes = new ArrayList<>();
        
        for (Route route: routes) {
            if (route.getDirection() == direction) {
                filteredRoutes.add(route);
            }
        }
        return filteredRoutes;
    }









}






All routes:
Direction: NORTH, length: 125.0
Direction: WEST, length: 160.0
Direction: NORTH, length: 114.0
Direction: NORTH, length: 196.0
Direction: NORTH, length: 167.0
Direction: NORTH, length: 169.0
Direction: WEST, length: 120.0
Direction: EAST, length: 124.0
Direction: WEST, length: 34.0
Direction: NORTH, length: 173.0
Direction: EAST, length: 107.0
Direction: WEST, length: 167.0
Direction: NORTH, length: 95.0

Routes in the direction NORTH
Direction: NORTH, length: 125.0
Direction: NORTH, length: 114.0
Direction: NORTH, length: 196.0
Direction: NORTH, length: 167.0
Direction: NORTH, length: 169.0
Direction: NORTH, length: 173.0
Direction: NORTH, length: 95.0

Routes in the direction SOUTH

Routes in the direction EAST
Direction: EAST, length: 124.0
Direction: EAST, length: 107.0

Routes in the direction WEST
Direction: WEST, length: 160.0
Direction: WEST, length: 120.0
Direction: WEST, length: 34.0
Direction: WEST, length: 167.0