-
Notifications
You must be signed in to change notification settings - Fork 7
[Java] Add BasketInformations #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package io.shodo; | ||
|
|
||
| import java.util.HashMap; | ||
|
|
||
| /** | ||
| * Created by thomas on 02/12/2019. | ||
| * This class is a basket | ||
| */ | ||
| public class BasketInformations { | ||
|
|
||
| // The product of the basket | ||
| static HashMap<String, Integer> map = new HashMap<String, Integer>(); | ||
|
|
||
| // The fact that the basket has promo code | ||
| private static boolean codeDePromotion = false; | ||
|
|
||
| public void addProductToBasket(String product, Integer price, boolean isPromoCode) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. price better be double to accomodate decimals. |
||
| if (isPromoCode) { | ||
| codeDePromotion = true; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we convey to callers that adding to basket failed or was not done? |
||
| } else { | ||
| map.put(product, price); | ||
| } | ||
| } | ||
|
|
||
| public Long getBasketPrice(boolean inCents) { | ||
| Integer v = 0; | ||
| for (String s : map.keySet()) { | ||
| v += map.get(s); | ||
| } | ||
| if (codeDePromotion) { | ||
| v -= 100; | ||
| } | ||
| return inCents ? v * 100 : Long.valueOf(v); | ||
| } | ||
|
|
||
| public void resetBasket() { | ||
| buyBasket(); | ||
| codeDePromotion = false; | ||
| } | ||
|
|
||
| public void buyBasket() { | ||
| map.clear(); | ||
| } | ||
|
|
||
| public boolean isBasketContains(String produit) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. spell check, produit -> product? |
||
| boolean found = false; | ||
| for (String s : map.keySet()) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not directly do map,get ? |
||
| if (s == produit) found = true; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return true if found, dont need to continue the loop. |
||
| } | ||
| return found; | ||
| } | ||
|
|
||
| public void mixWithBasket(BasketInformations b) { | ||
| map.putAll(b.map); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naming can be better, what is this a map of?