Beispielhafter Testfall (+ Warenkorb-Klasse) eingefügt.
parent
8de8329e02
commit
1ddadc8dcf
|
|
@ -8,7 +8,7 @@ public class OnlineShop {
|
||||||
public OnlineShop() {
|
public OnlineShop() {
|
||||||
lager = new ArrayList<Produkt>();
|
lager = new ArrayList<Produkt>();
|
||||||
|
|
||||||
lager.add(new Produkt("Wein"));
|
lager.add(new Produkt("Wein", 4.99));
|
||||||
}
|
}
|
||||||
|
|
||||||
public String[] produktListe() {
|
public String[] produktListe() {
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,11 @@ package de.th_mannheim.informatik.shop.backend;
|
||||||
|
|
||||||
public class Produkt {
|
public class Produkt {
|
||||||
String name;
|
String name;
|
||||||
|
double preis;
|
||||||
|
|
||||||
public Produkt(String name) {
|
public Produkt(String name, double preis) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
this.preis = preis;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue