Ivelin_1936

something

Jun 19th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.47 KB | None | 0 0
  1. package test;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.util.LinkedHashMap;
  6. import java.util.Map;
  7.  
  8. public class Main {
  9.     public static void main(String[] args) throws Exception {
  10.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  11.  
  12.         Map<String, Person> people = new LinkedHashMap<>();
  13.         Map<String, Product> products = new LinkedHashMap<>();
  14.  
  15.         try {
  16.             createAndAddPeople(reader, people);
  17.             createAndAddProducts(reader, products);
  18.  
  19.             String inLine = reader.readLine();
  20.             while (!"END".equals(inLine)) {
  21.                 String[] inTokens = inLine.split("\\s+");
  22.                 String personName = inTokens[0];
  23.                 String productName = inTokens[1];
  24.  
  25.                 if (people.containsKey(personName)) {
  26.                     if (products.containsKey(productName)) {
  27.  
  28.                         Person person = people.get(personName);
  29.                         Product product = products.get(productName);
  30.  
  31.                         try {
  32.                             person.buyProduct(product);
  33.                         } catch (IllegalArgumentException ex) {
  34.                             System.out.println(ex);
  35.                         }
  36.  
  37.                     }
  38.                 }
  39.  
  40.                 inLine = reader.readLine();
  41.             }
  42.  
  43.             for (Map.Entry<String, Person> map : people.entrySet()) {
  44.                 Person person = map.getValue();
  45.                 if (person.getProducts().size() > 0) {
  46.                     System.out.println(person);
  47.                 } else {
  48.                     System.out.printf("%s - Nothing bought", person.getName());
  49.                 }
  50.             }
  51.  
  52.         } catch (IllegalArgumentException iae) {
  53.             System.out.println(iae.getMessage());
  54.         }
  55.  
  56.  
  57.     }
  58.  
  59.     private static void createAndAddProducts(BufferedReader reader, Map<String, Product> products) throws Exception {
  60.  
  61.         String[] tokens = reader.readLine().split(";");
  62.         for (int i = 0; i < tokens.length; i++) {
  63.             Product product = null;
  64.             String[] inPersonTokens = tokens[i].split("=");
  65.  
  66.             String name = inPersonTokens[0];
  67.             double price = Double.parseDouble(inPersonTokens[1]);
  68.             product = new Product(name, price);
  69.  
  70.             products.put(name, product);
  71.         }
  72.     }
  73.  
  74.     private static void createAndAddPeople(BufferedReader reader, Map<String, Person> people) throws Exception {
  75.         String[] tokens = reader.readLine().split(";");
  76.  
  77.         for (int i = 0; i < tokens.length; i++) {
  78.             Person person = null;
  79.             String[] inPersonTokens = tokens[i].split("=");
  80.             String name = inPersonTokens[0];
  81.             double money = Double.parseDouble(inPersonTokens[1]);
  82.  
  83.             person = new Person(name, money);
  84.             people.put(name, person);
  85.  
  86.         }
  87.     }
  88. }
  89.  
  90. package test;
  91.  
  92. import java.util.ArrayList;
  93. import java.util.List;
  94.  
  95. public class Person {
  96.     private String name;
  97.     private double money; //second change money isnt int
  98.     private List<Product> products;
  99.  
  100.     public Person(String name, double money) {
  101.         setName(name);
  102.         setMoney(money);
  103.         this.products = new ArrayList<>();
  104.     }
  105.  
  106.     private void setName(String name) {
  107.         if (name == null || name.trim().isEmpty()) { //3th change
  108.             throw new IllegalArgumentException("Name cannot be empty");
  109.         }
  110.         this.name = name;
  111.     }
  112.  
  113.     public String getName() {
  114.         return this.name;
  115.     }
  116.  
  117.     private void setMoney(double money) {
  118.         if (money < 0) {
  119.             throw new IllegalArgumentException("Money cannot be negative");
  120.         } else {
  121.             this.money = money;
  122.         }
  123.     }
  124.  
  125.     public double getMoney() {
  126.         return this.money;
  127.     }
  128.  
  129.     public List<Product> getProducts() {
  130.         return this.products;
  131.     }
  132.  
  133.     public void buyProduct(Product product) {
  134.  
  135.         if (this.getMoney() < product.getPrice()) {
  136.             System.out.printf("%s can't afford %s%n", this.getName(), product.getName());
  137.         } else {
  138.             this.getProducts().add(product);
  139.             this.money -= product.getPrice();
  140.             System.out.println(this.getName() + " bought " + product.getName());
  141.         }
  142.     }
  143.  
  144.     @Override
  145.     public String toString() {
  146.         StringBuilder stringBuilder = new StringBuilder();
  147.         stringBuilder.append(this.name);
  148.         stringBuilder.append(" - ");
  149.         stringBuilder.append(String.join(", ", this.getProducts().stream()
  150.                 .map(p -> p.getName()).toArray(String[]::new)));
  151.  
  152.         return stringBuilder.toString();
  153.     }
  154. }
  155.  
  156. package test;
  157.  
  158. public class Product {
  159.     private String name;
  160.     private double price;
  161.  
  162.     public Product(String name, double price) {
  163.         setName(name);
  164.         setPrice(price);
  165.     }
  166.  
  167.     private void setName(String name) {
  168.         if (name == null || name.trim().isEmpty()) {  // first change
  169.             throw new IllegalArgumentException("Name cannot be empty");
  170.         }
  171.         this.name = name;
  172.     }
  173.  
  174.     public String getName() {
  175.         return this.name;
  176.     }
  177.  
  178.     private void setPrice(double price) {
  179.         if (price < 0) {
  180.             throw new IllegalArgumentException("Money cannot be negative");
  181.         }
  182.         this.price = price;
  183.     }
  184.  
  185.     public double getPrice() {
  186.         return this.price;
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment