Skip to content
Browse files

- Minor refactorings.

  • Loading branch information...
1 parent dca1146 commit 4a1a872f3ac0bf09281f7dc12fd9a5bfcb7b8388 @alexbiehl alexbiehl committed
View
4 src/main/java/org/fhw/asta/kasse/client/activity/PrintCustomsActivity.java
@@ -239,8 +239,8 @@ private void printArticles() {
strb.append("<table class='regtb'><tr class='headline'><td>Menge</td><td class='desc'>Beschreibung</td><td>Rabatt (&#037;)</td><td>E-Preis</td><td>G-Preis</td><td>Abgehohlt</td></tr>");
for (BasketItem art : articles) {
- EuroAmount euroAmount = art.getItemPrice().times(art.getAmount()).withDiscount(art.getDiscount());
-
+ EuroAmount euroAmount = art.totalWithDiscount();
+
/* EuroAmount euroAmount = new EuroAmount((int) Math.round((art
.getItemPrice().getCentAmount() * art.getAmount())
* ((100.0 - art.getDiscount()) / 100)));*/
View
453 src/main/java/org/fhw/asta/kasse/client/controller/BasketController.java
@@ -2,7 +2,6 @@
import java.util.Collections;
import java.util.Comparator;
-import java.util.Iterator;
import javax.annotation.Nullable;
@@ -15,7 +14,8 @@
import org.fhw.asta.kasse.shared.model.Article;
import org.fhw.asta.kasse.shared.service.checkout.CheckoutServiceAsync;
-import com.google.common.base.Function;
+import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
@@ -32,232 +32,277 @@
import com.google.inject.Inject;
public class BasketController {
- private final ListDataProvider<BasketItem> basketDataProvider;
+ private final ListDataProvider<BasketItem> basketDataProvider;
- private BasketWidget basketWidget;
+ private BasketWidget basketWidget;
- private FieldUpdater<BasketItem, String> deleteUpdater;
- private FieldUpdater<BasketItem, String> amountUpdater;
- private FieldUpdater<BasketItem, String> discountUpdater;
- private ValueChangeHandler<String> masterDiscountUpdater;
- private ValueChangeHandler<String> matrNrUpdater;
- private ClickHandler checkoutHandler;
- private ClickHandler checkoutHandlerUnPayed;
- private BasketComparator basketComparator = new BasketComparator();
+ private FieldUpdater<BasketItem, String> deleteUpdater;
+ private FieldUpdater<BasketItem, String> amountUpdater;
+ private FieldUpdater<BasketItem, String> discountUpdater;
+ private ValueChangeHandler<String> masterDiscountUpdater;
+ private ValueChangeHandler<String> matrNrUpdater;
+ private ClickHandler checkoutHandler;
+ private ClickHandler checkoutHandlerUnPayed;
+ private BasketComparator basketComparator = new BasketComparator();
- @Inject
- private CheckoutServiceAsync checkoutService;
+ @Inject
+ private CheckoutServiceAsync checkoutService;
- @Inject
- private PlaceController placeController;
+ @Inject
+ private PlaceController placeController;
- public BasketController() {
- this.basketDataProvider = new ListDataProvider<BasketItem>(new BasketItemKeyProvider());
- }
+ public BasketController() {
+ this.basketDataProvider = new ListDataProvider<BasketItem>(
+ new BasketItemKeyProvider());
+ }
- @Inject
- public void init(BasketWidget basketWidget) {
+ @Inject
+ public void init(BasketWidget basketWidget) {
- this.basketWidget = basketWidget;
+ this.basketWidget = basketWidget;
- this.basketDataProvider.addDataDisplay(this.basketWidget.getBasketTable());
+ this.basketDataProvider.addDataDisplay(this.basketWidget
+ .getBasketTable());
- this.deleteUpdater = new DeleteUpdater();
- basketWidget.getDeleteColumn().setFieldUpdater(this.deleteUpdater);
+ this.deleteUpdater = new DeleteUpdater();
+ basketWidget.getDeleteColumn().setFieldUpdater(this.deleteUpdater);
- this.amountUpdater = new AmountUpdater();
- basketWidget.getAmountColumn().setFieldUpdater(this.amountUpdater);
+ this.amountUpdater = new AmountUpdater();
+ basketWidget.getAmountColumn().setFieldUpdater(this.amountUpdater);
- this.discountUpdater = new DiscountUpdater();
- basketWidget.getDiscountColumn().setFieldUpdater(this.discountUpdater);
+ this.discountUpdater = new DiscountUpdater();
+ basketWidget.getDiscountColumn().setFieldUpdater(this.discountUpdater);
- this.masterDiscountUpdater = new MasterDiscountUpdater();
- basketWidget.getDiscountBox().addValueChangeHandler(this.masterDiscountUpdater);
+ this.masterDiscountUpdater = new MasterDiscountUpdater();
+ basketWidget.getDiscountBox().addValueChangeHandler(
+ this.masterDiscountUpdater);
- this.checkoutHandler = new CheckoutHandler();
- basketWidget.getCheckoutButton().addClickHandler(this.checkoutHandler);
+ this.checkoutHandler = new CheckoutHandler();
+ basketWidget.getCheckoutButton().addClickHandler(this.checkoutHandler);
- this.checkoutHandlerUnPayed = new CheckoutHandlerUnPayed();
- basketWidget.getCheckoutButtonUnPayed().addClickHandler(this.checkoutHandlerUnPayed);
+ this.checkoutHandlerUnPayed = new CheckoutHandlerUnPayed();
+ basketWidget.getCheckoutButtonUnPayed().addClickHandler(
+ this.checkoutHandlerUnPayed);
+
+ this.matrNrUpdater = new MatrNrUpdater();
+ basketWidget.getMatrNrBox().addValueChangeHandler(this.matrNrUpdater);
+ }
- this.matrNrUpdater = new MatrNrUpdater();
- basketWidget.getMatrNrBox().addValueChangeHandler(this.matrNrUpdater);
- }
+ private static class SameArticleIdPredicate implements
+ Predicate<BasketItem> {
- public void addBasketPosition(BasketItem basketItem) {
+ private final int articleId;
- final Iterator<BasketItem> basketIterator = this.basketDataProvider.getList().iterator();
- while (basketIterator.hasNext()) {
- final BasketItem next = basketIterator.next();
- if (next.getArticleId() == basketItem.getArticleId()) {
- basketItem = new BasketItem(basketItem.getItemName(), basketItem.getItemPrice(), basketItem.getArticleId(),
- basketItem.getAmount() + next.getAmount(), next.getDiscount());
- this.basketDataProvider.getList().remove(next);
- }
- }
+ public SameArticleIdPredicate(int articleId) {
+ this.articleId = articleId;
+ }
- this.basketDataProvider.getList().add(basketItem);
- this.flush();
+ @Override
+ public boolean apply(@Nullable BasketItem input) {
+ return input.getArticleId() == articleId;
+ }
+
+ }
+
+ public void addBasketPosition(BasketItem basketItem) {
+
+ Optional<BasketItem> maybeBasketItem = Iterables.tryFind(
+ basketDataProvider.getList(), new SameArticleIdPredicate(
+ basketItem.getArticleId()));
- }
+
+ if (maybeBasketItem.isPresent()) {
+
+ BasketItem bi = maybeBasketItem.get();
+
+ this.basketDataProvider.getList().remove(bi);
+
+ basketItem = basketItem.addAmount(bi.getAmount()).updateDiscount(bi.getDiscount());
+ }
+
+ this.basketDataProvider.getList().add(basketItem);
+ this.flush();
+ }
+
+ public void addBasketPosition(Article article) {
- public void addBasketPosition(Article article) {
+ this.addBasketPosition(new BasketItem(article.getName(), article
+ .getPrice(), article.getId(), 1));
+ }
+
+ private void flush() {
+ Collections.sort(this.basketDataProvider.getList(),
+ this.basketComparator);
+
+ EuroAmount sum = EuroAmount.ZERO_AMOUNT;
+
+ for (BasketItem bi : basketDataProvider.getList()) {
+ sum = sum.plus(bi.totalWithDiscount());
+ }
+
+ int discount = 0;
+
+ try {
+ discount = Integer.valueOf(this.basketWidget.getDiscountBox()
+ .getText());
+ } catch (final NumberFormatException e) {
+ discount = 0;
+ }
+
+ this.basketWidget.getSumLabel().setText(
+ EuroFormatter.format(sum.withDiscount(discount)));
+ }
+
+ private class CheckoutHandler implements ClickHandler {
+
+ @Override
+ public void onClick(ClickEvent event) {
+ BasketController.this.checkoutService
+ .doCheckout(
+ Lists.newArrayList(BasketController.this.basketDataProvider
+ .getList()), Integer
+ .valueOf(BasketController.this.basketWidget
+ .getDiscountBox().getText()),
+ BasketController.this.basketWidget.getMatrNrBox()
+ .getText(), 'p', new CheckoutCallback());
+ BasketController.this.flush();
+ }
+
+ }
+
+ private class CheckoutHandlerUnPayed implements ClickHandler {
+
+ @Override
+ public void onClick(ClickEvent event) {
+ BasketController.this.checkoutService
+ .doCheckout(
+ Lists.newArrayList(BasketController.this.basketDataProvider
+ .getList()), Integer
+ .valueOf(BasketController.this.basketWidget
+ .getDiscountBox().getText()),
+ BasketController.this.basketWidget.getMatrNrBox()
+ .getText(), 'o', new CheckoutCallback());
+ BasketController.this.flush();
+ }
+
+ }
- this.addBasketPosition(new BasketItem(article.getName(), article.getPrice(), article.getId(), 1));
- }
+ private class AmountUpdater implements FieldUpdater<BasketItem, String> {
+
+ @Override
+ public void update(int index, BasketItem object, String value) {
+ BasketItem toUpdate;
+ if (value.matches("[1-9][0-9]*")) {
+ toUpdate = new BasketItem(object.getItemName(),
+ object.getItemPrice(), object.getArticleId(),
+ Integer.valueOf(value), object.getDiscount());
+
+ } else {
+ toUpdate = new BasketItem(object.getItemName(),
+ object.getItemPrice(), object.getArticleId(), 1,
+ object.getDiscount());
+ }
+
+ BasketController.this.basketDataProvider.getList().remove(object);
+ BasketController.this.basketDataProvider.getList().add(toUpdate);
+ BasketController.this.flush();
+
+ }
+ }
+
+ private class DeleteUpdater implements FieldUpdater<BasketItem, String> {
+
+ @Override
+ public void update(int index, BasketItem object, String value) {
+ BasketController.this.basketDataProvider.getList().remove(object);
+ BasketController.this.flush();
+ }
+ }
+
+ private class DiscountUpdater implements FieldUpdater<BasketItem, String> {
+
+ @Override
+ public void update(int index, BasketItem object, String value) {
+ BasketItem toUpdate;
+
+ if (value.matches("[0-9]+")) {
+ toUpdate = new BasketItem(object.getItemName(),
+ object.getItemPrice(), object.getArticleId(),
+ object.getAmount(), Integer.valueOf(value));
+ } else {
+ toUpdate = new BasketItem(object.getItemName(),
+ object.getItemPrice(), object.getArticleId(),
+ object.getAmount(), 0);
+ }
+
+ BasketController.this.basketDataProvider.getList().remove(object);
+ BasketController.this.basketDataProvider.getList().add(toUpdate);
+ BasketController.this.flush();
+
+ }
+
+ }
+
+ private class MasterDiscountUpdater implements ValueChangeHandler<String> {
+
+ @Override
+ public void onValueChange(ValueChangeEvent<String> event) {
+ if (!event.getValue().matches("[0-9]+")) {
+ BasketController.this.basketWidget.getDiscountBox()
+ .setText("0");
+ }
+ BasketController.this.flush();
+ }
+
+ }
+
+ private class MatrNrUpdater implements ValueChangeHandler<String> {
+
+ @Override
+ public void onValueChange(ValueChangeEvent<String> event) {
+ BasketController.this.flush();
+ }
+
+ }
+
+ private static final class BasketItemKeyProvider implements
+ ProvidesKey<BasketItem> {
+
+ @Override
+ public Object getKey(BasketItem item) {
+ return item.getArticleId();
+ }
+
+ }
+
+ private final class BasketComparator implements Comparator<BasketItem> {
+
+ @Override
+ public int compare(BasketItem o1, BasketItem o2) {
+ return ComparisonChain.start()
+ .compare(o1.getArticleId(), o2.getArticleId()).result();
+ }
- private void flush() {
- Collections.sort(this.basketDataProvider.getList(), this.basketComparator);
+ }
- EuroAmount sum = EuroAmount.ZERO_AMOUNT;
-
- for (BasketItem bi : basketDataProvider.getList()) {
- sum = sum.plus(bi.getItemPrice().times(bi.getAmount()).withDiscount(bi.getDiscount()));
- }
-
- int discount = 0;
+ private class CheckoutCallback implements AsyncCallback<Integer> {
- try {
- discount = Integer.valueOf(this.basketWidget.getDiscountBox().getText());
- } catch (final NumberFormatException e) {
- discount = 0;
- }
+ @Override
+ public void onFailure(Throwable caught) {
+ // TODO FIXME
+ }
+
+ @Override
+ public void onSuccess(Integer result) {
+ BasketController.this.basketDataProvider.getList().clear();
+ final PrintCustomsToken token = new PrintCustomsToken(
+ PrintType.BILLORDER, result);
+ Window.open(
+ Window.Location.createUrlBuilder()
+ .setHash("PrintCustomsPlace:").buildString()
+ + token.toString(), "_blank", "");
+ }
- this.basketWidget.getSumLabel().setText(EuroFormatter.format(sum.withDiscount(discount)));
- }
-
- private class CheckoutHandler implements ClickHandler {
-
- @Override
- public void onClick(ClickEvent event) {
- BasketController.this.checkoutService.doCheckout(
- Lists.newArrayList(BasketController.this.basketDataProvider.getList()),
- Integer.valueOf(BasketController.this.basketWidget.getDiscountBox().getText()),
- BasketController.this.basketWidget.getMatrNrBox().getText(), 'p', new CheckoutCallback());
- BasketController.this.flush();
- }
-
- }
-
- private class CheckoutHandlerUnPayed implements ClickHandler {
-
- @Override
- public void onClick(ClickEvent event) {
- BasketController.this.checkoutService.doCheckout(
- Lists.newArrayList(BasketController.this.basketDataProvider.getList()),
- Integer.valueOf(BasketController.this.basketWidget.getDiscountBox().getText()),
- BasketController.this.basketWidget.getMatrNrBox().getText(), 'o', new CheckoutCallback());
- BasketController.this.flush();
- }
-
- }
-
- private class AmountUpdater implements FieldUpdater<BasketItem, String> {
-
- @Override
- public void update(int index, BasketItem object, String value) {
- BasketItem toUpdate;
- if (value.matches("[1-9][0-9]*")) {
- toUpdate = new BasketItem(object.getItemName(), object.getItemPrice(), object.getArticleId(),
- Integer.valueOf(value), object.getDiscount());
-
- } else {
- toUpdate = new BasketItem(object.getItemName(), object.getItemPrice(), object.getArticleId(), 1,
- object.getDiscount());
- }
-
- BasketController.this.basketDataProvider.getList().remove(object);
- BasketController.this.basketDataProvider.getList().add(toUpdate);
- BasketController.this.flush();
-
- }
- }
-
- private class DeleteUpdater implements FieldUpdater<BasketItem, String> {
-
- @Override
- public void update(int index, BasketItem object, String value) {
- BasketController.this.basketDataProvider.getList().remove(object);
- BasketController.this.flush();
- }
- }
-
- private class DiscountUpdater implements FieldUpdater<BasketItem, String> {
-
- @Override
- public void update(int index, BasketItem object, String value) {
- BasketItem toUpdate;
-
- if (value.matches("[0-9]+")) {
- toUpdate = new BasketItem(object.getItemName(), object.getItemPrice(), object.getArticleId(),
- object.getAmount(), Integer.valueOf(value));
- } else {
- toUpdate = new BasketItem(object.getItemName(), object.getItemPrice(), object.getArticleId(),
- object.getAmount(), 0);
- }
-
- BasketController.this.basketDataProvider.getList().remove(object);
- BasketController.this.basketDataProvider.getList().add(toUpdate);
- BasketController.this.flush();
-
- }
-
- }
-
- private class MasterDiscountUpdater implements ValueChangeHandler<String> {
-
- @Override
- public void onValueChange(ValueChangeEvent<String> event) {
- if (!event.getValue().matches("[0-9]+")) {
- BasketController.this.basketWidget.getDiscountBox().setText("0");
- }
- BasketController.this.flush();
- }
-
- }
-
- private class MatrNrUpdater implements ValueChangeHandler<String> {
-
- @Override
- public void onValueChange(ValueChangeEvent<String> event) {
- BasketController.this.flush();
- }
-
- }
-
- private static final class BasketItemKeyProvider implements ProvidesKey<BasketItem> {
-
- @Override
- public Object getKey(BasketItem item) {
- return item.getArticleId();
- }
-
- }
-
- private final class BasketComparator implements Comparator<BasketItem> {
-
- @Override
- public int compare(BasketItem o1, BasketItem o2) {
- return ComparisonChain.start().compare(o1.getArticleId(), o2.getArticleId()).result();
- }
-
- }
-
- private class CheckoutCallback implements AsyncCallback<Integer> {
-
- @Override
- public void onFailure(Throwable caught) {
- // TODO FIXME
- }
-
- @Override
- public void onSuccess(Integer result) {
- BasketController.this.basketDataProvider.getList().clear();
- final PrintCustomsToken token = new PrintCustomsToken(PrintType.BILLORDER, result);
- Window.open(Window.Location.createUrlBuilder().setHash("PrintCustomsPlace:").buildString() + token.toString(),
- "_blank", "");
- }
-
- }
+ }
}
View
94 src/main/java/org/fhw/asta/kasse/shared/basket/BasketItem.java
@@ -6,46 +6,6 @@
public class BasketItem implements Serializable {
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + this.amount;
- result = prime * result + this.articleId;
- result = prime * result + this.discount;
- result = prime * result + ((this.itemName == null) ? 0 : this.itemName.hashCode());
- result = prime * result + ((this.itemPrice == null) ? 0 : this.itemPrice.hashCode());
- return result;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (this.getClass() != obj.getClass())
- return false;
- final BasketItem other = (BasketItem) obj;
- if (this.amount != other.amount)
- return false;
- if (this.articleId != other.articleId)
- return false;
- if (this.discount != other.discount)
- return false;
- if (this.itemName == null) {
- if (other.itemName != null)
- return false;
- } else if (!this.itemName.equals(other.itemName))
- return false;
- if (this.itemPrice == null) {
- if (other.itemPrice != null)
- return false;
- } else if (!this.itemPrice.equals(other.itemPrice))
- return false;
- return true;
- }
-
private static final long serialVersionUID = 1L;
private String itemName;
@@ -90,8 +50,60 @@ public int getAmount() {
return this.amount;
}
- public EuroAmount getTotal() {
+ public EuroAmount total() {
return this.getItemPrice().times(this.amount);
}
+
+ public EuroAmount totalWithDiscount() {
+ return total().withDiscount(discount);
+ }
+
+ public BasketItem addAmount(int amount) {
+ return new BasketItem(itemName, itemPrice, articleId, amount + this.amount, discount);
+ }
+
+ public BasketItem updateDiscount(int discount) {
+ return new BasketItem(itemName, itemPrice, articleId, amount, discount);
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + this.amount;
+ result = prime * result + this.articleId;
+ result = prime * result + this.discount;
+ result = prime * result + ((this.itemName == null) ? 0 : this.itemName.hashCode());
+ result = prime * result + ((this.itemPrice == null) ? 0 : this.itemPrice.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (this.getClass() != obj.getClass())
+ return false;
+ final BasketItem other = (BasketItem) obj;
+ if (this.amount != other.amount)
+ return false;
+ if (this.articleId != other.articleId)
+ return false;
+ if (this.discount != other.discount)
+ return false;
+ if (this.itemName == null) {
+ if (other.itemName != null)
+ return false;
+ } else if (!this.itemName.equals(other.itemName))
+ return false;
+ if (this.itemPrice == null) {
+ if (other.itemPrice != null)
+ return false;
+ } else if (!this.itemPrice.equals(other.itemPrice))
+ return false;
+ return true;
+ }
}
View
2 src/main/java/org/fhw/asta/kasse/shared/common/EuroAmount.java
@@ -31,7 +31,7 @@ private EuroAmount(final int cents)
public int getCentAmount() {
return cents;
}
-
+
public EuroAmount plus(final EuroAmount summand)
{
return new EuroAmount(this.cents + summand.cents);

0 comments on commit 4a1a872

Please sign in to comment.
Something went wrong with that request. Please try again.