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

Transaction history view #117

Merged
merged 5 commits 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
113 changes: 87 additions & 26 deletions src/main/java/app/testTransactionHistoryView.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,88 @@
import use_cases.GetTransactionHistory.GetTransactionHistoryInteractor;
import use_cases.GetTransactionHistory.GetTransactionHistoryOutputBoundary;
import use_cases.Sell.SellInputBoundary;
//import use_cases.Sell.SellInteractor;
//import use_cases.Sell.SellOutputBoundary;
//import view.BuyView;
//import view.SellView;
//import view.TransactionHistoryView;
//import view.ViewManager;
//
//import javax.swing.*;
//import java.awt.*;
//import java.io.IOException;
//
//public class testTransactionHistoryView {
// public static void main(String[] args) throws IOException {
// // Build the main program window, the main panel containing the
// // various cards, and the layout, and stitch them together.
//
// // The main application window.
// JFrame application = new JFrame("Sell View");
// application.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//
import use_cases.Sell.SellInteractor;
import use_cases.Sell.SellOutputBoundary;
import view.BuyView;
import view.SellView;
import view.TransactionHistoryView;
import view.ViewManager;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;

public class testTransactionHistoryView {
public static void main(String[] args) throws IOException {
// Build the main program window, the main panel containing the
// various cards, and the layout, and stitch them together.

// The main application window.
JFrame application = new JFrame("View Transaction History");
application.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


CardLayout cardLayout = new CardLayout();

JPanel views = new JPanel(cardLayout);
application.add(views);

ViewManagerModel viewManagerModel = new ViewManagerModel();
new ViewManager(views, cardLayout, viewManagerModel);
BuyViewModel buyViewModel = new BuyViewModel();
DashboardViewModel dashboardViewModel = new DashboardViewModel();

FileUserDataAccessObject userDataAccessObject = new FileUserDataAccessObject(
"./user.json",
new CommonUserFactory()
);
userDataAccessObject.save();

APIAccessInterface driverAPI = new Finnhub();

BuyOutputBoundary buyPresenter = new BuyPresenter(viewManagerModel, buyViewModel);

BuyInteractor buyInteractor = new BuyInteractor(userDataAccessObject,
buyPresenter,
driverAPI);

BuyController buyController = new BuyController(buyInteractor);
GetTransactionHistoryViewModel getTransactionHistoryViewModel = new GetTransactionHistoryViewModel();

GetTransactionHistoryPresenter getTransactionHistoryPresenter = new GetTransactionHistoryPresenter(
viewManagerModel,
getTransactionHistoryViewModel,
dashboardViewModel
);

GetTransactionHistoryInteractor getTransactionHistoryInteractor = new GetTransactionHistoryInteractor(
userDataAccessObject,
getTransactionHistoryPresenter
);

GetTransactionHistoryController getTransactionHistoryController = new GetTransactionHistoryController(
getTransactionHistoryInteractor
);

// GetTransactionHistoryViewModel getTransactionHistoryViewModel,
// GetTransactionHistoryController getTransactionHistoryController,
// ViewManagerModel viewManagerModel, DashboardViewModel dashboardViewModel

TransactionHistoryView buyView = new TransactionHistoryView(
getTransactionHistoryController,
getTransactionHistoryViewModel,
viewManagerModel,
dashboardViewModel
);

views.add(buyView, buyView.viewName);

viewManagerModel.setActiveView(buyView.viewName);
viewManagerModel.firePropertyChanged();

application.pack();
application.setVisible(true);

// CardLayout cardLayout = new CardLayout();
//
// // The various View objects. Only one view is visible at a time.
Expand All @@ -62,7 +124,7 @@
// // This information will be changed by a presenter object that is reporting the
// // results from the use case. The ViewModels are observable, and will
// // be observed by the Views.
// GetTransactionHistoryViewModel sellViewModel = new GetTransactionHistoryViewModel();
// GetTransactionHistoryViewModel transactionHistoryViewModel = new GetTransactionHistoryViewModel();
//
// FileUserDataAccessObject userDataAccessObject = new FileUserDataAccessObject("./user.json", new CommonUserFactory());
// User newUser = new CommonUser();
Expand All @@ -80,7 +142,6 @@
// BuyInteractor buyInteractor = new BuyInteractor(userDataAccessObject, buyPresenter, driverAPI);
// BuyController buyController = new BuyController(buyInteractor);
//
// BuyView buyView = new BuyView(buyController, buyViewModel);
// buyInteractor.execute(buyInputData);
// buyInteractor.execute(buyInputData2);
//
Expand All @@ -94,7 +155,7 @@
//// SellController sellController = new SellController(sellInteractor);
//
// GetTransactionHistoryOutputBoundary getTransactionHistoryPresenter = new GetTransactionHistoryPresenter(viewManagerModel,
// sellViewModel, dashboardViewModel);
// transactionHistoryViewModel, dashboardViewModel);
// GetTransactionHistoryInputBoundary getTransactionHistoryInteractor = new GetTransactionHistoryInteractor(
// userDataAccessObject, getTransactionHistoryPresenter);
// GetTransactionHistoryController getTransactionHistoryController = new GetTransactionHistoryController(getTransactionHistoryInteractor);
Expand All @@ -103,13 +164,13 @@
//// SellView sellView = new SellView(sellViewModel, sellController, viewManagerModel, dashboardViewModel);
//// views.add(sellView, sellView.viewName);
//
// TransactionHistoryView historyView = new TransactionHistoryView(sellViewModel, getTransactionHistoryController, viewManagerModel, dashboardViewModel);
// TransactionHistoryView historyView = new TransactionHistoryView(transactionHistoryViewModel, getTransactionHistoryController, viewManagerModel, dashboardViewModel);
// views.add(historyView ,historyView.viewName);
// viewManagerModel.setActiveView(historyView.viewName);
// viewManagerModel.firePropertyChanged();
//
// application.pack();
// application.setVisible(true);
// }
//}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import java.util.List;

public class GetTransactionHistoryState {
private HashMap<String, List<List<Object>>> userRecord;
private HashMap<String, List<List<String>>> userRecord;

public HashMap<String, List<List<Object>>> getUserRecord() {
public HashMap<String, List<List<String>>> getUserRecord() {
return userRecord;
}

public void setUserRecord(HashMap<String, List<List<Object>>> userRecord) {
public void setUserRecord(HashMap<String, List<List<String>>> userRecord) {
this.userRecord = userRecord;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class GetTransactionHistoryViewModel extends ViewModel {

public final String TRANSACTIONS = "Transaction Record";

public final String BACK_BUTTON_LABEL = "Back";

private GetTransactionHistoryState state = new GetTransactionHistoryState();

public GetTransactionHistoryViewModel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@ public void execute() {
}


HashMap<String, List<List<Object>>> userRecord = new HashMap<>();
HashMap<String, List<List<String>>> userRecord = new HashMap<>();
for (String stock: stocks) {
// List of Transactions of a Particular Stock (List of Transaction)
List<List<Object>> listOfTransactionFacts = new ArrayList<>();
List<List<String>> listOfTransactionFacts = new ArrayList<>();

for (List<Transaction> transactions : listOfTransactions) {

for (Transaction transaction : transactions) {

String type = transaction.getType().toString();
Double amount = transaction.getAmount();
Double pricePurchasedAt = transaction.getPricePoint().getPrice();
LocalDate date = transaction.getPricePoint().getTimeStamp();
String amount = transaction.getAmount().toString();
String pricePurchasedAt = transaction.getPricePoint().getPrice().toString();
String date = transaction.getPricePoint().getTimeStamp().toString();

// List containing Transaction Facts
List<Object> Transaction = new ArrayList<>();
List<String> Transaction = new ArrayList<>();

Transaction.add(type);
Transaction.add(amount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
import java.util.List;

public class GetTransactionHistoryOutputData {
private HashMap<String, List<List<Object>>> userRecord;
private HashMap<String, List<List<String>>> userRecord;

public GetTransactionHistoryOutputData(HashMap<String, List<List<Object>>> userRecord) {
public GetTransactionHistoryOutputData(HashMap<String, List<List<String>>> userRecord) {
this.userRecord = userRecord;
}

public void setUserRecord(HashMap<String, List<List<Object>>> userRecord) {
public void setUserRecord(HashMap<String, List<List<String>>> userRecord) {
this.userRecord = userRecord;
}

public HashMap<String, List<List<Object>>> getUserRecord() {
public HashMap<String, List<List<String>>> getUserRecord() {
return userRecord;
}
}
124 changes: 73 additions & 51 deletions src/main/java/view/TransactionHistoryView.java
Original file line number Diff line number Diff line change
@@ -1,79 +1,101 @@
package view;
import interface_adapters.Dashboard.DashboardState;
import interface_adapters.GetTransactionHistory.GetTransactionHistoryState;
import interface_adapters.GetTransactionHistory.GetTransactionHistoryViewModel;
import interface_adapters.GetTransactionHistory.GetTransactionHistoryController;

import drivers.TableModel;
import interface_adapters.Buy.BuyController;
import interface_adapters.Buy.BuyState;
import interface_adapters.Buy.BuyViewModel;
import interface_adapters.Dashboard.DashboardViewModel;
import interface_adapters.Sell.SellController;
import interface_adapters.Sell.SellViewModel;
import interface_adapters.GetTransactionHistory.GetTransactionHistoryController;
import interface_adapters.GetTransactionHistory.GetTransactionHistoryViewModel;
import interface_adapters.ViewManagerModel;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

public class TransactionHistoryView extends JPanel implements ActionListener, PropertyChangeListener {

public class TransactionHistoryView extends JFrame implements ActionListener, PropertyChangeListener {
public final String viewName = "transactionHistory";
private final JTable table;
private final GetTransactionHistoryViewModel getTransactionHistoryViewModel;
private final DashboardViewModel dashboardViewModel;
private final ViewManagerModel viewManagerModel;

private final GetTransactionHistoryController getTransactionHistoryController;

public TransactionHistoryView(GetTransactionHistoryViewModel getTransactionHistoryViewModel,
GetTransactionHistoryController getTransactionHistoryController,
ViewManagerModel viewManagerModel, DashboardViewModel dashboardViewModel) {
JTable table;
final JButton back;
GetTransactionHistoryController getTransactionHistoryController;
public TransactionHistoryView(GetTransactionHistoryController getTransactionHistoryController,
GetTransactionHistoryViewModel getTransactionHistoryViewModel,
ViewManagerModel viewManagerModel,
DashboardViewModel dashboardViewModel
) {
this.getTransactionHistoryController = getTransactionHistoryController;
this.viewManagerModel = viewManagerModel;
this.getTransactionHistoryViewModel = getTransactionHistoryViewModel;
this.dashboardViewModel = dashboardViewModel;

// Set up the JFrame
JLabel title = new JLabel("Transaction History");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);

// Create a table model
DefaultTableModel model = new DefaultTableModel();
model.addColumn("Stock");
model.addColumn("Transaction Type");
model.addColumn("Amount");
model.addColumn("Price");
model.addColumn("Date");

GetTransactionHistoryState state = getTransactionHistoryViewModel.getState();
for (Map.Entry<String, List<List<Object>>> entry : state.getUserRecord().entrySet()) {
String stock = entry.getKey();
List<List<Object>> dataList = entry.getValue();

for (List<Object> rowData : dataList) {
model.addRow(new Object[]{stock, rowData.get(0), rowData.get(1), rowData.get(2), rowData.get(3)});
setPreferredSize(new Dimension(800, 400));

this.getTransactionHistoryController.execute();
back = new JButton(getTransactionHistoryViewModel.BACK_BUTTON_LABEL);

back.addActionListener(
evt -> {
dashboardViewModel.firePropertyChanged();
viewManagerModel.setActiveView(dashboardViewModel.getViewName());
viewManagerModel.firePropertyChanged();
}
);
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
setLayout(new BorderLayout());

DefaultTableModel tableModel = new DefaultTableModel();

// Add columns to the model
tableModel.addColumn("Stock"); // The key string
tableModel.addColumn("Transaction Type");
tableModel.addColumn("Amount");
tableModel.addColumn("Price");
tableModel.addColumn("Date");

// Populate the model with data
HashMap<String, List<List<String>>> userRecord = getTransactionHistoryViewModel.getState().getUserRecord();
System.out.println(userRecord);
for (HashMap.Entry<String, List<List<String>>> entry : userRecord.entrySet()) {
String key = entry.getKey();
List<List<String>> dataList = entry.getValue();

for (List<String> rowData : dataList) {
tableModel.addRow(new Object[]{key, rowData.get(0), rowData.get(1), rowData.get(2), rowData.get(3)});
}
}

this.table = new JTable(model);
// for (HashMap.Entry<String, List<List<String>>> entry : userRecord.entrySet()) {
// String key = entry.getKey();
// List<List<String>> dataList = entry.getValue();
// for (List<String> stringList : dataList) {
// Object[] rowData = new Object[5];
// rowData[0] = key;
// for (int i = 0; i < stringList.size(); i++) {
// rowData[i + 1] = stringList.get(i);
// }
// tableModel.addRow(rowData);
// }
// }

// Create the table using the model
table = new JTable(tableModel);

// Add the table to a scroll pane
JScrollPane scrollPane = new JScrollPane(table);

// Add the scroll pane to the JFrame
getContentPane().add(scrollPane, BorderLayout.CENTER);

// Display the JFrame
setLocationRelativeTo(null);
setVisible(true);
// Set layout and add components
setLayout(new BorderLayout());
add(back, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
}


@Override
// @Override
public void actionPerformed(ActionEvent e) {

}
Expand Down
Loading