-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathFrendliest.java
More file actions
50 lines (46 loc) · 1.46 KB
/
Frendliest.java
File metadata and controls
50 lines (46 loc) · 1.46 KB
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
// Importing map and hashmap
import java.util.HashMap;
import java.util.Map;
//Importing console program
import acm.program.ConsoleProgram;
public class Frendliest extends ConsoleProgram {
// This program prints the person who has the highest number of friends
public void run() {
setFont("-20");
Map<String, Integer> friendlist = new HashMap<>();
readPairs(friendlist);
printMostPopularPerson(friendlist);
}
// This method searches the most popular person in map and prints his/her name in console
private void printMostPopularPerson(Map<String, Integer> friendlist) {
String person = "";
int max = 0;
for(String key : friendlist.keySet()) {
if(friendlist.get(key) > max) {
max = friendlist.get(key);
person = key;
}
}
println(person + " has the highest number of friends!");
}
// This method reads string pairs from console and stores the number of friends for each person
// in map. Each pair contains two people's names.
private void readPairs(Map<String, Integer> friendlist) {
while(true) {
String friend = readLine("Enter the first person's name or [enter] to end: ");
if(friend.equals("")) {
break;
}
String person = readLine("Enter the second person's name or [enter] to end: ");
if(person.equals("")) {
break;
}
if(friendlist.containsKey(person)) {
friendlist.put(person, friendlist.get(person) + 1);
} else {
friendlist.put(person, 1);
}
println(friend + " is a friend of " + person);
}
}
}