Clement Colmerauer commited on 2024-12-13 11:07:35
Showing 10 changed files, with 41 additions and 0 deletions.
| ... | ... |
@@ -111,6 +111,28 @@ public class Player {
|
| 111 | 111 |
} |
| 112 | 112 |
} |
| 113 | 113 |
|
| 114 |
+ public void buy(Item i) |
|
| 115 |
+ {
|
|
| 116 |
+ if(this.money.compareTo(i.getValue()) == 1) |
|
| 117 |
+ {
|
|
| 118 |
+ throw new UnsupportedOperationException("Player doesn't have enough money.");
|
|
| 119 |
+ } |
|
| 120 |
+ |
|
| 121 |
+ this.removeMoney(i.getValue().toInt()); |
|
| 122 |
+ this.inventory.add(i); |
|
| 123 |
+ } |
|
| 124 |
+ |
|
| 125 |
+ public void sell(Item i) |
|
| 126 |
+ {
|
|
| 127 |
+ if(!this.inventory.contains(i)) |
|
| 128 |
+ {
|
|
| 129 |
+ throw new UnsupportedOperationException("Player can't sell what they don't own.");
|
|
| 130 |
+ } |
|
| 131 |
+ |
|
| 132 |
+ this.inventory.remove(i); |
|
| 133 |
+ this.addMoney(i.getValue().toInt()); |
|
| 134 |
+ } |
|
| 135 |
+ |
|
| 114 | 136 |
public void hurt(int damage) |
| 115 | 137 |
{
|
| 116 | 138 |
if(damage < 0 ) |
| ... | ... |
@@ -213,6 +213,25 @@ public class UnitTests {
|
| 213 | 213 |
assertEquals(i.toString(),"1kg de plume :\nPese plus lourd que le plomb de part le poids du destin de ces pauvres oiseaux\nWeight : 1\nValue : 2"); |
| 214 | 214 |
} |
| 215 | 215 |
|
| 216 |
+ @Test |
|
| 217 |
+ @DisplayName("Item and player")
|
|
| 218 |
+ void testBuySell() |
|
| 219 |
+ {
|
|
| 220 |
+ Item i = new Item("1kg de plume","Pese plus lourd que le plomb de part le poids du destin de ces pauvres oiseaux", Natural.valueOf(1),Natural.valueOf(2));
|
|
| 221 |
+ Player p = new Player("Florian", "Grognak le barbare", Jobs.ADVENTURER, 100, new ArrayList<>(),10);
|
|
| 222 |
+ p.buy(i); |
|
| 223 |
+ assertEquals(p.getInventory().size(),1); |
|
| 224 |
+ assertEquals(p.getMoney(),Natural.valueOf(98)); |
|
| 225 |
+ p.sell(i); |
|
| 226 |
+ assertEquals(p.getInventory().size(),0); |
|
| 227 |
+ assertEquals(p.getMoney(),Natural.valueOf(100)); |
|
| 228 |
+ assertThrows(UnsupportedOperationExceptionn.class, () -> p.sell(i)); |
|
| 229 |
+ p = new Player("Florian", "Grognak le barbare", Jobs.ADVENTURER, 0, new ArrayList<>(),10);
|
|
| 230 |
+ assertThrows(UnsupportedOperationExceptionn.class, () -> p.buy(i)); |
|
| 231 |
+ assertThrows(UnsupportedOperationExceptionn.class, () -> p.sell(i)); |
|
| 232 |
+ |
|
| 233 |
+ } |
|
| 234 |
+ |
|
| 216 | 235 |
@Test |
| 217 | 236 |
@DisplayName("Natural Tests")
|
| 218 | 237 |
void testNatural() |
| 219 | 238 |