Skip to content

Commit 5e626fe

Browse files
committed
capture game
1 parent 55b0b69 commit 5e626fe

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package ood.design;
2+
3+
import java.util.*;
4+
5+
public class CaptureGame {
6+
7+
private Map<String,Integer> scoreMap ;
8+
private Map<String,Integer> pointmap;
9+
Map<Integer,List<String>> topScores;
10+
11+
12+
public CaptureGame(){
13+
pointmap = new HashMap<>();
14+
pointmap.put("banana",3);
15+
pointmap.put("apple",5);
16+
pointmap.put("berry",7);
17+
pointmap.put("tomato",1);
18+
19+
scoreMap = new HashMap<>();
20+
topScores = new TreeMap<>(Comparator.reverseOrder());
21+
}
22+
23+
public void capture(String player, String fruit){
24+
int oldScore = scoreMap.getOrDefault(player,0);
25+
int newScore = oldScore + pointmap.get(fruit);
26+
27+
if(topScores.containsKey(oldScore)){
28+
topScores.get(oldScore).remove(player);
29+
if(topScores.get(oldScore).isEmpty())
30+
topScores.remove(oldScore);
31+
}
32+
topScores.putIfAbsent(newScore,new ArrayList<>());
33+
topScores.get(newScore).add(player);
34+
35+
scoreMap.putIfAbsent(player,0);
36+
scoreMap.put(player, newScore);
37+
}
38+
39+
public List<String> getTopPlayer(){
40+
int highest = topScores.keySet().stream().findFirst().get();
41+
return topScores.get(highest);
42+
}
43+
44+
public static void main(String[] args){
45+
CaptureGame game = new CaptureGame();
46+
game.capture("ravi","banana");
47+
game.capture("ravi","apple");
48+
game.capture("jivan","tomato");
49+
50+
System.out.println(
51+
game.getTopPlayer()
52+
);
53+
}
54+
}

0 commit comments

Comments
 (0)