Begfix Sortierung und Filterung in PrivateBank.java
This commit is contained in:
@@ -163,7 +163,7 @@ public class PrivateBank implements Bank, JsonSerializer<Transaction>, JsonDeser
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void createAccount(String account, List<Transaction> transactions) throws AccountAlreadyExistsException, TransactionAlreadyExistException, TransactionAttributeException, IOException {
|
public void createAccount(String account, List<Transaction> transactions) throws AccountAlreadyExistsException, TransactionAlreadyExistException, TransactionAttributeException {
|
||||||
createAccount(account);
|
createAccount(account);
|
||||||
for (Transaction transaction : transactions) {
|
for (Transaction transaction : transactions) {
|
||||||
if (this.accountsToTransactions.get(account).contains(transaction))
|
if (this.accountsToTransactions.get(account).contains(transaction))
|
||||||
@@ -221,7 +221,7 @@ public class PrivateBank implements Bank, JsonSerializer<Transaction>, JsonDeser
|
|||||||
|
|
||||||
// 2 setting as incoming transfer
|
// 2 setting as incoming transfer
|
||||||
else if (transaction instanceof IncomingTransfer) {
|
else if (transaction instanceof IncomingTransfer) {
|
||||||
if (((IncomingTransfer) transaction).getSender().equals("") || ((IncomingTransfer) transaction).getRecipient().equals(""))
|
if (((IncomingTransfer) transaction).getSender().isEmpty() || ((IncomingTransfer) transaction).getRecipient().equals(""))
|
||||||
throw new TransactionAttributeException("Transfer without sender or recipient");
|
throw new TransactionAttributeException("Transfer without sender or recipient");
|
||||||
|
|
||||||
else if (((IncomingTransfer) transaction).getRecipient().equals(account))
|
else if (((IncomingTransfer) transaction).getRecipient().equals(account))
|
||||||
@@ -232,7 +232,7 @@ public class PrivateBank implements Bank, JsonSerializer<Transaction>, JsonDeser
|
|||||||
|
|
||||||
// 3 setting as outgoing transfer
|
// 3 setting as outgoing transfer
|
||||||
else if (transaction instanceof OutgoingTransfer) {
|
else if (transaction instanceof OutgoingTransfer) {
|
||||||
if (((OutgoingTransfer) transaction).getSender().equals("") || ((OutgoingTransfer) transaction).getRecipient().equals(""))
|
if (((OutgoingTransfer) transaction).getSender().isEmpty() || ((OutgoingTransfer) transaction).getRecipient().isEmpty())
|
||||||
throw new TransactionAttributeException("Transfer without sender or recipient");
|
throw new TransactionAttributeException("Transfer without sender or recipient");
|
||||||
|
|
||||||
else if (((OutgoingTransfer) transaction).getSender().equals(account))
|
else if (((OutgoingTransfer) transaction).getSender().equals(account))
|
||||||
@@ -332,8 +332,16 @@ public class PrivateBank implements Bank, JsonSerializer<Transaction>, JsonDeser
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<Transaction> getTransactionsSorted(String account, boolean asc) {
|
public List<Transaction> getTransactionsSorted(String account, boolean asc) {
|
||||||
List<Transaction> transactions = this.accountsToTransactions.get(account);
|
List<Transaction> transactions = new ArrayList<>(this.accountsToTransactions.get(account));
|
||||||
transactions.sort(Comparator.comparingDouble(Transaction::calculate));
|
|
||||||
|
if (transactions == null || transactions.isEmpty())
|
||||||
|
return new ArrayList<>();
|
||||||
|
|
||||||
|
if (asc)
|
||||||
|
transactions.sort(Comparator.comparingDouble(Transaction::calculate));
|
||||||
|
else
|
||||||
|
transactions.sort(Comparator.comparingDouble(Transaction::calculate).reversed());
|
||||||
|
|
||||||
return transactions;
|
return transactions;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -346,7 +354,8 @@ public class PrivateBank implements Bank, JsonSerializer<Transaction>, JsonDeser
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<Transaction> getTransactionsByType(String account, boolean positive) {
|
public List<Transaction> getTransactionsByType(String account, boolean positive) {
|
||||||
List<Transaction> transactions = this.accountsToTransactions.get(account);
|
// an der Kopie der Liste Arbeiten oder nur Filtern und nicht entfernen
|
||||||
|
List<Transaction> transactions = new ArrayList<>(this.accountsToTransactions.get(account));
|
||||||
if (positive)
|
if (positive)
|
||||||
transactions.removeIf(transaction -> transaction.calculate() >= 0);
|
transactions.removeIf(transaction -> transaction.calculate() >= 0);
|
||||||
else
|
else
|
||||||
@@ -400,7 +409,7 @@ public class PrivateBank implements Bank, JsonSerializer<Transaction>, JsonDeser
|
|||||||
*
|
*
|
||||||
* @throws IOException if an error occurs while deserializing
|
* @throws IOException if an error occurs while deserializing
|
||||||
*/
|
*/
|
||||||
public void readAccounts() throws IOException, java.io.IOException, AccountAlreadyExistsException, TransactionAlreadyExistException, AccountDoesNotExistException, TransactionAttributeException {
|
public void readAccounts() throws java.io.IOException, AccountAlreadyExistsException, TransactionAlreadyExistException, TransactionAttributeException {
|
||||||
/*
|
/*
|
||||||
1. JSONs einlesen
|
1. JSONs einlesen
|
||||||
2. for each Transaction
|
2. for each Transaction
|
||||||
|
|||||||
@@ -2,15 +2,21 @@ package ui;
|
|||||||
|
|
||||||
import bank.PrivateBank;
|
import bank.PrivateBank;
|
||||||
import bank.Transaction;
|
import bank.Transaction;
|
||||||
|
import bank.exceptions.AccountDoesNotExistException;
|
||||||
|
import javafx.event.ActionEvent;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.fxml.FXMLLoader;
|
import javafx.fxml.FXMLLoader;
|
||||||
|
import javafx.scene.Node;
|
||||||
import javafx.scene.Parent;
|
import javafx.scene.Parent;
|
||||||
import javafx.scene.control.*;
|
import javafx.scene.control.*;
|
||||||
|
import javafx.scene.layout.GridPane;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.time.LocalDate;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public class AccountviewController {
|
public class AccountviewController {
|
||||||
|
|
||||||
@@ -21,27 +27,80 @@ public class AccountviewController {
|
|||||||
private Label balanceLabel;
|
private Label balanceLabel;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private ListView<String> transactionListView;
|
private ListView<Transaction> transactionListView;
|
||||||
|
|
||||||
private PrivateBank privateBank;
|
private PrivateBank privateBank;
|
||||||
private String currentAccount;
|
private String currentAccount;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a second copied instance of a Bank
|
||||||
|
*
|
||||||
|
* @param accountName
|
||||||
|
* @param bank
|
||||||
|
*/
|
||||||
public void setAccount(String accountName, PrivateBank bank) {
|
public void setAccount(String accountName, PrivateBank bank) {
|
||||||
this.currentAccount = accountName;
|
this.currentAccount = accountName;
|
||||||
this.privateBank = bank;
|
this.privateBank = bank;
|
||||||
updateAccountInfo();
|
updateAccountInfo();
|
||||||
loadTransactions();
|
loadTransactions();
|
||||||
|
|
||||||
|
// Kontextmenü erstellen
|
||||||
|
ContextMenu contextMenu = new ContextMenu();
|
||||||
|
|
||||||
|
// Kontextmenü-Items definieren
|
||||||
|
MenuItem deleteItem = new MenuItem("Löschen");
|
||||||
|
|
||||||
|
// Kontextmenü-Item Aktion: Transaktion löschen
|
||||||
|
deleteItem.setOnAction(
|
||||||
|
event -> {
|
||||||
|
Transaction selectedTransaction = transactionListView.getSelectionModel().getSelectedItem();
|
||||||
|
if (selectedTransaction != null) {
|
||||||
|
boolean confirmed = showDeleteConfirmation(selectedTransaction);
|
||||||
|
if (confirmed) {
|
||||||
|
Alert errorAlert = new Alert(Alert.AlertType.ERROR);
|
||||||
|
errorAlert.setTitle("Fehler");
|
||||||
|
try {
|
||||||
|
privateBank.removeTransaction(currentAccount, selectedTransaction);
|
||||||
|
transactionListView.getItems().remove(selectedTransaction);
|
||||||
|
updateAccountInfo();
|
||||||
|
} catch (AccountDoesNotExistException e) {
|
||||||
|
errorAlert.setHeaderText("Account existiert nicht");
|
||||||
|
errorAlert.setContentText("Der Account \"" + currentAccount + "\" existiert nicht.");
|
||||||
|
errorAlert.showAndWait();
|
||||||
|
} catch (bank.exceptions.IOException e) {
|
||||||
|
errorAlert.setHeaderText("Account JSON Fehler");
|
||||||
|
errorAlert.setContentText("Die JSON des Accounts \"" + currentAccount + "\" konnte nicht bearbeitet werden.");
|
||||||
|
errorAlert.showAndWait();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
errorAlert.setHeaderText("Transaktion konnte nicht gelöscht werden.");
|
||||||
|
errorAlert.setContentText("Es ist ein unerwarteter Fehler aufgetreten.");
|
||||||
|
errorAlert.showAndWait();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Kontextmenü-Items dem Kontextmenü hinzufügen
|
||||||
|
contextMenu.getItems().add(deleteItem);
|
||||||
|
|
||||||
|
// An ListView binden
|
||||||
|
transactionListView.setContextMenu(contextMenu);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the labels of an account
|
||||||
|
*/
|
||||||
private void updateAccountInfo() {
|
private void updateAccountInfo() {
|
||||||
|
double balance = this.privateBank.getAccountBalance(currentAccount);
|
||||||
|
|
||||||
accountNameLabel.setText("Account: " + currentAccount);
|
accountNameLabel.setText("Account: " + currentAccount);
|
||||||
double balance = privateBank.getAccountBalance(currentAccount);
|
|
||||||
balanceLabel.setText("Balance: " + balance);
|
balanceLabel.setText("Balance: " + balance);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadTransactions() {
|
private void loadTransactions() {
|
||||||
var transactions = privateBank.getTransactions(currentAccount);
|
showTransactions(this.privateBank.getTransactions(currentAccount));
|
||||||
showTransactions(transactions);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
@@ -59,40 +118,192 @@ public class AccountviewController {
|
|||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private void onSortAscending() {
|
private void onSortAscending() {
|
||||||
var sortedTransactions = privateBank.getTransactionsSorted(currentAccount, true);
|
showTransactions(this.privateBank.getTransactionsSorted(currentAccount, true));
|
||||||
showTransactions(sortedTransactions);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private void onSortDescending() {
|
private void onSortDescending() {
|
||||||
var sortedTransactions = privateBank.getTransactionsSorted(currentAccount, false);
|
showTransactions(this.privateBank.getTransactionsSorted(currentAccount, false));
|
||||||
showTransactions(sortedTransactions);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private void onShowPositive() {
|
private void onShowPositive() {
|
||||||
var positive = privateBank.getTransactionsByType(currentAccount, true);
|
showTransactions(this.privateBank.getTransactionsByType(currentAccount, false));
|
||||||
showTransactions(positive);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private void onShowNegative() {
|
private void onShowNegative() {
|
||||||
var negative = privateBank.getTransactionsByType(currentAccount, false);
|
showTransactions(this.privateBank.getTransactionsByType(currentAccount, true));
|
||||||
showTransactions(negative);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private void onShowAll() {
|
private void onShowAll() {
|
||||||
var all = privateBank.getTransactions(currentAccount);
|
System.out.println(this.privateBank.getTransactions(currentAccount));
|
||||||
showTransactions(all);
|
showTransactions(this.privateBank.getTransactions(currentAccount));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the Listview of transaction based on a given list
|
||||||
|
*
|
||||||
|
* @param transactions
|
||||||
|
*/
|
||||||
private void showTransactions(List<Transaction> transactions) {
|
private void showTransactions(List<Transaction> transactions) {
|
||||||
List<String> displayList = new ArrayList<>();
|
List<String> displayList = new ArrayList<>();
|
||||||
for (var t : transactions) {
|
for (Transaction t : transactions)
|
||||||
displayList.add(t.toString());
|
displayList.add(t.toString());
|
||||||
}
|
|
||||||
transactionListView.getItems().setAll(displayList);
|
transactionListView.getItems().setAll(transactions);
|
||||||
updateAccountInfo(); // Balance neu anzeigen, falls sich was geändert hat
|
updateAccountInfo(); // Balance neu anzeigen, falls sich was geändert hat
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean showDeleteConfirmation(Transaction transaction) {
|
||||||
|
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
|
||||||
|
alert.setTitle("Transaktion löschen");
|
||||||
|
alert.setHeaderText("Soll die Transaktion wirklich gelöscht werden?");
|
||||||
|
alert.setContentText("Transaktion:\n" + transaction.toString() + "\n\nDiese Aktion kann nicht rückgängig gemacht werden.");
|
||||||
|
|
||||||
|
ButtonType yesButton = new ButtonType("Ja", ButtonBar.ButtonData.YES);
|
||||||
|
ButtonType noButton = new ButtonType("Nein", ButtonBar.ButtonData.NO);
|
||||||
|
|
||||||
|
alert.getButtonTypes().setAll(yesButton, noButton);
|
||||||
|
|
||||||
|
var result = alert.showAndWait();
|
||||||
|
return result.isPresent() && result.get() == yesButton;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean validateTransactionInput(ComboBox<String> typeComboBox, TextField amountField, TextField descriptionField, TextField senderReceiverField) {
|
||||||
|
String type = typeComboBox.getValue();
|
||||||
|
String amountStr = amountField.getText().trim();
|
||||||
|
String desc = descriptionField.getText().trim();
|
||||||
|
|
||||||
|
// Basis-Checks
|
||||||
|
if (desc.isEmpty()) return false;
|
||||||
|
double amount;
|
||||||
|
try {
|
||||||
|
amount = Double.parseDouble(amountStr);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type.equals("Transfer")) {
|
||||||
|
String partner = senderReceiverField.getText().trim();
|
||||||
|
if (partner.isEmpty()) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Optional<Transaction> showNewTransactionDialog() {
|
||||||
|
Dialog<Transaction> dialog = new Dialog<>();
|
||||||
|
dialog.setTitle("Neue Transaktion");
|
||||||
|
dialog.setHeaderText("Bitte geben Sie die Daten der neuen Transaktion ein.");
|
||||||
|
|
||||||
|
// Buttons
|
||||||
|
ButtonType okButton = new ButtonType("OK", ButtonBar.ButtonData.OK_DONE);
|
||||||
|
ButtonType cancelButton = new ButtonType("Abbrechen", ButtonBar.ButtonData.CANCEL_CLOSE);
|
||||||
|
dialog.getDialogPane().getButtonTypes().addAll(okButton, cancelButton);
|
||||||
|
|
||||||
|
// UI-Elemente
|
||||||
|
Label typeLabel = new Label("Typ:");
|
||||||
|
ComboBox<String> typeComboBox = new ComboBox<>();
|
||||||
|
typeComboBox.getItems().addAll("Payment", "Transfer");
|
||||||
|
typeComboBox.getSelectionModel().selectFirst(); // Default Payment
|
||||||
|
|
||||||
|
Label amountLabel = new Label("Betrag:");
|
||||||
|
TextField amountField = new TextField();
|
||||||
|
|
||||||
|
Label descriptionLabel = new Label("Beschreibung:");
|
||||||
|
TextField descriptionField = new TextField();
|
||||||
|
|
||||||
|
Label senderReceiverLabel = new Label("Partner-Konto (nur bei Transfer):");
|
||||||
|
TextField senderReceiverField = new TextField();
|
||||||
|
|
||||||
|
// Layout
|
||||||
|
GridPane grid = new GridPane();
|
||||||
|
grid.setHgap(10);
|
||||||
|
grid.setVgap(10);
|
||||||
|
|
||||||
|
grid.add(typeLabel, 0, 0);
|
||||||
|
grid.add(typeComboBox, 1, 0);
|
||||||
|
|
||||||
|
grid.add(amountLabel, 0, 1);
|
||||||
|
grid.add(amountField, 1, 1);
|
||||||
|
|
||||||
|
grid.add(descriptionLabel, 0, 2);
|
||||||
|
grid.add(descriptionField, 1, 2);
|
||||||
|
|
||||||
|
grid.add(senderReceiverLabel, 0, 3);
|
||||||
|
grid.add(senderReceiverField, 1, 3);
|
||||||
|
|
||||||
|
dialog.getDialogPane().setContent(grid);
|
||||||
|
|
||||||
|
// OK-Button Validation
|
||||||
|
Node okBtnNode = dialog.getDialogPane().lookupButton(okButton);
|
||||||
|
okBtnNode.addEventFilter(ActionEvent.ACTION, event -> {
|
||||||
|
// Validierung
|
||||||
|
if (!validateTransactionInput(typeComboBox, amountField, descriptionField, senderReceiverField)) {
|
||||||
|
event.consume(); // Verhindere schließen des Dialogs
|
||||||
|
Alert alert = new Alert(Alert.AlertType.WARNING);
|
||||||
|
alert.setTitle("Ungültige Eingabe");
|
||||||
|
alert.setHeaderText("Bitte alle Felder korrekt ausfüllen.");
|
||||||
|
alert.setContentText("Betrag muss eine Zahl sein, Beschreibung darf nicht leer sein.\n" +
|
||||||
|
"Für Transfer muss ein gültiges Partner-Konto angegeben werden.");
|
||||||
|
alert.showAndWait();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
dialog.setResultConverter(dialogButton -> {
|
||||||
|
if (dialogButton == okButton) {
|
||||||
|
try {
|
||||||
|
String type = typeComboBox.getValue();
|
||||||
|
double amount = Double.parseDouble(amountField.getText());
|
||||||
|
String description = descriptionField.getText().trim();
|
||||||
|
String date = LocalDate.now().toString();
|
||||||
|
|
||||||
|
if (type.equals("Payment")) {
|
||||||
|
return new bank.Payment(date, amount, description);
|
||||||
|
} else {
|
||||||
|
String partnerAccount = senderReceiverField.getText().trim();
|
||||||
|
if (partnerAccount.isEmpty()) return null;
|
||||||
|
|
||||||
|
if (!partnerAccount.equals(currentAccount)) {
|
||||||
|
// OutgoingTransfer: currentAccount -> partnerAccount
|
||||||
|
return new bank.OutgoingTransfer(date, amount, description, currentAccount, partnerAccount);
|
||||||
|
} else {
|
||||||
|
// IncomingTransfer: partnerAccount -> currentAccount
|
||||||
|
return new bank.IncomingTransfer(date, amount, description, partnerAccount, currentAccount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
// Sollte nie hier landen, da oben schon abgefangen, aber falls doch:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
return dialog.showAndWait();
|
||||||
|
}
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private void onNewTransactionClicked() {
|
||||||
|
Optional<Transaction> transactionOpt = showNewTransactionDialog();
|
||||||
|
if (transactionOpt.isPresent()) {
|
||||||
|
Transaction transaction = transactionOpt.get();
|
||||||
|
try {
|
||||||
|
privateBank.addTransaction(currentAccount, transaction);
|
||||||
|
loadTransactions(); // Liste neu laden, um neue Transaktion anzuzeigen
|
||||||
|
updateAccountInfo(); // Kontostand aktualisieren
|
||||||
|
} catch (Exception e) {
|
||||||
|
// Fehler beim Hinzufügen anzeigen
|
||||||
|
Alert errorAlert = new Alert(Alert.AlertType.ERROR);
|
||||||
|
errorAlert.setTitle("Fehler");
|
||||||
|
errorAlert.setHeaderText("Transaktion konnte nicht hinzugefügt werden.");
|
||||||
|
errorAlert.setContentText("Überprüfen Sie Ihre Eingaben oder versuchen Sie es später erneut.");
|
||||||
|
errorAlert.showAndWait();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,12 +2,15 @@ package ui;
|
|||||||
|
|
||||||
import bank.PrivateBank;
|
import bank.PrivateBank;
|
||||||
|
|
||||||
|
import bank.exceptions.AccountAlreadyExistsException;
|
||||||
|
import bank.exceptions.IOException;
|
||||||
import javafx.fxml.*;
|
import javafx.fxml.*;
|
||||||
import javafx.scene.Parent;
|
import javafx.scene.Parent;
|
||||||
import javafx.scene.control.*;
|
import javafx.scene.control.*;
|
||||||
import javafx.scene.input.MouseButton;
|
import javafx.scene.input.MouseButton;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
|
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@@ -19,61 +22,53 @@ public class MainviewController {
|
|||||||
|
|
||||||
private PrivateBank privateBank; // Referenz auf Ihr Bankobjekt
|
private PrivateBank privateBank; // Referenz auf Ihr Bankobjekt
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
// Bank initialisieren oder übergeben lassen
|
// Bank initialisieren
|
||||||
this.privateBank = new PrivateBank(
|
this.privateBank = main.getPrivateBank();
|
||||||
"Bank1",
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
"accountdata"
|
|
||||||
);
|
|
||||||
|
|
||||||
List<String> accounts = List.of(
|
|
||||||
"Konto1", "Konto2", "Konto3"
|
|
||||||
);
|
|
||||||
|
|
||||||
for (String account : accounts) {
|
|
||||||
try {
|
|
||||||
privateBank.createAccount(account);
|
|
||||||
privateBank.writeAccount(account);
|
|
||||||
} catch (Exception e) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Zeige alle vorhandenen Konten an
|
||||||
accountListView.getItems().addAll(privateBank.getAllAccounts());
|
accountListView.getItems().addAll(privateBank.getAllAccounts());
|
||||||
|
|
||||||
// Kontextmenü definieren
|
// Kontextmenü definieren
|
||||||
ContextMenu contextMenu = new ContextMenu();
|
ContextMenu contextMenu = new ContextMenu();
|
||||||
|
|
||||||
|
// Kontextmenü-Items definieren
|
||||||
MenuItem selectItem = new MenuItem("Auswählen");
|
MenuItem selectItem = new MenuItem("Auswählen");
|
||||||
MenuItem deleteItem = new MenuItem("Löschen");
|
MenuItem deleteItem = new MenuItem("Löschen");
|
||||||
|
|
||||||
// Aktionen definieren
|
// Kontextmenü-Item Aktion: Konto auswählen
|
||||||
selectItem.setOnAction(event -> {
|
selectItem.setOnAction(
|
||||||
String selectedAccount = accountListView.getSelectionModel().getSelectedItem();
|
event -> {
|
||||||
if (selectedAccount != null) {
|
String selectedAccount = accountListView.getSelectionModel().getSelectedItem();
|
||||||
switchToAccountView(selectedAccount);
|
if (selectedAccount != null)
|
||||||
}
|
switchToAccountView(selectedAccount);
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Kontextmenü-Item Aktion: Konto löschen
|
||||||
deleteItem.setOnAction(
|
deleteItem.setOnAction(
|
||||||
event -> {
|
event -> {
|
||||||
String selectedAccount = accountListView.getSelectionModel().getSelectedItem();
|
String selectedAccount = accountListView.getSelectionModel().getSelectedItem();
|
||||||
if (selectedAccount != null) {
|
if (selectedAccount != null) {
|
||||||
boolean confirmed = showDeleteConfirmation(selectedAccount);
|
boolean confirmed = showDeleteConfirmation(selectedAccount);
|
||||||
if (confirmed) {
|
if (confirmed) {
|
||||||
// Account aus der Bank entfernen
|
// Account löschen
|
||||||
try {
|
try {
|
||||||
privateBank.deleteAccount(selectedAccount);
|
privateBank.deleteAccount(selectedAccount);
|
||||||
// ListView aktualisieren
|
// ListView aktualisieren
|
||||||
accountListView.getItems().remove(selectedAccount);
|
accountListView.getItems().remove(selectedAccount);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
// Fehlerbehandlung falls nötig
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Kontextmenü-Items dem Kontextmenü hinzufügen
|
||||||
contextMenu.getItems().addAll(selectItem, deleteItem);
|
contextMenu.getItems().addAll(selectItem, deleteItem);
|
||||||
|
|
||||||
// Kontextmenü an die ListView binden
|
// Kontextmenü an die ListView binden
|
||||||
@@ -92,6 +87,9 @@ public class MainviewController {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param accountName
|
||||||
|
*/
|
||||||
private void switchToAccountView(String accountName) {
|
private void switchToAccountView(String accountName) {
|
||||||
try {
|
try {
|
||||||
// Laden der zweiten FXML
|
// Laden der zweiten FXML
|
||||||
@@ -102,7 +100,7 @@ public class MainviewController {
|
|||||||
AccountviewController accountController = loader.getController();
|
AccountviewController accountController = loader.getController();
|
||||||
accountController.setAccount(accountName, privateBank);
|
accountController.setAccount(accountName, privateBank);
|
||||||
|
|
||||||
// Scene in der selben Stage wechseln
|
// Scene in derselben Stage wechseln
|
||||||
Stage stage = (Stage) accountListView.getScene().getWindow();
|
Stage stage = (Stage) accountListView.getScene().getWindow();
|
||||||
stage.getScene().setRoot(root);
|
stage.getScene().setRoot(root);
|
||||||
|
|
||||||
@@ -111,6 +109,10 @@ public class MainviewController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param accountName
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
private boolean showDeleteConfirmation(String accountName) {
|
private boolean showDeleteConfirmation(String accountName) {
|
||||||
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
|
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
|
||||||
alert.setTitle("Account löschen");
|
alert.setTitle("Account löschen");
|
||||||
@@ -126,4 +128,54 @@ public class MainviewController {
|
|||||||
|
|
||||||
return result.isPresent() && result.get() == yesButton;
|
return result.isPresent() && result.get() == yesButton;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private void onNewAccountClicked() {
|
||||||
|
// Dialog zum Eingeben eines neuen Account-Namens
|
||||||
|
TextInputDialog dialog = new TextInputDialog();
|
||||||
|
dialog.setTitle("Neuen Account erstellen");
|
||||||
|
dialog.setHeaderText("Bitte geben Sie den Namen des neuen Accounts ein:");
|
||||||
|
dialog.setContentText("Account-Name:");
|
||||||
|
|
||||||
|
Optional<String> result = dialog.showAndWait();
|
||||||
|
if (result.isPresent()) {
|
||||||
|
String newAccountName = result.get().trim();
|
||||||
|
if (!newAccountName.isEmpty()) {
|
||||||
|
Alert errorAlert = new Alert(Alert.AlertType.ERROR);
|
||||||
|
errorAlert.setTitle("Fehler");
|
||||||
|
try {
|
||||||
|
// Account erstellen
|
||||||
|
privateBank.createAccount(newAccountName);
|
||||||
|
privateBank.writeAccount(newAccountName);
|
||||||
|
|
||||||
|
// neuen Account der Listview hinzufügen
|
||||||
|
if (!accountListView.getItems().contains(newAccountName))
|
||||||
|
accountListView.getItems().add(newAccountName);
|
||||||
|
|
||||||
|
} catch (AccountAlreadyExistsException e) {
|
||||||
|
errorAlert.setHeaderText("Account existiert bereits");
|
||||||
|
errorAlert.setContentText("Der Account \"" + newAccountName + "\" wurde bereits angelegt.");
|
||||||
|
errorAlert.showAndWait();
|
||||||
|
} catch (IOException e) {
|
||||||
|
errorAlert.setHeaderText("Account JSON Fehler");
|
||||||
|
errorAlert.setContentText("Die JSON des Accounts \"" + newAccountName + "\" konnte nicht bearbeitet werden.");
|
||||||
|
errorAlert.showAndWait();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
errorAlert.setHeaderText("Account konnte nicht erstellt werden.");
|
||||||
|
errorAlert.setContentText("Überprüfen Sie, ob der Name bereits existiert oder ein anderer Fehler vorliegt.");
|
||||||
|
errorAlert.showAndWait();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Leeren Namen nicht akzeptieren
|
||||||
|
Alert alert = new Alert(Alert.AlertType.WARNING);
|
||||||
|
alert.setTitle("Warnung");
|
||||||
|
alert.setHeaderText("Ungültiger Account-Name");
|
||||||
|
alert.setContentText("Bitte geben Sie einen gültigen Namen ein.");
|
||||||
|
alert.showAndWait();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,17 @@
|
|||||||
package ui;
|
package ui;
|
||||||
|
|
||||||
|
import bank.PrivateBank;
|
||||||
import javafx.application.Application;
|
import javafx.application.Application;
|
||||||
import javafx.fxml.FXMLLoader;
|
import javafx.fxml.FXMLLoader;
|
||||||
import javafx.scene.Scene;
|
import javafx.scene.Scene;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
|
|
||||||
public class main extends Application {
|
public class main extends Application {
|
||||||
|
/**
|
||||||
|
* Starting point
|
||||||
|
* @param primaryStage
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void start(Stage primaryStage) throws Exception {
|
public void start(Stage primaryStage) throws Exception {
|
||||||
FXMLLoader loader = new FXMLLoader(getClass().getResource("/Mainview.fxml"));
|
FXMLLoader loader = new FXMLLoader(getClass().getResource("/Mainview.fxml"));
|
||||||
@@ -17,4 +23,11 @@ public class main extends Application {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
launch(args);
|
launch(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static PrivateBank privateBank = new PrivateBank("Bank1", 0, 0, "accountdata");
|
||||||
|
|
||||||
|
public static PrivateBank getPrivateBank() {
|
||||||
|
return privateBank;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,21 +11,24 @@
|
|||||||
<!-- Back Button -->
|
<!-- Back Button -->
|
||||||
<Button text="Back" fx:id="backButton" onAction="#onBackButtonClicked"/>
|
<Button text="Back" fx:id="backButton" onAction="#onBackButtonClicked"/>
|
||||||
|
|
||||||
|
<Button text="Neue Transaktion" onAction="#onNewTransactionClicked"/>
|
||||||
|
|
||||||
<!-- Account Name Label -->
|
<!-- Account Name Label -->
|
||||||
<Label fx:id="accountNameLabel" text="Account: "/>
|
<Label fx:id="accountNameLabel" text="Account: "/>
|
||||||
|
|
||||||
<!-- Kontostand Label -->
|
<!-- Balance Label -->
|
||||||
<Label fx:id="balanceLabel" text="Balance: "/>
|
<Label fx:id="balanceLabel" text="Balance: "/>
|
||||||
</HBox>
|
</HBox>
|
||||||
</top>
|
</top>
|
||||||
|
|
||||||
<center>
|
<center>
|
||||||
|
<!-- List of Accounts Transactions -->
|
||||||
<ListView fx:id="transactionListView" />
|
<ListView fx:id="transactionListView" />
|
||||||
</center>
|
</center>
|
||||||
|
|
||||||
<bottom>
|
<bottom>
|
||||||
<HBox spacing="10" alignment="CENTER">
|
<HBox spacing="10" alignment="CENTER">
|
||||||
<!-- Buttons / Menüs zur Anzeigeänderung -->
|
<!-- Buttons for Sorting -->
|
||||||
<Button text="Aufsteigend sortieren" onAction="#onSortAscending"/>
|
<Button text="Aufsteigend sortieren" onAction="#onSortAscending"/>
|
||||||
<Button text="Absteigend sortieren" onAction="#onSortDescending"/>
|
<Button text="Absteigend sortieren" onAction="#onSortDescending"/>
|
||||||
<Button text="Nur positive" onAction="#onShowPositive"/>
|
<Button text="Nur positive" onAction="#onShowPositive"/>
|
||||||
|
|||||||
@@ -8,12 +8,7 @@
|
|||||||
<top>
|
<top>
|
||||||
<MenuBar>
|
<MenuBar>
|
||||||
<Menu text="Datei">
|
<Menu text="Datei">
|
||||||
<MenuItem text="Neu"/>
|
<MenuItem text="Neu" onAction="#onNewAccountClicked"/>
|
||||||
<MenuItem text="Öffnen"/>
|
|
||||||
<MenuItem text="Beenden"/>
|
|
||||||
</Menu>
|
|
||||||
<Menu text="Hilfe">
|
|
||||||
<MenuItem text="Über..."/>
|
|
||||||
</Menu>
|
</Menu>
|
||||||
</MenuBar>
|
</MenuBar>
|
||||||
</top>
|
</top>
|
||||||
|
|||||||
Reference in New Issue
Block a user