I'm posting after 2 hours of trying to solve it but I'd like some help here. The task is the following:
Create a method getHeaviest which takes no parameters and returns a string. When called, the method should return the name of the heaviest dinosaur in the database. How you achieve this is up to you. If no dinosaurs are in the database, return an empty string instead. Don't worry about dinosaurs having the same weight.
import java.util.Map;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;
import java.util.Map.Entry;
public class randomdamdam {
private Map<String, Integer> dinos;
public randomdamdam () {
dinos = new HashMap<>();
}
public int size(){
return dinos.size();
}
public void addDino(String newDino, int weight){
if (!dinos.containsKey(newDino)) {
dinos.put(newDino, weight);
System.out.println(newDino + " added. Weight: " + newDino + "kg");
} else {
System.out.println(newDino + " cannot be added. It is already in the database!");
}
}
public void updateDino (String updatedDino, int newWeight){
if (dinos.containsKey(updatedDino)){
System.out.println(updatedDino + "updated. Weight: " +newWeight+ "kg");
} else {
String line = updatedDino + "cannot be updated. It is not in the database!";
System.out.println(line);
}
}
public void removeDino (String removedDino) {
if (dinos.containsKey(removedDino)){
System.out.println(removedDino + "removed");
} else {
String line2= removedDino +"cannot be removed. It is not in the database!";
System.out.println(line2);
}
}
public int getWeight (String existingDinosaur) {
if (dinos.containsKey(existingDinosaur)){
return dinos.get(existingDinosaur);
} else {
String ofweight = existingDinosaur + "cannot be found in the database!";
System.out.println(ofweight);
return 0;
}
}
public Set<String> getDinoNames(){
Set<String> names = dinos.keySet();
return names;
}
public String getHeaviest () {
int max = Collections.max(dinos.values());
for (Entry<String, Integer> heaviestBoi : dinos.entrySet()) {
if (heaviestBoi.getValue() == max ) {
String heavy = heaviestBoi.toString();
return heaviestBoi;
}}
}
So the thing is that I'd like to get the heaviest dino out of every dino and I've tried multiple times to do that but I actually can't.