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 caaad96..0000000 Binary files a/bin/Main.class and /dev/null differ