238025681

Shopping Spree

Jul 5th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.35 KB | None | 0 0
  1. package Prob4ShoppingSpree;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9.  
  10. /**
  11.  *
  12.  * @author kalin
  13.  */
  14. public class Prob4ShoppingSpree {
  15.  
  16.     //([A-Za-z]+)\s*=\s*([0-9]+)
  17.     public static void main(String[] args) {
  18.         try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));) {
  19.             String[] input = reader.readLine().trim().split(";");
  20.             Pattern patt = Pattern.compile("([A-Za-z]+)\\s*=\\s*([-]*[0-9]+)");
  21.             List<Person> persons = new ArrayList<>();
  22.             List<Product> products = new ArrayList<>();
  23.  
  24.             for (int i = 0; i < input.length; i++) {
  25.                 Matcher match = patt.matcher(input[i]);
  26.                 while (match.find()) {
  27.                     persons.add(new Person(match.group(1), Integer.parseInt(match.group(2))));
  28.                 }
  29.             }
  30.  
  31.             input = reader.readLine().trim().split(";");
  32.             for (int i = 0; i < input.length; i++) {
  33.                 Matcher match = patt.matcher(input[i]);
  34.                 while (match.find()) {
  35.                     products.add(new Product(match.group(1), Integer.parseInt(match.group(2))));
  36.                 }
  37.             }
  38.  
  39.             input = reader.readLine().trim().split(" ");
  40.             while (!input[0].equals("END")) {
  41.                 String personName = input[0];
  42.                 String productName = input[1];
  43.                 Person curentPerson = persons.stream().filter(p -> p.getName().equals(personName)).findFirst().orElse(null);
  44.                 Product curentProduct = products.stream().filter(p -> p.getName().equals(productName)).findFirst().orElse(null);
  45.                 curentPerson.boughtProduct(curentProduct);
  46.  
  47.                 input = reader.readLine().trim().split(" ");
  48.             }
  49.             persons.forEach(p ->
  50.             {System.out.printf("%s - %s%n", p.getName(), p.getBagOfProducts().size() > 0 ? p.getBagOfProducts().toString().replace("[", "").replace("]", "") : "Nothing bought");});
  51.            
  52.         } catch (IllegalArgumentException iae) {
  53.             System.out.println(iae.getMessage());
  54.         } catch (Exception e) {
  55.             e.printStackTrace();
  56.         }
  57.     }
  58.  
  59. }
  60.  
  61. class Person {
  62.  
  63.     private String name;
  64.     private int money;
  65.     private ArrayList<String> bagOfProducts;
  66.  
  67.     public Person(String name, int money) {
  68.         this.setName(name);
  69.         this.setMoney(money);
  70.         this.bagOfProducts = new ArrayList<>();
  71.     }
  72.  
  73.     private void setName(String name) {
  74.         if (name.isEmpty() || name.trim().length() == 0) {
  75.             throw new IllegalArgumentException("Name cannot be empty");
  76.         }
  77.         this.name = name;
  78.     }
  79.  
  80.     private void setMoney(int money) {
  81.         if (money < 0) {
  82.             throw new IllegalArgumentException("Money cannot be negative");
  83.         }
  84.         this.money = money;
  85.     }
  86.  
  87.     public String getName() {
  88.         return name;
  89.     }
  90.  
  91.     public void boughtProduct(Product product) {
  92.         if ((this.money - product.getCost()) < 0) {
  93.             System.out.printf("%s can't afford %s%n", this.name, product.getName());
  94.         } else {
  95.             this.money -= product.getCost();
  96.             this.bagOfProducts.add(product.getName());
  97.             System.out.printf("%s bought %s%n", this.name, product.getName());
  98.         }
  99.  
  100.     }
  101.  
  102.     public ArrayList<String> getBagOfProducts() {
  103.         return bagOfProducts;
  104.     }
  105.  
  106.     @Override
  107.     public String toString() {
  108.         return "Person{" + "name=" + name + '}';
  109.     }
  110.  
  111. }
  112.  
  113. class Product {
  114.  
  115.     private String name;
  116.     private int cost;
  117.  
  118.     public Product(String name, int cost) {
  119.         this.setName(name);
  120.         this.setCost(cost);
  121.     }
  122.  
  123.     private void setName(String name) {
  124.         if (name.isEmpty() || name.trim().length() == 0) {
  125.             throw new IllegalArgumentException("Name cannot be empty");
  126.         }
  127.         this.name = name;
  128.     }
  129.  
  130.     private void setCost(int cost) {
  131.         if (cost < 0) {
  132.             throw new IllegalArgumentException("Money cannot be negative");
  133.         }
  134.         this.cost = cost;
  135.     }
  136.  
  137.     public String getName() {
  138.         return name;
  139.     }
  140.  
  141.     public int getCost() {
  142.         return cost;
  143.     }
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment