Erweiterung der Tests

Anpassungen beim Hinzufügen von Transaktionen
This commit is contained in:
2024-12-10 12:30:43 +01:00
parent 1eb50ea3be
commit 202f13d2bf
4 changed files with 130 additions and 40 deletions

View File

@@ -147,9 +147,15 @@ public class AccountviewController {
*/
private void showTransactions(List<Transaction> transactions) {
transactionListView.getItems().setAll(transactions);
updateAccountInfo(); // Balance neu anzeigen, falls sich was geändert hat
updateAccountInfo();
}
/**
* Confirmation of transaction delete
*
* @param transaction transaction to be deleted
* @return confirmation
*/
private boolean showDeleteConfirmation(Transaction transaction) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Transaktion löschen");
@@ -165,7 +171,16 @@ public class AccountviewController {
return result.isPresent() && result.get() == yesButton;
}
private boolean validateTransactionInput(ComboBox<String> typeComboBox, TextField amountField, TextField descriptionField, TextField senderReceiverField) {
/**
* Validates user inputs for adding transaction
*
* @param typeComboBox
* @param amountField
* @param descriptionField
* @param senderReceiverField
* @return
*/
private boolean validateTransactionInput(ComboBox<String> typeComboBox, TextField amountField, TextField descriptionField, TextField senderField, TextField recieverField) {
String type = typeComboBox.getValue();
String amountStr = amountField.getText().trim();
String desc = descriptionField.getText().trim();
@@ -179,9 +194,13 @@ public class AccountviewController {
return false;
}
if (type.equals("Transfer")) {
String partner = senderReceiverField.getText().trim();
if (partner.isEmpty()) return false;
if (type.equals("Überweisung")) {
String sender = senderField.getText().trim();
String reciever = recieverField.getText().trim();
return (sender.equals(currentAccount) || reciever.equals(currentAccount)) &&
!sender.equals(reciever) &&
!sender.isEmpty() && !reciever.isEmpty();
}
return true;
@@ -193,14 +212,17 @@ public class AccountviewController {
dialog.setHeaderText("Bitte geben Sie die Daten der neuen Transaktion ein.");
// Buttons
ButtonType okButton = new ButtonType("OK", ButtonBar.ButtonData.OK_DONE);
ButtonType okButton = new ButtonType("Hinzufügen", ButtonBar.ButtonData.OK_DONE);
ButtonType cancelButton = new ButtonType("Abbrechen", ButtonBar.ButtonData.CANCEL_CLOSE);
dialog.getDialogPane().getButtonTypes().addAll(okButton, cancelButton);
// UI-Elemente
Label dateLabel = new Label("Datum:");
TextField dateField = new TextField();
Label typeLabel = new Label("Typ:");
ComboBox<String> typeComboBox = new ComboBox<>();
typeComboBox.getItems().addAll("Payment", "Transfer");
typeComboBox.getItems().addAll("Ein-/Auszahlung", "Überweisung");
typeComboBox.getSelectionModel().selectFirst(); // Default Payment
Label amountLabel = new Label("Betrag:");
@@ -209,8 +231,10 @@ public class AccountviewController {
Label descriptionLabel = new Label("Beschreibung:");
TextField descriptionField = new TextField();
Label senderReceiverLabel = new Label("Partner-Konto (nur bei Transfer):");
TextField senderReceiverField = new TextField();
Label senderLabel = new Label("Sender:");
TextField senderField = new TextField();
Label recieverLabel = new Label("Empfänger:");
TextField recieverField = new TextField();
// Layout
GridPane grid = new GridPane();
@@ -220,14 +244,20 @@ public class AccountviewController {
grid.add(typeLabel, 0, 0);
grid.add(typeComboBox, 1, 0);
grid.add(amountLabel, 0, 1);
grid.add(amountField, 1, 1);
grid.add(dateLabel, 0, 1);
grid.add(dateField, 1, 1);
grid.add(descriptionLabel, 0, 2);
grid.add(descriptionField, 1, 2);
grid.add(amountLabel, 0, 2);
grid.add(amountField, 1, 2);
grid.add(senderReceiverLabel, 0, 3);
grid.add(senderReceiverField, 1, 3);
grid.add(descriptionLabel, 0, 3);
grid.add(descriptionField, 1, 3);
grid.add(senderLabel, 0, 4);
grid.add(senderField, 1, 4);
grid.add(recieverLabel, 0, 5);
grid.add(recieverField, 1, 5);
dialog.getDialogPane().setContent(grid);
@@ -235,13 +265,13 @@ public class AccountviewController {
Node okBtnNode = dialog.getDialogPane().lookupButton(okButton);
okBtnNode.addEventFilter(ActionEvent.ACTION, event -> {
// Validierung
if (!validateTransactionInput(typeComboBox, amountField, descriptionField, senderReceiverField)) {
if (!validateTransactionInput(typeComboBox, amountField, descriptionField, senderField, recieverField)) {
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.setContentText("- der Betrag muss eine Zahl sein \n- die Beschreibung darf nicht leer sein.\n " +
"- bei einer Überweisung dürfen Sender & Empfänger nicht leer und nicht gleich sein");
alert.showAndWait();
}
});
@@ -252,23 +282,23 @@ public class AccountviewController {
String type = typeComboBox.getValue();
double amount = Double.parseDouble(amountField.getText());
String description = descriptionField.getText().trim();
String date = LocalDate.now().toString();
String date = dateField.getText().trim();
if (dateField.getText().trim().isEmpty())
date = LocalDate.now().toString();
if (type.equals("Payment")) {
if (type.equals("Ein-/Auszahlung")) {
return new bank.Payment(date, amount, description);
} else {
String partnerAccount = senderReceiverField.getText().trim();
if (partnerAccount.isEmpty()) return null;
String recieverAccount = recieverField.getText().trim();
String senderAccount = senderField.getText().trim();
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);
if (recieverAccount.isEmpty() && senderAccount.isEmpty() || recieverAccount.equals(senderAccount))
return null;
else if (senderAccount.equals(currentAccount))
return new bank.OutgoingTransfer(date, amount, description, currentAccount, recieverAccount);
else if (recieverAccount.equals(currentAccount))
return new bank.IncomingTransfer(date, amount, description, recieverAccount, currentAccount);
}
}
} catch (NumberFormatException e) {
// Sollte nie hier landen, da oben schon abgefangen, aber falls doch:
return null;

View File

@@ -4,14 +4,13 @@ import bank.PrivateBank;
import bank.exceptions.AccountAlreadyExistsException;
import bank.exceptions.IOException;
import javafx.fxml.*;
import javafx.scene.Parent;
import javafx.scene.control.*;
import javafx.scene.input.MouseButton;
import javafx.stage.Stage;
import java.awt.event.ActionEvent;
import java.util.List;
import java.util.Optional;

View File

@@ -1,6 +1,10 @@
package ui;
import bank.PrivateBank;
import bank.exceptions.AccountAlreadyExistsException;
import bank.exceptions.IOException;
import bank.exceptions.TransactionAlreadyExistException;
import bank.exceptions.TransactionAttributeException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
@@ -9,6 +13,7 @@ import javafx.stage.Stage;
public class main extends Application {
/**
* Starting point
*
* @param primaryStage
* @throws Exception
*/
@@ -24,9 +29,19 @@ public class main extends Application {
launch(args);
}
private static PrivateBank privateBank = new PrivateBank("Bank1", 0, 0, "accountdata");
private static PrivateBank privateBank = new PrivateBank(
"Bank1",
0,
0,
"accountdata");
public static PrivateBank getPrivateBank() {
try {
privateBank.readAccounts();
} catch (IOException | AccountAlreadyExistsException | TransactionAlreadyExistException |
TransactionAttributeException e) {
System.out.println(e.getMessage());
}
return privateBank;
}

View File

@@ -356,7 +356,7 @@ public class PrivateBankTest {
@Test
@Order(16)
public void getTransactionsByTypeTest() {
List<Transaction> transactionList = List.of(
List<Transaction> transactionsPositive = List.of(
new IncomingTransfer(
"01.01.2000",
300,
@@ -365,14 +365,30 @@ public class PrivateBankTest {
"Konto_1"
)
);
List<Transaction> transactionsNegative = List.of(
new Payment(
"01.01.2000",
-100,
"Payment",
0,
0
)
);
assertEquals(
transactionList,
transactionsPositive,
privateBank.getTransactionsByType(
"Konto_1",
false
)
);
System.out.println("getTransactionByTypeTest in <Konto_1> is correct.");
assertEquals(
transactionsNegative,
privateBank.getTransactionsByType(
"Konto_1",
true
)
);
}
@Test
@@ -408,9 +424,39 @@ public class PrivateBankTest {
true
)
);
assertEquals(
List.of(
new IncomingTransfer(
"01.01.2000",
300,
"IncomingTransfer from Tom",
"Tom",
"Konto_6"
),
new IncomingTransfer(
"01.01.2000",
100,
"IncomingTransfer from Hans",
"Hans",
"Konto_6"
),
new OutgoingTransfer(
"01.01.2000",
100,
"OutgoingTransfer to Hans",
"Konto_6",
"Hans"
)
),
privateBank.getTransactionsSorted(
"Konto_6",
false
)
);
}
@DisplayName("Serialize Accounts")
@DisplayName("Serialize existing Accounts")
@Order(18)
@ParameterizedTest
@ValueSource(strings = {"Konto_1", "Konto_2", "Konto_3", "Konto_4", "Konto_5", "Konto_6", "Konto_7", "Konto_8"})
@@ -421,16 +467,16 @@ public class PrivateBankTest {
System.out.println("SerializeAccountsTest in <" + account + "> is correct.");
}
@DisplayName("Deserialize Accounts")
@DisplayName("Deserialize existing Accounts")
@Order(19)
@Test
public void DeserializeAccountsTest() {
// Die eingelesenen Konten sind bereits vorhanden
assertThrows(
AccountAlreadyExistsException.class, () -> privateBank.readAccounts()
);
}
@DisplayName("Delete existing account")
@Order(20)
@Test