File size: 4,394 Bytes
e8ecd2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
129
130
131
132
133
134
135
136
137
138
139
140
Write a class SecretAgent, which has the following features:
A constructor that takes as parameters a name (string) and a code (string)
Get methods getName and getCode.
Set method setCode(String code), which sets the code attribute if the given parameter follows the rules (rules below); otherwise, the code is set to "000"
A static method static boolean codeOk(String code), which returns true if the code is according to the rules
The code 'follows the rules' if it has exactly 3 numbers and no other characters. 
The first 2 numbers must be zeros.






import java.util.Random;

public class Test{
    public static void main(String[] args){
        final Random r = new Random();
        

        System.out.println("Testing the class SecretAgent...");

        String[] en = "James Jane John Jill Jim Janet George Sarah Oliver Emma".split(" ");
        String[] sn = "Bond Pond Smith Johnson Clarke Wilson Taylor Morris".split(" ");
        String[] k = "500 505 040 00A 00X XYZ X0X 700 777 0X0X 0007 0003 0070".split(" ");
        
        for (int test = 1; test <=2; test++) {
            System.out.println("Test " + test);
            
            String name = en[r.nextInt(en.length)] + " " + sn[r.nextInt(sn.length)];
            String code = "00" + (r.nextInt(9) + 1);
            SecretAgent sa = new SecretAgent(name, code);
            
            System.out.println("Object created with parameters (" + name + ", " + code + ")");
            
            System.out.println("getName() returns " + sa.getName());
            System.out.println("getCode() returns " + sa.getCode());
            
            System.out.println("Attempting to change code to a valid one...");
            
            for (int iteration=1; iteration<= 2; iteration++) {
                code = "00" + (r.nextInt(9) + 1);
                System.out.println("Attempting with code " + code);
                sa.setCode(code);
                System.out.println("getCode() returns " + sa.getCode());
            }
            
            System.out.println("Attempting to change code to an invalid one...");
 
            code = k[r.nextInt(k.length)];
            System.out.println("Attempting with code " + code);
            sa.setCode(code);
            System.out.println("getCode() returns " + sa.getCode());
        }
        
        System.out.println("Test 3: Calling the static method");
        String[] codes = "007 004 003 008 0009 070 00A 600 888".split(" ");
        for (String code : codes) {
            System.out.println("Testing code " + code + ", valid: " + SecretAgent.codeOk(code));
        }
        
    }
}




//ADD
class SecretAgent {
    private String name;
    private String code;

    public SecretAgent(String name, String code) {
        this.name = name;
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        if (codeOk(code)) {
            this.code = code;
        } else {
            this.code = "000";
        }
    }

    // https://www.geeksforgeeks.org/java-string-matches-method-in-java-with-examples/
    // https://stackoverflow.com/questions/28145881/how-does-d-work-in-java
    public static boolean codeOk(String code) {
        return code.matches("00\\d");
    }
}




Testing the class SecretAgent...
Test 1
Object created with parameters (George Taylor, 005)
getName() returns George Taylor
getCode() returns 005
Attempting to change code to a valid one...
Attempting with code 003
getCode() returns 003
Attempting with code 003
getCode() returns 003
Attempting to change code to an invalid one...
Attempting with code 500
getCode() returns 000
Test 2
Object created with parameters (Emma Taylor, 002)
getName() returns Emma Taylor
getCode() returns 002
Attempting to change code to a valid one...
Attempting with code 002
getCode() returns 002
Attempting with code 006
getCode() returns 006
Attempting to change code to an invalid one...
Attempting with code 700
getCode() returns 000
Test 3: Calling the static method
Testing code 007, valid: true
Testing code 004, valid: true
Testing code 003, valid: true
Testing code 008, valid: true
Testing code 0009, valid: false
Testing code 070, valid: false
Testing code 00A, valid: false
Testing code 600, valid: false
Testing code 888, valid: false