File size: 2,760 Bytes
dcba1b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
Write a method
public static void addToGroup(SuperGroup group, ArrayList<SuperAnimal> animals)

which receives a 'super group' and 'a list of super animals' as arguments.

Method tries to add the members to group one at a time.
For each animal, the method outputs if the adding was successful or not (see the example below).
After iterating through all animals, the group is output using the 'outputGroup method' in class SuperGroup.



Example of calling the method:
public static void main(String[] args) {
   //LIST OF ANIMALS
   ArrayList<SuperAnimal> al = new ArrayList<>();
   al.add(new SuperAnimal("pig", "Fifer", "Super musician"));
   al.add(new SuperAnimal("cat", "Crazy Kat", "Super strength"));
   al.add(new SuperAnimal("dog", "Super Snoops", "Super barking"));

   //GROUP
   SuperGroup group = new Supergroup(The Barkvengers");
   
   // FOR SPECIFIED GROUP, ADD LIST OF ANIMALS
   addToGroup(group, al);
}


Program outputs:
Success: Fifer (pig), super strength: Supermusisointi
Success: Crazy Kat (cat), super strength: Super strength
Fail: Super Snoops(dog), super strength: Super barking
The Barkvengers
Fifer (pig), super strength: Super musician
Crazy Kat (cat), super strength: super strength



======================================


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

public class Test{
    public static void main(String[] args){
        final Random r = new Random();
        
        ArrayList<SuperAnimal> al = new ArrayList<>();
        
        al.add(new SuperAnimal("pig", "Mr. Piggy", "Superbreath"));
        al.add(new SuperAnimal("cow", "Captain Moo", "Supermilk"));
        al.add(new SuperAnimal("cat", "Felixx", "Super vision"));
        al.add(new SuperAnimal("cat", "Car-Field", "Supercar"));

        Collections.shuffle(al, r);
        SuperGroup group = new SuperGroup("Ryhmä Äks");
        addToGroup(group, al);
    }






    //ADD
    public static void addToGroup(SuperGroup group, ArrayList<SuperAnimal> animals) {
        for (SuperAnimal animal : animals) {
            try {
                group.addMember(animal);
                System.out.println("Success: " + animal);
            } 
            catch (SuperException e) {
                System.out.println("Fail: " + animal);
            }
        }
        group.outputGroup();
    }



    
}




Success: Felixx (cat), superStrength: Super vision
Success: Car-Field (cat), superStrength: Supercar
Success: Captain Moo (cow), superStrength: Supermilk
Success: Mr. Piggy (pig), superStrength: Superbreath
Ryhmä Äks
Felixx (cat), superStrength: Super vision
Car-Field (cat), superStrength: Supercar
Captain Moo (cow), superStrength: Supermilk
Mr. Piggy (pig), superStrength: Superbreath