Zwischenstand Views

This commit is contained in:
2024-12-07 01:29:34 +01:00
parent 47f9285f37
commit ed73c8e876
7 changed files with 342 additions and 7 deletions

View File

@@ -0,0 +1,98 @@
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
}
}

View File

@@ -0,0 +1,129 @@
package ui;
import bank.PrivateBank;
import javafx.fxml.*;
import javafx.scene.Parent;
import javafx.scene.control.*;
import javafx.scene.input.MouseButton;
import javafx.stage.Stage;
import java.util.List;
import java.util.Optional;
public class MainviewController {
@FXML
private ListView<String> accountListView;
private PrivateBank privateBank; // Referenz auf Ihr Bankobjekt
public void initialize() {
// Bank initialisieren oder übergeben lassen
this.privateBank = new PrivateBank(
"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) {
}
}
accountListView.getItems().addAll(privateBank.getAllAccounts());
// Kontextmenü definieren
ContextMenu contextMenu = new ContextMenu();
MenuItem selectItem = new MenuItem("Auswählen");
MenuItem deleteItem = new MenuItem("Löschen");
// Aktionen definieren
selectItem.setOnAction(event -> {
String selectedAccount = accountListView.getSelectionModel().getSelectedItem();
if (selectedAccount != null) {
switchToAccountView(selectedAccount);
}
});
deleteItem.setOnAction(
event -> {
String selectedAccount = accountListView.getSelectionModel().getSelectedItem();
if (selectedAccount != null) {
boolean confirmed = showDeleteConfirmation(selectedAccount);
if (confirmed) {
// Account aus der Bank entfernen
try {
privateBank.deleteAccount(selectedAccount);
// ListView aktualisieren
accountListView.getItems().remove(selectedAccount);
} catch (Exception e) {
e.printStackTrace();
// Fehlerbehandlung falls nötig
}
}
}
});
contextMenu.getItems().addAll(selectItem, deleteItem);
// Kontextmenü an die ListView binden
accountListView.setContextMenu(contextMenu);
// Optional: Double-Click zum Auswählen ohne Kontextmenü
accountListView.setOnMouseClicked(
event -> {
if (event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 2) {
String selectedAccount = accountListView.getSelectionModel().getSelectedItem();
if (selectedAccount != null) {
switchToAccountView(selectedAccount);
}
}
}
);
}
private void switchToAccountView(String accountName) {
try {
// Laden der zweiten FXML
FXMLLoader loader = new FXMLLoader(getClass().getResource("Accountview.fxml"));
Parent root = loader.load();
// Zugriff auf Controller und Datenübergabe
AccountviewController accountController = loader.getController();
accountController.setAccount(accountName, privateBank);
// Scene in der selben Stage wechseln
Stage stage = (Stage) accountListView.getScene().getWindow();
stage.getScene().setRoot(root);
} catch (Exception e) {
e.printStackTrace();
}
}
private boolean showDeleteConfirmation(String accountName) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Account löschen");
alert.setHeaderText("Soll der Account '" + accountName + "' wirklich gelöscht werden?");
alert.setContentText("Diese 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);
Optional<ButtonType> result = alert.showAndWait();
return result.isPresent() && result.get() == yesButton;
}
}

View File

@@ -0,0 +1,20 @@
package ui;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/Mainview.fxml"));
Scene scene = new Scene(loader.load());
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

View File

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane fx:id="root" xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="ui.AccountviewController">
<top>
<HBox spacing="10" alignment="CENTER_LEFT">
<!-- Back Button -->
<Button text="Back" fx:id="backButton" onAction="#onBackButtonClicked"/>
<!-- Account Name Label -->
<Label fx:id="accountNameLabel" text="Account: "/>
<!-- Kontostand Label -->
<Label fx:id="balanceLabel" text="Balance: "/>
</HBox>
</top>
<center>
<ListView fx:id="transactionListView" />
</center>
<bottom>
<HBox spacing="10" alignment="CENTER">
<!-- Buttons / Menüs zur Anzeigeänderung -->
<Button text="Aufsteigend sortieren" onAction="#onSortAscending"/>
<Button text="Absteigend sortieren" onAction="#onSortDescending"/>
<Button text="Nur positive" onAction="#onShowPositive"/>
<Button text="Nur negative" onAction="#onShowNegative"/>
<Button text="Alle anzeigen" onAction="#onShowAll"/>
</HBox>
</bottom>
</BorderPane>

View File

@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane fx:id="root" xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="ui.MainviewController">
<top>
<MenuBar>
<Menu text="Datei">
<MenuItem text="Neu"/>
<MenuItem text="Öffnen"/>
<MenuItem text="Beenden"/>
</Menu>
<Menu text="Hilfe">
<MenuItem text="Über..."/>
</Menu>
</MenuBar>
</top>
<center>
<ListView fx:id="accountListView"/>
</center>
</BorderPane>

View File

@@ -492,4 +492,16 @@ public class PrivateBankTest {
);
}
@DisplayName("Get all accounts")
@Order(22)
@Test
public void testGetAllAccounts() {
List<String> expectedAccounts = List.of(
"Konto_1", "Konto_3", "Konto_2", "Konto_5", "Konto_4", "Konto_7", "Konto_6", "Konto_8"
);
assertEquals(
expectedAccounts,
privateBank.getAllAccounts()
);
}
}