Commit 07f9002c authored by Behr, Svenja's avatar Behr, Svenja
Browse files

- Einbindung der Interfaces

parent ac403da7
package swt.ea2.bookshop;
public class Book {
import swt.ea2.tdd.Book;
import swt.ea2.tdd.BookSeries;
public class BookImpl implements Book {
private BookSeries series;
private int part;
private String title;
public Book(String title, int part){
public BookImpl(String title){
this.title = title;
this.part = part;
this.series = null;
}
public Book(String title, int part, BookSeries series){
public BookImpl(String title, BookSeries series){
this.title = title;
this.part = part;
this.series = series;
}
public double getPrice(){
if(series != null){
return series.getBasePrice();
return series.getPricePerBook();
}
return 0.0;
}
public BookSeries getSeries(){
return this.series;
@Override
public String getTitle() {
return this.title;
}
public int getPart(){
return this.part;
public BookSeries getSeries(){
return this.series;
}
}
package swt.ea2.bookshop;
public class BookSeries {
import swt.ea2.tdd.BookSeries;
public class BookSeriesImpl implements BookSeries {
private double basePrice;
private String title;
public BookSeries(String title, double price){
public BookSeriesImpl(String title, double price){
this.title = title;
this.basePrice = price;
}
public double getBasePrice(){
@Override
public String getSeriesName() {
return this.title;
}
@Override
public double getPricePerBook() {
return this.basePrice;
}
}
package swt.ea2.bookshop;
import swt.ea2.tdd.Book;
import swt.ea2.tdd.BookSeries;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
......
package swt.ea2.bookshop;
import swt.ea2.tdd.Book;
import swt.ea2.tdd.ShoppingCart;
import java.util.ArrayList;
import java.util.List;
public class Cart {
public class ShoppingCartImpl implements ShoppingCart {
private BookSeriesSet series = new BookSeriesSet();
......@@ -10,10 +14,20 @@ public class Cart {
* Reads the book from the series sorted list
* @return List of all inserted books
*/
public ArrayList<Book> getBooks(){
public List<Book> getBooks(){
return series.getBooks();
}
@Override
public double getPrice() {
double sum = 0.0;
for (ArrayList<Book> seriesSets : series.getSeries()) {
sum += getOneSeriesSetPrice(seriesSets);
}
return sum;
}
/**
* Adds the book to the list
* @param book Book to add to shopping card
......@@ -30,19 +44,6 @@ public class Cart {
series.removeBook(book);
}
/**
* Sums up the prices, based on the set they belong to
* @return sum with more or less the price you would expect
*/
public double getCardSum(){
double sum = 0.0;
for (ArrayList<Book> seriesSets : series.getSeries()) {
sum += getOneSeriesSetPrice(seriesSets);
}
return sum;
}
/**
* Returns the sum for one single series set
* @param seriesSet List of books of one series
......@@ -51,7 +52,7 @@ public class Cart {
private double getOneSeriesSetPrice(ArrayList<Book> seriesSet) {
double oneSetSum = 0.0;
for(Book b : seriesSet){
oneSetSum += b.getPrice();
oneSetSum += b.getSeries().getPricePerBook();
}
oneSetSum *= getDiscount(seriesSet.size());
......
package swt.ea2.tdd;
import java.awt.print.Book;
import java.util.List;
/*
......
......@@ -2,33 +2,35 @@ package swt.ea2.test.bookshop;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import swt.ea2.bookshop.Book;
import swt.ea2.bookshop.BookSeries;
import swt.ea2.bookshop.Cart;
import swt.ea2.bookshop.BookImpl;
import swt.ea2.bookshop.BookSeriesImpl;
import swt.ea2.bookshop.ShoppingCartImpl;
import swt.ea2.tdd.Book;
import swt.ea2.tdd.BookSeries;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CartTest {
public class ShoppingCartTest {
private Cart c;
private ShoppingCartImpl c;
private BookSeries[] bs = {new BookSeries("Harry Potter", 8),
new BookSeries("Nevernight Chroniken", 13)};
private BookSeries[] bs = {new BookSeriesImpl("Harry Potter", 8),
new BookSeriesImpl("Nevernight Chroniken", 13)};
private Book[] bookShelf = {new Book("Harry Potter und der Stein der Weisen", 1, bs[0]),
new Book("Harry Potter und die Kammer des Schreckens", 2, bs[0]),
new Book("Harry Potter und der Gefangene von Askaban", 3, bs[0]),
new Book("Harry Potter und der Feuerkelch", 4, bs[0]),
new Book("Harry Potter und der Orden des Phönix", 5, bs[0]),
new Book("Harry Potter und der Halbblutprinz", 6, bs[0]),
new Book("Harry Potter und die Heiligtümer des Todes", 7, bs[0]),
new Book("Die Prüfung", 1, bs[1]),
new Book("Das Spiel", 2, bs[1]),
new Book("Die Rache", 3, bs[1])};
private Book[] bookShelf = {new BookImpl("Harry Potter und der Stein der Weisen", bs[0]),
new BookImpl("Harry Potter und die Kammer des Schreckens", bs[0]),
new BookImpl("Harry Potter und der Gefangene von Askaban", bs[0]),
new BookImpl("Harry Potter und der Feuerkelch", bs[0]),
new BookImpl("Harry Potter und der Orden des Phönix", bs[0]),
new BookImpl("Harry Potter und der Halbblutprinz", bs[0]),
new BookImpl("Harry Potter und die Heiligtümer des Todes", bs[0]),
new BookImpl("Die Prüfung", bs[1]),
new BookImpl("Das Spiel", bs[1]),
new BookImpl("Die Rache", bs[1])};
@BeforeEach
public void init(){
c = new Cart();
c = new ShoppingCartImpl();
}
@Test
......@@ -59,38 +61,38 @@ public class CartTest {
@Test
public void testGetCardSum_ForEmptyCard(){
assertEquals(0.0, c.getCardSum());
assertEquals(0.0, c.getPrice());
}
@Test
public void testGetCardSum_NoDiscount_OneBook(){
c.addBook(bookShelf[0]);
assertEquals(8.0, c.getCardSum());
assertEquals(8.0, c.getPrice());
}
@Test
public void testGetCardSum_NoDiscount_TwoDifferentBooks(){
c.addBook(bookShelf[0]);
c.addBook(bookShelf[7]);
assertEquals(21.0, c.getCardSum());
assertEquals(21.0, c.getPrice());
}
@Test
public void testGetCardSum_NoDiscount_SeveralOfASeries(){
c.addBook(bookShelf[0]);
assertEquals(8, c.getCardSum());
assertEquals(8, c.getPrice());
c.addBook(bookShelf[1]);
assertEquals(15.2, c.getCardSum());
assertEquals(15.2, c.getPrice());
c.addBook(bookShelf[2]);
assertEquals(21.6, c.getCardSum());
assertEquals(21.6, c.getPrice());
c.addBook(bookShelf[3]);
assertEquals(25.6, c.getCardSum());
assertEquals(25.6, c.getPrice());
c.addBook(bookShelf[4]);
assertEquals(30, c.getCardSum());
assertEquals(30, c.getPrice());
}
public void testGetCardSum_LikeMentioned(){
......@@ -108,6 +110,6 @@ public class CartTest {
c.addBook(bookShelf[1]);
c.addBook(bookShelf[2]);
assertEquals(51.6, c.getCardSum());
assertEquals(51.6, c.getPrice());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment