Skip to content

Commit

Permalink
Fix buy interactor issue 158 (#161)
Browse files Browse the repository at this point in the history
* fix 158

* switched logic to extend fix to sell

* fixed faulty logic with base stock interactor
  • Loading branch information
awesominat committed Nov 29, 2023
1 parent 70dddec commit f1f6a57
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 13 deletions.
8 changes: 0 additions & 8 deletions src/main/java/entities/CommonUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ public Boolean isInPortfolio(String ticker) {
return portfolio.containsKey(ticker);
}


public void updatePortfolio(String ticker, Double amount) {
if (amount == 0) {
portfolio.remove(ticker);
return;
}
portfolio.put(ticker, amount);
}
public Double getStockOwned(String ticker) {
return portfolio.get(ticker);
}
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/use_cases/BaseStockInteractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ protected TransactionHistory initHistory(String ticker, Double boughtAt) {
return new TransactionHistory(newStock, transactions);
}
protected void updatePortfolio(User user, String ticker, Double amount) {
if (amount == 0 && user.isInPortfolio(ticker)) {
user.removeFromPortfolio(ticker);
if (user.isInPortfolio(ticker)) {
Double currentlyOwned = user.getStockOwned(ticker);
if (currentlyOwned + amount <= 0.0) {
user.removeFromPortfolio(ticker);
} else {
user.addToPortfolio(ticker, currentlyOwned + amount);
}
} else {
user.addToPortfolio(ticker, amount);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/use_cases/Buy/BuyInteractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ public void execute(BuyInputData buyInputData) {
buyPresenter.prepareSuccessView(buyOutputData);
return;
}
HashMap<String, Double> portfolio = user.getPortfolio();

if (amount == null) {
try {
CompanyInformation companyInformation = driverAPI.getCompanyProfile(ticker);
Double currentPrice = driverAPI.getCurrentPrice(ticker).getPrice();

HashMap<String, Double> portfolio = user.getPortfolio();
Double currentlyHeld = 0.0;
if (portfolio.containsKey(ticker)) {
currentlyHeld = portfolio.get(ticker);
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/use_cases/Sell/SellInteractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public void execute(SellInputData sellInputData) {
}

Double currentPrice = driverAPI.getCurrentPrice(ticker).getPrice();
Double currentlyOwned = user.getStockOwned(ticker);

user.addBalance(currentPrice * amount);
HashMap<String, TransactionHistory> userHistory = user.getHistory();
Expand All @@ -57,7 +56,7 @@ public void execute(SellInputData sellInputData) {
super.updatePortfolio(
user,
ticker,
currentlyOwned - amount
-amount
);

super.addToHistory(
Expand Down

0 comments on commit f1f6a57

Please sign in to comment.