Advertisement
LoraOrliGeo

HeroRepository_UnitTest

Aug 1st, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. package heroRepository;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4.  
  5. public class HeroRepository {
  6.     private List<Hero> data;
  7.  
  8.     public HeroRepository() {
  9.         this.data = new ArrayList<>();
  10.     }
  11.  
  12.     public void add(Hero hero) {
  13.         if (this.data.contains(hero)){
  14.             throw new IllegalArgumentException("Duplicate heroes!");
  15.         }
  16.         this.data.add(hero);
  17.     }
  18.  
  19.     public void remove(String name) {
  20.         Hero hero = this.data.stream().filter(x -> x.getName()
  21.                 .equals(name)).findFirst().orElse(null);
  22.  
  23.         if (hero==null){
  24.             throw new NullPointerException("Hero doesn't exist");
  25.         }
  26.         this.data.remove(hero);
  27.     }
  28.  
  29.     public Hero getHeroWithHighestStrength() {
  30.         int best = 0;
  31.         Hero bestHero = null;
  32.         for (Hero hero : data) {
  33.             if (hero.getItem().getStrength() > best) {
  34.                 bestHero = hero;
  35.                 best=hero.getItem().getStrength();
  36.             }
  37.         }
  38.         if (bestHero==null){
  39.             throw new NullPointerException("Hero with highest strength not found");
  40.         }
  41.         return bestHero;
  42.     }
  43.  
  44.     public Hero getHeroWithHighestAgility() {
  45.         int best = 0;
  46.         Hero bestHero = null;
  47.         for (Hero hero : data) {
  48.             if (hero.getItem().getAgility() > best) {
  49.                 bestHero = hero;
  50.                 best=hero.getItem().getAgility();
  51.             }
  52.         }
  53.         if (bestHero==null){
  54.             throw new NullPointerException("Hero with highest agility not found");
  55.         }
  56.         return bestHero;
  57.     }
  58.  
  59.     public Hero getHeroWithHighestIntelligence() {
  60.         int best = 0;
  61.         Hero bestHero = null;
  62.         for (Hero hero : data) {
  63.             if (hero.getItem().getIntelligence() > best) {
  64.                 bestHero = hero;
  65.                 best=hero.getItem().getIntelligence();
  66.             }
  67.         }
  68.         if (bestHero==null){
  69.             throw new NullPointerException("Hero with highest intelligence not found");
  70.         }
  71.         return bestHero;
  72.     }
  73.  
  74.     public int getCount() {
  75.         return this.data.size();
  76.     }
  77.  
  78.     @Override
  79.     public String toString() {
  80.         StringBuilder sb = new StringBuilder();
  81.         this.data.forEach(x -> sb.append(x.toString()));
  82.         return sb.toString();
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement