|
| 1 | +package com.codedifferently.lesson16.boxer; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.Random; |
| 6 | + |
| 7 | +public class Boxer { |
| 8 | + public enum BoxerStyle { |
| 9 | + SWITCHHITTER, |
| 10 | + BOXERPUNCHER, |
| 11 | + SOUTHPAW, |
| 12 | + INFIGHTER, |
| 13 | + SLUGGER, |
| 14 | + OUTFIGHTER, |
| 15 | + ORTHODOX; |
| 16 | + } |
| 17 | + |
| 18 | + public class BoxerIsRetiredException extends Exception { |
| 19 | + public BoxerIsRetiredException(String message) { |
| 20 | + super(message); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + public class BoxerHasNoFightsException extends Exception { |
| 25 | + public BoxerHasNoFightsException(String message) { |
| 26 | + super(message); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + private String name; |
| 31 | + private HashMap<String, Character> fights; |
| 32 | + private int power; |
| 33 | + private int health; |
| 34 | + private BoxerStyle skillSet; |
| 35 | + private boolean ableToFight; |
| 36 | + private BoxerStyle[] style = BoxerStyle.values(); |
| 37 | + private static Random rand = new Random(); |
| 38 | + |
| 39 | + public Boxer(String name, int power, int health) { |
| 40 | + this.name = name; |
| 41 | + this.power = power; |
| 42 | + this.health = health; |
| 43 | + this.ableToFight = true; |
| 44 | + this.skillSet = BoxerStyle.ORTHODOX; |
| 45 | + fights = new HashMap<>(); |
| 46 | + } |
| 47 | + |
| 48 | + public String getName() { |
| 49 | + return name; |
| 50 | + } |
| 51 | + |
| 52 | + public boolean getAbleToFight() { |
| 53 | + return ableToFight; |
| 54 | + } |
| 55 | + |
| 56 | + public void workout(int additional) { |
| 57 | + power += additional; |
| 58 | + } |
| 59 | + |
| 60 | + public int getPower() { |
| 61 | + return power; |
| 62 | + } |
| 63 | + |
| 64 | + public HashMap<String, Character> getFightHistory() { |
| 65 | + return fights; |
| 66 | + } |
| 67 | + |
| 68 | + public int getHealth() { |
| 69 | + return health; |
| 70 | + } |
| 71 | + |
| 72 | + public void rest() { |
| 73 | + health += 1; |
| 74 | + } |
| 75 | + |
| 76 | + public void rollSkillSet() { |
| 77 | + int randomIndex = rand.nextInt(style.length); |
| 78 | + BoxerStyle newStyle = style[randomIndex]; |
| 79 | + skillSet = newStyle; |
| 80 | + System.out.println("Random style: " + skillSet); |
| 81 | + } |
| 82 | + |
| 83 | + public void addFights(String name, char d) throws BoxerIsRetiredException { |
| 84 | + if (ableToFight == false) { |
| 85 | + throw new BoxerIsRetiredException("Boxer is retired"); |
| 86 | + } |
| 87 | + fights.put(name, d); |
| 88 | + } |
| 89 | + |
| 90 | + public void retire() throws BoxerIsRetiredException { |
| 91 | + checkRetirementStatus(); |
| 92 | + ableToFight = false; |
| 93 | + } |
| 94 | + |
| 95 | + private void checkRetirementStatus() throws BoxerIsRetiredException { |
| 96 | + if (ableToFight == false) { |
| 97 | + throw new BoxerIsRetiredException("Boxer is already retired"); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private void checkBoxerHasntFoughtAnyone() throws BoxerHasNoFightsException { |
| 102 | + if (fights.isEmpty()) { |
| 103 | + throw new BoxerHasNoFightsException("Boxer hasnt fought anyone"); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public String getFights() throws BoxerHasNoFightsException { |
| 108 | + checkBoxerHasntFoughtAnyone(); |
| 109 | + String fightsHad = ""; |
| 110 | + for (Map.Entry<String, Character> entry : fights.entrySet()) { |
| 111 | + String key = entry.getKey(); |
| 112 | + char value = entry.getValue(); |
| 113 | + fightsHad += "You fought " + key + " " + value; |
| 114 | + } |
| 115 | + return fightsHad; |
| 116 | + } |
| 117 | + |
| 118 | + public void bout(Boxer boxer) throws BoxerIsRetiredException { |
| 119 | + if (ableToFight == false) { |
| 120 | + throw new BoxerIsRetiredException("Boxer is retired"); |
| 121 | + } |
| 122 | + String boxerName = boxer.getName(); |
| 123 | + if (power < boxer.getPower()) { |
| 124 | + ableToFight = false; |
| 125 | + fights.put(boxerName, 'L'); |
| 126 | + } else if (power == boxer.getPower()) { |
| 127 | + fights.put(boxerName, 'D'); |
| 128 | + } else { |
| 129 | + fights.put(boxerName, 'W'); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + public void setName(String name) { |
| 134 | + this.name = name; |
| 135 | + } |
| 136 | + |
| 137 | + public void setFights(HashMap<String, Character> fights) { |
| 138 | + this.fights = fights; |
| 139 | + } |
| 140 | + |
| 141 | + public void setPower(int power) { |
| 142 | + this.power = power; |
| 143 | + } |
| 144 | + |
| 145 | + public void setHealth(int health) { |
| 146 | + this.health = health; |
| 147 | + } |
| 148 | + |
| 149 | + public BoxerStyle getSkillSet() { |
| 150 | + return skillSet; |
| 151 | + } |
| 152 | + |
| 153 | + public void setSkillSet(BoxerStyle skillSet) { |
| 154 | + this.skillSet = skillSet; |
| 155 | + } |
| 156 | + |
| 157 | + public void setAbleToFight(boolean ableToFight) { |
| 158 | + this.ableToFight = ableToFight; |
| 159 | + } |
| 160 | + |
| 161 | + public BoxerStyle[] getStyle() { |
| 162 | + return style; |
| 163 | + } |
| 164 | + |
| 165 | + public void setStyle(BoxerStyle[] style) { |
| 166 | + this.style = style; |
| 167 | + } |
| 168 | + |
| 169 | + public static Random getRand() { |
| 170 | + return rand; |
| 171 | + } |
| 172 | + |
| 173 | + public static void setRand(Random rand) { |
| 174 | + Boxer.rand = rand; |
| 175 | + } |
| 176 | +} |
0 commit comments