Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Map;
- public class App {
- static Item item = new Item();
- public static void main(String[] args) {
- shoppingCartItems();
- }
- public static void shoppingCartItems() {
- item.getShoppingCartItems();
- System.out.println("There are " + Supermarket.getPriceByProduct().size() + " items in the cart.\nWhich is a total of $" + Supermarket.getProductSum() + " dollars.");
- }
- static boolean isPickedForPriceChange(Map.Entry<String, Integer> priceByProduct) {
- return item.getPrice() == priceByProduct.getValue();
- }
- static void printProducts(Map.Entry<String, Integer> shoppingCartElement) {
- System.out.println(shoppingCartElement.getKey() + ":" + shoppingCartElement.getValue());
- }
- static void priceIncreaseText(Map.Entry<String, Integer> shoppingCartItem) {
- System.out.println("There is a price increase on " + shoppingCartItem.getKey() + " which price has increased from " + shoppingCartItem.getValue() + " to "
- + (int) (shoppingCartItem.getValue() * 1.1));
- }
- static void priceDiscountText(Map.Entry<String, Integer> shoppingCartItem) {
- System.out.println("There is a discount on " + shoppingCartItem.getKey() + " which price has dropped from " + shoppingCartItem.getValue() + " to "
- + (int) (shoppingCartItem.getValue() * 0.7));
- }
- static boolean isProductOnDiscount() {
- return Math.random() < 0.8;
- }
- static boolean isProductPriceIncrease() {
- return Math.random() < 0.3;
- }
- static int getPriceIncrease(Map.Entry<String, Integer> shoppingCartItem) {
- return (int) (shoppingCartItem.getValue() * 1.1);
- }
- static int getDiscount(Map.Entry<String, Integer> shoppingCartItem) {
- return (int) (shoppingCartItem.getValue() * 0.7);
- }
- }
- ----------------------
- import java.util.*;
- public class Supermarket extends App{
- private static final Map<String, Integer> priceByProducts = new HashMap<>();
- public static Map<String, Integer> getPriceByProduct() {
- return priceByProducts;
- }
- static int getProductSum() {
- int sum = 0;
- int sumDiscount = 0;
- int sumPriceIncrease = 0;
- priceByProducts.entrySet().forEach(App::printProducts);
- sumDiscount += getPriceByProduct().entrySet().stream().filter
- (priceByProduct -> isProductOnDiscount() && isPickedForPriceChange(priceByProduct)).map(App::getDiscount).reduce(0, Integer::sum);
- priceByProducts.entrySet().stream().filter(priceByProduct -> isProductOnDiscount() && isPickedForPriceChange(priceByProduct)).forEach(App::priceDiscountText);
- sumPriceIncrease += getPriceByProduct().entrySet().stream().filter
- (priceByProduct -> isProductPriceIncrease() && isPickedForPriceChange(priceByProduct)).map(App::getPriceIncrease).reduce(0, Integer::sum);
- priceByProducts.entrySet().stream().filter(priceByProduct -> isProductPriceIncrease() && isPickedForPriceChange(priceByProduct)).forEach(App::priceIncreaseText);
- return (priceByProducts.values().stream().reduce(0, Integer::sum) + sumDiscount + sumPriceIncrease);
- --------------
- for (Map.Entry<String, Integer> priceByProduct : Supermarket.getPriceByProduct().entrySet()) {
- printProducts(priceByProduct);
- if (isProductOnDiscount() && isPickedForPriceChange(priceByProduct)) {
- priceDiscountText(priceByProduct);
- sum += Supermarket.getDiscount(priceByProduct);
- } else if (Supermarket.isProductPriceIncrease() && isPickedForPriceChange(priceByProduct)) {
- priceIncreaseText(priceByProduct);
- sum += Supermarket.getPriceIncrease(priceByProduct);
- } else {
- sum += priceByProduct.getValue();
- }
- }
- return sum;
- }
- // for each sollution or stream
- for (Map.Entry<String, Integer> priceByProduct : Supermarket.getPriceByProduct().entrySet()) {
- printProducts(priceByProduct);
- if (isProductOnDiscount() && isPickedForPriceChange(priceByProduct)) {
- priceDiscountText(priceByProduct);
- sum += Supermarket.getDiscount(priceByProduct);
- } else if (Supermarket.isProductPriceIncrease() && isPickedForPriceChange(priceByProduct)) {
- priceIncreaseText(priceByProduct);
- sum += Supermarket.getPriceIncrease(priceByProduct);
- } else {
- sum += priceByProduct.getValue();
- }
- }
- return sum;
- }
- }
- ------------------------------
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.Random;
- public class Item {
- private final String name;
- public Item(String name) {
- this.name = name;
- }
- public Item() {
- name = "";
- }
- public String getName() {
- return name;
- }
- public int getPrice() {
- int MAX_PRODUCT_PRICE = 100;
- return new Random().nextInt(1, MAX_PRODUCT_PRICE);
- }
- void getShoppingCartItems() {
- try (BufferedReader br = getBufferedReader()) {
- for (String name; (name = br.readLine()) != null; ) {
- new Item(name).addItems();
- }
- } catch (IOException e) {
- System.out.println(e.getCause().getMessage());
- }
- }
- private BufferedReader getBufferedReader() throws IOException {
- File file = new File("src/main/resources/groceriesList.txt");
- return new BufferedReader(new FileReader(file));
- }
- private void addItems() {
- Supermarket.getPriceByProduct().putIfAbsent(getName(), getPrice());
- }
- }
Add Comment
Please, Sign In to add comment