1
5 class User {
6
7 private String name;
9 private Friend friendList;
11
14 public User(String name) {
15 this.name = name;
16 }
17
18
21 public String name() {
22 return name;
23 }
24
25
28 public Friend friends() {
29 return friendList;
30 }
31
32
35 public void showFriends() {
36 System.out.print("{ "+name()+": ");
37
38 Friend friend = friendList;
39 while (friend != null) { User u = friend.who();
41 System.out.print(u.name()+" ");
42
43 friend = friend.next();
45 }
46 System.out.println("}");
47 }
48
49
52 public boolean isFriend(User user) {
53
54 Friend friend = friendList;
55 while (friend != null) {
56
57 if (user == friend.who()) return true;
59
60 friend = friend.next();
62 }
63 return false;
64 }
65
66
69 public void addFriend(User user) {
70
71 if (!isFriend(user)) {
73
74 friendList = new Friend(user, friendList);
77
78 user.addFriend(this);
80 }
81 }
82
83
86 void showCommonFriends(User user) {
87 System.out.print("{ "+name()+" & "+user.name()+": ");
88
89 Friend friend = friendList;
90 while (friend != null) {
91
92 User f = friend.who();
93 if (f.isFriend(user))
95 System.out.print(f.name()+" ");
96
97 friend = friend.next();
99 }
100 System.out.println("}");
101 }
102
103 }
104