From 2547bee34d49528870cdc4e04bd3e96a0931eed7 Mon Sep 17 00:00:00 2001 From: cediackermann Date: Mon, 11 May 2026 09:34:35 +0200 Subject: [PATCH] add Exercise 1 and 2 --- 01/Drei.java | 28 +++++++++++++++++++ 01/ShoppingCart.java | 10 ------- 01/ShoppingCartFunctional.java | 25 +++++++++++++++++ 01/ShoppingCartImperative.java | 48 +++++++++++++++++++++++++++++++++ 02/WeDontLikeCharA.swift | 16 +++++++++++ bin/Main.class | Bin 1078 -> 0 bytes 6 files changed, 117 insertions(+), 10 deletions(-) create mode 100644 01/Drei.java delete mode 100644 01/ShoppingCart.java create mode 100644 01/ShoppingCartFunctional.java create mode 100644 01/ShoppingCartImperative.java create mode 100644 02/WeDontLikeCharA.swift delete mode 100644 bin/Main.class diff --git a/01/Drei.java b/01/Drei.java new file mode 100644 index 0000000..db6376a --- /dev/null +++ b/01/Drei.java @@ -0,0 +1,28 @@ +import java.util.ArrayList; +import java.util.List; + +public class Drei { + + public int getTipPercentage(List names) { + if (names.size() <= 2) { + return 0; + } else if (names.size() <= 4) { + return 10; + } else { + return 20; + } + } + + public static void main(String[] args) { + Drei tipCalculator = new Drei(); + List names = new ArrayList<>(); + names.add("test"); + names.add("test2"); + names.add("test3"); + names.add("test4"); + names.add("test5"); + + System.out.println(tipCalculator.getTipPercentage(names)); + } + +} diff --git a/01/ShoppingCart.java b/01/ShoppingCart.java deleted file mode 100644 index bd815d1..0000000 --- a/01/ShoppingCart.java +++ /dev/null @@ -1,10 +0,0 @@ -import java.util.ArrayList; -import java.util.List; - -public class ShoppingCart { - - private List items = new ArrayList(); - private boolean bookAdded; - - public static void main(String[] args) {} -} diff --git a/01/ShoppingCartFunctional.java b/01/ShoppingCartFunctional.java new file mode 100644 index 0000000..02160f1 --- /dev/null +++ b/01/ShoppingCartFunctional.java @@ -0,0 +1,25 @@ +import java.util.Arrays; +import java.util.List; + +public class ShoppingCartFunctional { + + /** + * Pure function that calculates the discount percentage based on the cart content. + * @param cart The list of items in the shopping cart. + * @return 5 if a book is present, 0 otherwise. + */ + public static double getDiscountPercentage(List cart) { + return cart.stream() + .anyMatch(item -> item.toLowerCase().contains("book")) + ? 5 + : 0; + } + + public static void main(String[] args) { + List cartWithBook = Arrays.asList("Apple", "Java Book"); + List cartWithoutBook = Arrays.asList("Apple", "Orange"); + + System.out.println("Discount with book: " + getDiscountPercentage(cartWithBook)); + System.out.println("Discount without book: " + getDiscountPercentage(cartWithoutBook)); + } +} diff --git a/01/ShoppingCartImperative.java b/01/ShoppingCartImperative.java new file mode 100644 index 0000000..f19feb3 --- /dev/null +++ b/01/ShoppingCartImperative.java @@ -0,0 +1,48 @@ +import java.util.ArrayList; +import java.util.List; + +public class ShoppingCartImperative { + private List items = new ArrayList<>(); + private boolean bookAdded = false; + + public void addItem(String item) { + items.add(item); + if (item.toLowerCase().contains("book")) { + bookAdded = true; + } else { + bookAdded = false; + } + } + + public void removeItem(String item) { + items.remove(item); + for (String remainingItem : items) { + if (remainingItem.toLowerCase().contains("book")) { + bookAdded = true; + return; + } else { + bookAdded = false; + } + } + } + + public List getItems() { + return new ArrayList<>(items); + } + + public double getDiscount() { + return bookAdded ? 5 : 0; + } + + public static void main(String[] args) { + ShoppingCartImperative cart = new ShoppingCartImperative(); + cart.addItem("Apple"); + cart.addItem("Java Book"); + System.out.println("Items: " + cart.getItems()); + System.out.println("Discount: " + cart.getDiscount()); + + cart.removeItem("Java Book"); + System.out.println("Items after removal: " + cart.getItems()); + System.out.println("Discount after removal: " + cart.getDiscount()); + } +} diff --git a/02/WeDontLikeCharA.swift b/02/WeDontLikeCharA.swift new file mode 100644 index 0000000..c4306a5 --- /dev/null +++ b/02/WeDontLikeCharA.swift @@ -0,0 +1,16 @@ +struct ScoreWord { + let word: String + + var score: Int { + return word.lowercased().filter { $0 != "a" }.count + } +} + +let inputs: [String] = ["Apple", "Banana", "Cherry", "Date"] + +let scoredWords = inputs.map(ScoreWord.init) +let sortedWords = scoredWords.sorted { $0.score > $1.score } + +for entry in sortedWords { + print("\(entry.word): \(entry.score) points") +} diff --git a/bin/Main.class b/bin/Main.class deleted file mode 100644 index caaad96e8ba4096f60ab54119ff90737dd809848..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1078 zcmZuwOH(qD&oTaKHy~Sjg%U?m#e2YDjU|^FMUl zuyLb{%DCVM=nwHPxDvk`I%Bb9k$dmSch5QBBR~FpKLW6T`7BaUG^i%h$S~x#`7ST} zJlHJ1SlS}d0Ud^htcfm|r^21RomkW;Ds0dtYbhb}!t%?=3t}gZ z1g}+e^e|+-T3tju_I5=UIrM4Bo9M?m26dTx0Yi_=eYfHBSU7GN2_h?=uqC#L$_q9t z_F@(VoY!!{!~g~v`p*z#(CSj&Cswui#$MHNks%d?IxaDEZ3x%r|D24NxFT&B@55*# zk=8Lv3GIoUEXHw7!*vry*r(G@>=0QTR=0RGA4PmmirH&b9TN$t%H$xt?N6Sv4z zBN_T;ahICqi(oU}N}9DuKC0mc$@DIH zfq34it&8Y2U-t=7Ye}gK#kEd8dzGQviMjiBnb(te4YO3>GeR}cT%^{j)Kw>JM6Ouy zB)cISIVBrH70uCv$wvi~Ge++jIt|lGWkI_14eSeT81B(qewKR!_wj&M8Tu1Sf;wNJ z<+~5jn=?K_QxBjjO;{2fA~5|QHhhT79f4N~%(Mkc;Z!1AAaBy6MXOCV=AdDPhW#O- z&!E53P&lS7(@;T*4s4|ZaXOIB7)p#ry_MJwX=jLZLYXZ!F}6H;G^fm{mOA_i1Cy3I zJ(IT5IsO5eea7}xc5wV_=re_%7#>WncF{@x0Noft4@PNkqc1UCfO1T5TEjS*e#EG* k)^AGAkVD=wFxIN9S<094lTA!Daa$_1_rjvn<3#nzUvh`cY5)KL