Skip to content

Commit

Permalink
ensure that prices can only be increased by positive numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
taveras committed Apr 17, 2024
1 parent e9da68b commit 2e0345e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/edu/berkeley/aep/Stock.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public int currentPrice() {
}

public void increasePrice(int amount) {
if (amount <= 0) {
throw new RuntimeException("Unable to increase stock price by " + amount);
}
price += amount;
}

Expand Down
10 changes: 10 additions & 0 deletions test/edu/berkeley/aep/StockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

public class StockTest {
@Test
Expand All @@ -18,6 +19,15 @@ public void canIncreasePrice() {
assertEquals(105, company.currentPrice());
}

@Test
public void canNotIncreasePriceByNegativeNumber() {
Stock company = new Stock("ABC", 100);
assertThrows(
RuntimeException.class,
() -> company.increasePrice(-5)
);
}

@Test
public void canDecreasePrice() {
Stock company = new Stock("ABC", 100);
Expand Down

0 comments on commit 2e0345e

Please sign in to comment.