Files
OOS/src/main/java/ui/AccountviewController.java
2024-12-09 17:13:33 +01:00

99 lines
2.7 KiB
Java

package ui;
import bank.PrivateBank;
import bank.Transaction;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.*;
import javafx.stage.Stage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class AccountviewController {
@FXML
private Label accountNameLabel;
@FXML
private Label balanceLabel;
@FXML
private ListView<String> transactionListView;
private PrivateBank privateBank;
private String currentAccount;
public void setAccount(String accountName, PrivateBank bank) {
this.currentAccount = accountName;
this.privateBank = bank;
updateAccountInfo();
loadTransactions();
}
private void updateAccountInfo() {
accountNameLabel.setText("Account: " + currentAccount);
double balance = privateBank.getAccountBalance(currentAccount);
//balanceLabel.setText("Balance: " + balance);
}
private void loadTransactions() {
var transactions = privateBank.getTransactions(currentAccount);
showTransactions(transactions);
}
@FXML
private void onBackButtonClicked() {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/main.fxml"));
Parent root = loader.load();
Stage stage = (Stage) transactionListView.getScene().getWindow();
stage.getScene().setRoot(root);
} catch (IOException e) {
e.printStackTrace();
}
}
@FXML
private void onSortAscending() {
var sortedTransactions = privateBank.getTransactionsSorted(currentAccount, true);
showTransactions(sortedTransactions);
}
@FXML
private void onSortDescending() {
var sortedTransactions = privateBank.getTransactionsSorted(currentAccount, false);
showTransactions(sortedTransactions);
}
@FXML
private void onShowPositive() {
var positive = privateBank.getTransactionsByType(currentAccount, true);
showTransactions(positive);
}
@FXML
private void onShowNegative() {
var negative = privateBank.getTransactionsByType(currentAccount, false);
showTransactions(negative);
}
@FXML
private void onShowAll() {
var all = privateBank.getTransactions(currentAccount);
showTransactions(all);
}
private void showTransactions(List<Transaction> transactions) {
List<String> displayList = new ArrayList<>();
for (var t : transactions) {
displayList.add(t.toString());
}
// transactionListView.getItems().setAll(displayList);
updateAccountInfo(); // Balance neu anzeigen, falls sich was geändert hat
}
}