-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassEncapsulation.java
60 lines (49 loc) · 1.51 KB
/
ClassEncapsulation.java
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
class Account {
boolean isActive = false; // public by default
public String login = "";
public String email = "";
public int id = 0;
protected int[] connections = new int[10];
private int connectionsNumber = 0;
public void display() {
System.out.println("ID: " + id + "\n"
+ "Login: " + login + "\n"
+ "E-mail: " + email + "\n"
+ "Is active: " + isActive + "\n"
+ "Has connections: " + hasConnections()
);
};
public boolean hasConnections() {
return (connectionsNumber > 0);
};
public boolean addConnection(int connectedAccountId) {
if (connectionsNumber == 10)
return false;
connections[connectionsNumber] = connectedAccountId;
connectionsNumber++;
return true;
}
}
class SocialMediaAccount extends Account {
public boolean isFriend(int checkingAccountId) {
for(int connectionAccountId : connections) {
if (checkingAccountId == connectionAccountId)
return true;
}
return false;
}
}
class ClassEncapsulation {
public static void main(String[] args) {
SocialMediaAccount timothy = new SocialMediaAccount();
timothy.login = "tim";
timothy.email = "timothy.muppetone@gmail.com";
timothy.isActive = true;
timothy.display();
System.out.println();
timothy.addConnection(100);
System.out.println("Has connections: " + timothy.hasConnections());
System.out.println("Is ID 100 a friend? " + timothy.isFriend(100));
System.out.println("Is ID 100 a friend? " + timothy.isFriend(200));
}
}