TheRightGuy

Advice?

Apr 8th, 2022 (edited)
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.80 KB | None | 0 0
  1. import java.util.Map;
  2.  
  3. public class App {
  4.  
  5.     static Item item = new Item();
  6.  
  7.     public static void main(String[] args) {
  8.         shoppingCartItems();
  9.     }
  10.  
  11.     public static void shoppingCartItems() {
  12.         item.getShoppingCartItems();
  13.         System.out.println("There are " + Supermarket.getPriceByProduct().size() + " items in the cart.\nWhich is a total of $" + Supermarket.getProductSum() + " dollars.");
  14.     }
  15.  
  16.     static boolean isPickedForPriceChange(Map.Entry<String, Integer> priceByProduct) {
  17.         return item.getPrice() == priceByProduct.getValue();
  18.     }
  19.  
  20.     static void printProducts(Map.Entry<String, Integer> shoppingCartElement) {
  21.         System.out.println(shoppingCartElement.getKey() + ":" + shoppingCartElement.getValue());
  22.     }
  23.  
  24.     static void priceIncreaseText(Map.Entry<String, Integer> shoppingCartItem) {
  25.         System.out.println("There is a price increase on " + shoppingCartItem.getKey() + " which price has increased from " + shoppingCartItem.getValue() + " to "
  26.                 + (int) (shoppingCartItem.getValue() * 1.1));
  27.     }
  28.  
  29.     static void priceDiscountText(Map.Entry<String, Integer> shoppingCartItem) {
  30.         System.out.println("There is a discount on " + shoppingCartItem.getKey() + " which price has dropped from " + shoppingCartItem.getValue() + " to "
  31.                 + (int) (shoppingCartItem.getValue() * 0.7));
  32.     }
  33.  
  34.     static boolean isProductOnDiscount() {
  35.         return Math.random() < 0.8;
  36.     }
  37.  
  38.     static boolean isProductPriceIncrease() {
  39.         return Math.random() < 0.3;
  40.     }
  41.  
  42.     static int getPriceIncrease(Map.Entry<String, Integer> shoppingCartItem) {
  43.         return (int) (shoppingCartItem.getValue() * 1.1);
  44.     }
  45.  
  46.     static int getDiscount(Map.Entry<String, Integer> shoppingCartItem) {
  47.         return (int) (shoppingCartItem.getValue() * 0.7);
  48.     }
  49. }
  50.  
  51. ----------------------
  52. import java.util.*;
  53.  
  54. public class Supermarket extends App{
  55.  
  56.     private static final Map<String, Integer> priceByProducts = new HashMap<>();
  57.  
  58.     public static Map<String, Integer> getPriceByProduct() {
  59.         return priceByProducts;
  60.     }
  61.  
  62.     static int getProductSum() {
  63.         int sum = 0;
  64.         int sumDiscount = 0;
  65.         int sumPriceIncrease = 0;
  66.   priceByProducts.entrySet().forEach(App::printProducts);
  67.  
  68.         sumDiscount += getPriceByProduct().entrySet().stream().filter
  69.                 (priceByProduct -> isProductOnDiscount() && isPickedForPriceChange(priceByProduct)).map(App::getDiscount).reduce(0, Integer::sum);
  70.  
  71.         priceByProducts.entrySet().stream().filter(priceByProduct -> isProductOnDiscount() && isPickedForPriceChange(priceByProduct)).forEach(App::priceDiscountText);
  72.  
  73.  
  74.         sumPriceIncrease += getPriceByProduct().entrySet().stream().filter
  75.                 (priceByProduct -> isProductPriceIncrease() && isPickedForPriceChange(priceByProduct)).map(App::getPriceIncrease).reduce(0, Integer::sum);
  76.  
  77.         priceByProducts.entrySet().stream().filter(priceByProduct -> isProductPriceIncrease() && isPickedForPriceChange(priceByProduct)).forEach(App::priceIncreaseText);
  78.        
  79.         return (priceByProducts.values().stream().reduce(0, Integer::sum) + sumDiscount + sumPriceIncrease);
  80. --------------
  81.         for (Map.Entry<String, Integer> priceByProduct : Supermarket.getPriceByProduct().entrySet()) {
  82.             printProducts(priceByProduct);
  83.             if (isProductOnDiscount() && isPickedForPriceChange(priceByProduct)) {
  84.                 priceDiscountText(priceByProduct);
  85.                 sum += Supermarket.getDiscount(priceByProduct);
  86.             } else if (Supermarket.isProductPriceIncrease() && isPickedForPriceChange(priceByProduct)) {
  87.                 priceIncreaseText(priceByProduct);
  88.                 sum += Supermarket.getPriceIncrease(priceByProduct);
  89.             } else {
  90.                 sum += priceByProduct.getValue();
  91.             }
  92.         }
  93.         return sum;
  94.     }
  95.  
  96. // for each sollution or stream
  97.         for (Map.Entry<String, Integer> priceByProduct : Supermarket.getPriceByProduct().entrySet()) {
  98.             printProducts(priceByProduct);
  99.             if (isProductOnDiscount() && isPickedForPriceChange(priceByProduct)) {
  100.                 priceDiscountText(priceByProduct);
  101.                 sum += Supermarket.getDiscount(priceByProduct);
  102.             } else if (Supermarket.isProductPriceIncrease() && isPickedForPriceChange(priceByProduct)) {
  103.                 priceIncreaseText(priceByProduct);
  104.                 sum += Supermarket.getPriceIncrease(priceByProduct);
  105.             } else {
  106.                 sum += priceByProduct.getValue();
  107.             }
  108.         }
  109.         return sum;
  110.     }
  111. }
  112.  
  113. ------------------------------
  114. import java.io.BufferedReader;
  115. import java.io.File;
  116. import java.io.FileReader;
  117. import java.io.IOException;
  118. import java.util.Random;
  119.  
  120. public class Item {
  121.     private final String name;
  122.  
  123.     public Item(String name) {
  124.         this.name = name;
  125.     }
  126.  
  127.     public Item() {
  128.         name = "";
  129.     }
  130.  
  131.     public String getName() {
  132.         return name;
  133.     }
  134.  
  135.     public int getPrice() {
  136.         int MAX_PRODUCT_PRICE = 100;
  137.         return new Random().nextInt(1, MAX_PRODUCT_PRICE);
  138.     }
  139.  
  140.      void getShoppingCartItems() {
  141.         try (BufferedReader br = getBufferedReader()) {
  142.             for (String name; (name = br.readLine()) != null; ) {
  143.                 new Item(name).addItems();
  144.             }
  145.         } catch (IOException e) {
  146.             System.out.println(e.getCause().getMessage());
  147.         }
  148.     }
  149.  
  150.     private BufferedReader getBufferedReader() throws IOException {
  151.         File file = new File("src/main/resources/groceriesList.txt");
  152.         return new BufferedReader(new FileReader(file));
  153.     }
  154.  
  155.     private void addItems() {
  156.         Supermarket.getPriceByProduct().putIfAbsent(getName(), getPrice());
  157.     }
  158.  
  159. }
  160.  
Add Comment
Please, Sign In to add comment