Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use case buy property #28

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/main/java/tutorial/BuyPropertyProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package UseCase;
import Entity.Player;
import Entity.Properties;

public class BuyPropertyProcessor {
private Player player;
private Properties properties;

public BuyPropertyProcessor(Player player, Properties properties) {
// assume controller passes an object of Player and an object of Properties
this.player = player;
this.properties = properties;
}

public String BuyProperty() {
if (this.player.getMoney() >= this.properties.getPrice()) {
if (!this.properties.OwnedByPlayer()){
this.player.loseMoney(this.properties.getPrice());
this.properties.resetOwner(this.player);
return "Purchase successful";
}
}

return "You don't have enought money";
// we also need to check whether this player's position on the board is the position of this property, so JUST
// after the player moved, controller decide whether to call this method.
// So in controller, the code looks like: if (processor.BuyProperty()) {do something such as call presentor. ...}
}
}
38 changes: 38 additions & 0 deletions src/test/java/tutorial/BuyPropertyProcessorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package tutorial;



import org.junit.Test;
import org.junit.*;

import static org.junit.Assert.*;

import java.io.*;
import java.util.Optional;

public class BuyPropertyProcessorTest {

@Test
public void testBuyProperty1() {
Properties properties = new Properties("A",100, 30, false, "red", 50);
Player player = new Player("Jimmy", 1500, 1,"", 1);
BuyPropertyProcessor processor = new BuyPropertyProcessor(player, properties);
assertTrue(processor.BuyProperty());
assertEquals(1400, player.getMoney());
}


@Test
public void testBuyProperty2() {
Properties properties = new Properties("B",100, 30, false, "red", 50);
Player player1 = new Player("Jimmy", 1500, 1,"", 1);
properties.resetOwner(player1);
Player player2 = new Player("JimmyNumber2", 1500, 1,"", 1);
BuyPropertyProcessor processor = new BuyPropertyProcessor(player2, properties);
assertFalse(processor.BuyProperty());
assertEquals(1500, player2.getMoney());
}



}