Commit f3ec87fb authored by Stuht, Torben's avatar Stuht, Torben
Browse files

stuht: refactored my solution by using the given interfaces

parent 5f01466c
package swt.ea2.tdd;
public class BookImpl implements Book {
private String title;
private BookSeriesImpl series;
public BookImpl(String title, BookSeriesImpl series) {
this.title = title;
this.series = series;
}
@Override
public String getTitle() {
return null;
}
@Override
public BookSeries getSeries() {
return null;
}
}
package swt.ea2.tdd.harrypotter;
package swt.ea2.tdd;
import swt.ea2.tdd.harrypotter.Book;
import java.util.ArrayList;
public class Series {
private String title;
public class BookSeriesImpl implements BookSeries {
private String seriesName;
private double pricePerBook;
private ArrayList<Book> books;
public Series() {
public BookSeriesImpl() {
this.books = new ArrayList<>();
}
public String getTitle() {
return title;
@Override
public String getSeriesName() {
return null;
}
public void setTitle(String title) {
this.title = title;
@Override
public double getPricePerBook() {
return 0;
}
public ArrayList<Book> getBooks() {
return books;
}
public void setSeriesName(String seriesName) {
this.seriesName = seriesName;
}
}
package swt.ea2.tdd;
import java.awt.print.Book;
//import java.awt.print.Book;
import java.util.List;
/*
......@@ -9,7 +9,7 @@ import java.util.List;
*/
public interface ShoppingCart {
public List<Book> getBooks();
public List<Book> getBooks(); // todo should we really use java.awt.print.Book and not swt.ea2.tdd.Book?
public double getPrice();
......
package swt.ea2.tdd.harrypotter;
package swt.ea2.tdd;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class BookTrade {
public class ShoppingCartImpl implements ShoppingCart {
private static final double DISCOUNT_2_OF_A_SERIES = 0.05;
private static final double DISCOUNT_3_OF_A_SERIES = 0.1;
private static final double DISCOUNT_4_OF_A_SERIES = 0.2;
private static final double MAXIMUM_DISCOUNT = 0.25;
public double calculatePrice(ShoppingCart cart) {
private ArrayList<Book> books;
@Override
public List<Book> getBooks() {
return null;
}
@Override
public double getPrice() {
double price = 0.0;
ArrayList<Series> series = this.matchOrdersBySeries(cart.getBooks());
List<BookSeriesImpl> series = this.matchOrdersBySeries(this.getBooks());
for (Series seriesElement : series) {
for (BookSeriesImpl seriesElement : series) {
double seriesPrice = 0.0;
// adding up prices for a series
for (Book book : seriesElement.getBooks()) {
seriesPrice += book.getPrice();
seriesPrice += book.getSeries().getPricePerBook();
}
// getting discount
......@@ -46,19 +53,17 @@ public class BookTrade {
//adding price to main price
price = price + seriesPrice;
}
return price;
}
private ArrayList<Series> matchOrdersBySeries(ArrayList<Book> books) {
ArrayList<Series> series = new ArrayList<>();
private ArrayList<BookSeriesImpl> matchOrdersBySeries(List<Book> books) {
ArrayList<BookSeriesImpl> series = new ArrayList<>();
for (Book book : books) {
boolean addedToSeries = false;
for (Series seriesElement : series) {
if (seriesElement.getTitle().equals(book.getSeries())) {
for (BookSeriesImpl seriesElement : series) {
if (seriesElement.getSeriesName().equals(book.getSeries().getSeriesName())) {
// searching for a book with the same title in the series,
// that might have been added before
......@@ -76,13 +81,12 @@ public class BookTrade {
// adding new Series, if book couldn't be added to an existing series
if (!addedToSeries) {
Series newSeries = new Series();
newSeries.setTitle(book.getSeries());
BookSeriesImpl newSeries = new BookSeriesImpl();
newSeries.setSeriesName(book.getSeries().getSeriesName());
newSeries.getBooks().add(book);
series.add(newSeries);
}
}
return series;
}
}
package swt.ea2.tdd.harrypotter;
public class Book {
private String title;
private String series;
private Double price;
public Book(String title, String series, Double price) {
this.title = title;
this.series = series;
this.price = price;
}
public Double getPrice() {
return price;
}
public String getSeries() {
return series;
}
public String getTitle() {
return title;
}
}
package swt.ea2.tdd.harrypotter;
import java.util.ArrayList;
public class ShoppingCart {
private ArrayList<Book> books;
public ShoppingCart() {
this.books = new ArrayList<>();
}
public ArrayList<Book> getBooks() {
return books;
}
}
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