add Exercise 1 and 2

This commit is contained in:
cediackermann 2026-05-11 09:34:35 +02:00
parent 08d40b6241
commit 2547bee34d
6 changed files with 117 additions and 10 deletions

28
01/Drei.java Normal file
View file

@ -0,0 +1,28 @@
import java.util.ArrayList;
import java.util.List;
public class Drei {
public int getTipPercentage(List<String> 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<String> names = new ArrayList<>();
names.add("test");
names.add("test2");
names.add("test3");
names.add("test4");
names.add("test5");
System.out.println(tipCalculator.getTipPercentage(names));
}
}

View file

@ -1,10 +0,0 @@
import java.util.ArrayList;
import java.util.List;
public class ShoppingCart {
private List<String> items = new ArrayList<String>();
private boolean bookAdded;
public static void main(String[] args) {}
}

View file

@ -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<String> cart) {
return cart.stream()
.anyMatch(item -> item.toLowerCase().contains("book"))
? 5
: 0;
}
public static void main(String[] args) {
List<String> cartWithBook = Arrays.asList("Apple", "Java Book");
List<String> cartWithoutBook = Arrays.asList("Apple", "Orange");
System.out.println("Discount with book: " + getDiscountPercentage(cartWithBook));
System.out.println("Discount without book: " + getDiscountPercentage(cartWithoutBook));
}
}

View file

@ -0,0 +1,48 @@
import java.util.ArrayList;
import java.util.List;
public class ShoppingCartImperative {
private List<String> 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<String> 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());
}
}

16
02/WeDontLikeCharA.swift Normal file
View file

@ -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")
}

Binary file not shown.