Beispielhafter Testfall (+ Warenkorb-Klasse) eingefügt.

main
hummel 2025-12-08 14:32:08 +01:00
parent 8de8329e02
commit 1ddadc8dcf
4 changed files with 50 additions and 2 deletions

View File

@ -8,7 +8,7 @@ public class OnlineShop {
public OnlineShop() {
lager = new ArrayList<Produkt>();
lager.add(new Produkt("Wein"));
lager.add(new Produkt("Wein", 4.99));
}
public String[] produktListe() {

View File

@ -2,9 +2,11 @@ package de.th_mannheim.informatik.shop.backend;
public class Produkt {
String name;
double preis;
public Produkt(String name) {
public Produkt(String name, double preis) {
this.name = name;
this.preis = preis;
}
public String toString() {

View File

@ -0,0 +1,25 @@
package de.th_mannheim.informatik.shop.backend;
import java.util.ArrayList;
public class Warenkorb {
private ArrayList<Produkt> inhalt;
public Warenkorb() {
inhalt = new ArrayList<Produkt>();
}
public void produktHinzufügen(Produkt p) {
inhalt.add(p);
}
public double berechneGesamtpreis() {
double preis = 0;
for (Produkt p : inhalt)
preis+= p.preis;
return preis;
}
}

View File

@ -0,0 +1,21 @@
package de.th_mannheim.informatik.shop.backend;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class WarenkorbTest {
@Test
void testGesamtPreis() {
Produkt p1 = new Produkt("Wein", 4.99);
Produkt p2 = new Produkt("Wasser", 0.7);
Warenkorb wk = new Warenkorb();
wk.produktHinzufügen(p1);
wk.produktHinzufügen(p2);
assertEquals(5.69, wk.berechneGesamtpreis(), 0.001);
}
}