Skip to content

Commit

Permalink
introfuce second version with Java 21 preview features
Browse files Browse the repository at this point in the history
  • Loading branch information
MBoegers committed Oct 9, 2023
1 parent 606dd50 commit f69a437
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
55 changes: 45 additions & 10 deletions src/main/java/io/github/mboegers/dop/invoice/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

class Main {

//region One method style

/**
* Behandle eine Rechnung abhing davon, ob sie intern oder extern ist.
* Verwendet
Expand All @@ -18,17 +20,15 @@ class Main {
static void sendInvoiceFor(Rechnung rechnung) {
switch (rechnung) {
case InterneVerechnung(var abt, double wert) -> storeInDB(abt, wert);
case ExternVersandt(var kunde, var wert) -> {
double mwst = MwStRechner.SwitchExpressionWhenClauseUnnamed.calculateMwSt(kunde, wert);

switch (kunde) {
case Privatkunde(String name, String address) ->
sendViaMail(address, formatInvoiceText(name, wert, mwst));
case Businesskunde(var name, var address, _) ->
sendViaMail(address, formatInvoiceText(name, wert, mwst));
}
;
case ExternVersandt(Privatkunde(var name, var address), var wert) -> {
var mwst = wert * 0.1;
var text = formatInvoiceText(name, wert, mwst);
sendViaMail(address, text);
}
case ExternVersandt(Businesskunde(var name, var address, var isAbzugBerechtigt), double wert)
when isAbzugBerechtigt -> sendViaMail(address, formatInvoiceText(name, wert, 0d));
case ExternVersandt(Businesskunde(var name, var address, _), double wert) ->
sendViaMail(address, formatInvoiceText(name, wert, wert * 0.1));
}
}

Expand All @@ -43,6 +43,41 @@ private static String formatInvoiceText(String name, double wert, double mwst) {
return txt;
}

// endregion

//region Delegate Style
static void sendInvoiceFor2(Rechnung rechnung) {
switch (rechnung) {
case InterneVerechnung(var abt, double wert) -> storeInDB(abt, wert);
case ExternVersandt(Kunde kunde, var wert) -> {
var mwst = MwStRechner.SwitchExpressionWhenClauseUnnamed.calculateMwSt(kunde, wert);

var text = produceInvoiceText(kunde, wert, mwst);

switch (kunde) {
case Privatkunde(_, var address) -> sendViaMail(address, text);
case Businesskunde(_, var address, _) -> sendViaMail(address, text);
}
}
}
}

private static String produceInvoiceText(Kunde kunde, double wert, double mwst) {
var kundeName = switch (kunde) {
case Privatkunde(String name, var _) -> name;
case Businesskunde(var name, _, _) -> name;
};

return FMT. """
Hallo \{ kundeName },
Bitte senden Sie uns den Rechnungsbetrag in Höhe von %.2f\{ wert }€ plus %.2f\{ mwst }€ MwSt.
Mit freundlichen Grüßen
Merlin Bögershausen
""" ;
}
//endregion

public static void main(String[] args) {
System.out.println("Behandle Rechnung");
var rechnungen = List.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ private SwitchExpressionWhenClauseUnnamed() {
}

public static double calculateMwSt(Kunde kunde, double wert) {
requireNonNull(kunde);
return switch (kunde) {
case Businesskunde(String _, _, var isVorsteuerAbzugsberechtigt)
when isVorsteuerAbzugsberechtigt -> 0.0d;
Expand Down

0 comments on commit f69a437

Please sign in to comment.