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

added table and current balance to sellview page #124

Merged
merged 1 commit into from
Nov 26, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ public void prepareSuccessView(DashboardOutputData dashboardOutputData) {
dashboardState.setOwnedFullNames(dashboardOutputData.getFullNamesInformation());
dashboardState.setUserStats(dashboardOutputData.getUserStats());

// below code should not be necessary, since all switches to dashboard view are followed by a viewManagerModel call
// set active view to be dashboard
viewManagerModel.setActiveView(dashboardViewModel.getViewName());
// viewManagerModel.setActiveView(dashboardViewModel.getViewName());
// fire the property changed for view manager model such that the view changes to dashboard
viewManagerModel.firePropertyChanged();
// viewManagerModel.firePropertyChanged();
}
}
2 changes: 1 addition & 1 deletion src/main/java/interface_adapters/Sell/SellController.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void execute(String amount, String ticker) {
SellInputData sellInputData = new SellInputData(Double.parseDouble(amount), ticker);
sellInteractor.execute(sellInputData);
} catch (NumberFormatException ex) {
SellInputData sellInputData = new SellInputData(ticker);
SellInputData sellInputData = new SellInputData(-1.0, ticker);
sellInteractor.execute(sellInputData);
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/interface_adapters/Sell/SellPresenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ public SellPresenter(ViewManagerModel viewManagerModel,

@Override
public void prepareSuccessView(SellOutputData response) {
SellState sellState = sellViewModel.getState();
String sellSuccess = String.format("Congratulations! Sold %.2f stocks of ", response.getAmount()) + response.getTicker();
sellState.setSellSuccess(sellSuccess);
dashboardViewModel.firePropertyChanged();
viewManagerModel.setActiveView(dashboardViewModel.getViewName());
sellViewModel.firePropertyChanged();
viewManagerModel.setActiveView(sellViewModel.getViewName());
viewManagerModel.firePropertyChanged();
}

Expand Down
12 changes: 11 additions & 1 deletion src/main/java/interface_adapters/Sell/SellState.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ public class SellState {
private String stockSelected = null;
private String amountError = null;
private String amount = null;
private String sellSuccess = null;
private List<String> ownedStocks = null;

public SellState(String stockSelected, String amount, String amountError, List<String> ownedStocks) {
public SellState(String stockSelected, String amount, String amountError, List<String> ownedStocks, String sellSuccess) {
this.amountError = amountError;
this.amount = amount;
this.stockSelected = stockSelected;
this.ownedStocks = ownedStocks;
this.sellSuccess = sellSuccess;
}

public String getAmountError() {
Expand Down Expand Up @@ -47,6 +49,14 @@ public List<String> getOwnedStocks() {
return ownedStocks;
}

public void setSellSuccess(String sellSuccess) {
this.sellSuccess = sellSuccess;
}

public String getSellSuccess() {
return sellSuccess;
}

// Because of the previous copy constructor, the default constructor must be explicit. Hence overloading.
public SellState() {

Expand Down
10 changes: 0 additions & 10 deletions src/main/java/use_cases/Sell/SellInputData.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,12 @@
public class SellInputData {
public Double amount;
public String ticker;
public boolean amountFormatError = false;

public SellInputData(Double amount, String ticker) {
this.amount = amount;
this.ticker = ticker;
}

public SellInputData(String ticker) {
this.amount = null;
this.ticker = ticker;
this.amountFormatError = true;
}

public Double getAmount() {
return amount;
}
Expand All @@ -24,7 +17,4 @@ public String getTicker() {
return ticker;
}

public boolean isAmountFormatError() {
return amountFormatError;
}
}
5 changes: 2 additions & 3 deletions src/main/java/use_cases/Sell/SellInteractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ public SellInteractor(SellDataAccessInterface userDataAccessInterface,
public void execute(SellInputData sellInputData) {
String ticker = sellInputData.getTicker();
Double amount = sellInputData.getAmount();
boolean amountFormatError = sellInputData.isAmountFormatError();

User user = userDataAccessObject.get();

if (amountFormatError) {
sellPresenter.prepareFailView("Please enter a decimal value");
if (amount <= 0.0) {
sellPresenter.prepareFailView("Please enter a decimal value greater than 0");
return;
}
if (!user.hasStock(ticker) || user.getStockOwned(ticker) < amount) {
Expand Down
27 changes: 26 additions & 1 deletion src/main/java/view/SellView.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package view;

import drivers.TableModel;
import interface_adapters.Dashboard.DashboardState;
import interface_adapters.Dashboard.DashboardViewModel;
import interface_adapters.ViewManagerModel;
Expand All @@ -16,6 +17,7 @@
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import java.util.HashMap;
import java.util.List;

public class SellView extends JPanel implements ActionListener, PropertyChangeListener {
Expand All @@ -31,6 +33,7 @@ public class SellView extends JPanel implements ActionListener, PropertyChangeLi

final JButton sell;
final JButton back;
final JTable table;

public SellView(SellViewModel sellViewModel, SellController sellController, ViewManagerModel viewManagerModel, DashboardViewModel dashboardViewModel) {
this.sellController = sellController;
Expand All @@ -39,19 +42,23 @@ public SellView(SellViewModel sellViewModel, SellController sellController, View
this.sellViewModel = sellViewModel;
this.sellViewModel.addPropertyChangeListener(this);

JLabel title = new JLabel("Sell Screen");
JLabel title = new JLabel("Sell Stocks");
title.setAlignmentX(Component.CENTER_ALIGNMENT);

JLabel stockSelectionLabel = new JLabel("Select an owned stock");
LabelTextPanel stockAmountInfo = new LabelTextPanel(
new JLabel("Select amount to sell"), amountInputField);

stockInputField.setMaximumSize(new Dimension(100,100));
stockInputField.setAlignmentX(Component.CENTER_ALIGNMENT);

JPanel buttons = new JPanel();
back = new JButton("Back");
buttons.add(back);
sell = new JButton("Sell Stocks");
buttons.add(sell);
table = new JTable();
table.setPreferredSize(new Dimension(30, 200));

sell.addActionListener( // This creates an anonymous subclass of ActionListener and instantiates it.
new ActionListener() {
Expand Down Expand Up @@ -109,6 +116,7 @@ public void actionPerformed(ActionEvent e) {


this.add(title);
this.add(table);
this.add(stockSelectionLabel);
this.add(stockInputField);
this.add(stockAmountInfo);
Expand All @@ -126,6 +134,13 @@ public void actionPerformed(ActionEvent evt) {
public void propertyChange(PropertyChangeEvent evt) {
// Display any potential errors
SellState state = (SellState) evt.getNewValue();

String sellSuccess = state.getSellSuccess();
if (sellSuccess != null) {
JOptionPane.showMessageDialog(this, sellSuccess);
}
state.setSellSuccess(null);

String amountError = state.getAmountError();
if (amountError != null) {
JOptionPane.showMessageDialog(this, amountError);
Expand All @@ -142,6 +157,16 @@ public void propertyChange(PropertyChangeEvent evt) {
stockInputField.addItem(s);
}
}

// Show table of stocks and amounts owned
HashMap<String, String> ownedStocksAmounts = new HashMap<String, String>();
List<Double> ownedAmounts = dashboardState.getOwnedAmounts();
if (ownedStocks != null && ownedAmounts != null && !ownedAmounts.isEmpty() && !ownedStocks.isEmpty()) {
for (int i = 0; i<ownedStocks.size(); i++) {
ownedStocksAmounts.put(ownedStocks.get(i), String.valueOf(ownedAmounts.get(i)));
}
}
table.setModel(new TableModel(ownedStocksAmounts));
}

}
Loading