Advertisement
TheRightGuy

Advice?

Feb 17th, 2022
709
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.19 KB | None | 0 0
  1. public class App {
  2.  
  3.     public static void main(String[] args) {
  4.         Supermarket supermarket = new Supermarket();
  5.         supermarket.start();
  6.     }
  7. }
  8.  
  9. public class Supermarket {
  10.     private final Map<String, Integer> shoppingCartItems = new HashMap<>();
  11.  
  12.     public void start() {
  13.         getValuesFromFile();
  14.         System.out.println("There are " + shoppingCartItems.size() + " items in the cart.");
  15.         int sum = 0;
  16.         Object randomValue = getRandomItem();
  17.         sum = getPriceSum(sum, randomValue);
  18.         System.out.println("Which is a total of $" + sum + " dollars.");
  19.     }
  20.  
  21.     private int getPriceSum(int sum, Object randomValue) {
  22.         for (Map.Entry<String, Integer> entry : shoppingCartItems.entrySet()) {
  23.             System.out.println(entry.getKey() + ":" + entry.getValue());
  24.             if (isOnDiscount() && randomValue == entry.getValue()) {
  25.                 sum = getDiscount(sum, entry);
  26.             } else if (isPriceIncrease() && randomValue == entry.getValue()) {
  27.                 sum = getPriceIncrease(sum, entry);
  28.             } else {
  29.                 sum += entry.getValue();
  30.             }
  31.         }
  32.         return sum;
  33.     }
  34.  
  35.     private int getPriceIncrease(int sum, Map.Entry<String, Integer> entry) {
  36.         System.out.println("There is a price increase on " + entry.getKey() + " which price has increased from " + entry.getValue() + " to "
  37.                 + (int) (entry.getValue() * 1.1));
  38.         sum += entry.getValue() * 1.1;
  39.         return sum;
  40.     }
  41.  
  42.     private int getDiscount(int sum, Map.Entry<String, Integer> entry) {
  43.         System.out.println("There is a discount on " + entry.getKey() + " which price has dropped from " + entry.getValue() + " to "
  44.                 + (int) (entry.getValue() * 0.7));
  45.         sum += entry.getValue() * 0.7;
  46.         return sum;
  47.     }
  48.  
  49.     private Object getRandomItem() {
  50.         Random generator = new Random();
  51.         Object[] values = shoppingCartItems.values().toArray();
  52.         return values[generator.nextInt(values.length)];
  53.     }
  54.  
  55.     private void getValuesFromFile() {
  56.         try {
  57.             BufferedReader bufferedReader = getBufferedReader();
  58.             addToShoppingCart(bufferedReader);
  59.         } catch (IOException e) {
  60.             throw new FileSystemNotFoundException("The file could not be found, check the file or adjust the location");
  61.         }
  62.     }
  63.  
  64.     private BufferedReader getBufferedReader() throws FileNotFoundException {
  65.         File file = new File("src/main/resources/groceriesList.txt");
  66.         FileReader fileReader = new FileReader(file);
  67.         return new BufferedReader(fileReader);
  68.     }
  69.  
  70.     private void addToShoppingCart(BufferedReader bufferedReader) throws IOException {
  71.         String line;
  72.         int MAX_LINE = 96;
  73.         for (int i = 0; i < MAX_LINE ; i++) {
  74.             line = bufferedReader.readLine().toLowerCase(Locale.ROOT);
  75.             shoppingCartItems.putIfAbsent(line,(int) (Math.random()*100 +1));
  76.         }
  77.     }
  78.  
  79.     private boolean isOnDiscount() {
  80.         double d = Math.random();
  81.         return d < 0.5;
  82.     }
  83.  
  84.     private boolean isPriceIncrease() {
  85.         double d = Math.random();
  86.         return d < 0.2;
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement