Advertisement
Tsuki11

Untitled

Jul 7th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. Main ----->
  2.  
  3. package E3_ShoppingSpree;
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.util.LinkedHashMap;
  9. import java.util.LinkedHashSet;
  10. import java.util.Map;
  11. import java.util.Set;
  12.  
  13. public class Main {
  14. public static void main(String[] args) throws IOException {
  15. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  16.  
  17. Map<String, Person> collectPeople = new LinkedHashMap<>();
  18. Map<String, Product> productsCollect = new LinkedHashMap<>();
  19.  
  20.  
  21. try {
  22. String[] people = reader.readLine().split(";");
  23. for (String person : people) {
  24.  
  25. String[] tokens = person.split("=");
  26. Person person1 = new Person(tokens[0], Double.parseDouble(tokens[1]));
  27. collectPeople.putIfAbsent(tokens[0], person1);
  28. }
  29.  
  30. String[] products = reader.readLine().split(";");
  31.  
  32. for (String product : products) {
  33. String[] tokens = product.split("=");
  34. Product product1 = new Product(tokens[0], Double.parseDouble(tokens[1]));
  35. productsCollect.putIfAbsent(tokens[0], product1);
  36. }
  37.  
  38. String line;
  39. while (!"end".equalsIgnoreCase(line = reader.readLine())) {
  40. String[] tokens = line.split("\\s+");
  41. String name = tokens[0];
  42. String productName = tokens[1];
  43. if (collectPeople.containsKey(name) && productsCollect.containsKey(productName)) {
  44. Person person = collectPeople.get(name);
  45. Product product = productsCollect.get(productName);
  46. person.buyProduct(product);
  47. }
  48. }
  49.  
  50.  
  51. } catch (IllegalArgumentException e) {
  52. System.out.println(e.getMessage());
  53. }
  54. collectPeople.values().forEach(System.out::println);
  55. }
  56. }
  57.  
  58.  
  59.  
  60. Person ----------->
  61.  
  62. package E3_ShoppingSpree;
  63.  
  64. import java.util.ArrayList;
  65. import java.util.List;
  66. import java.util.stream.Collectors;
  67.  
  68. public class Person implements ValidateData {
  69. private String name;
  70. private double money;
  71. private List<Product> products;
  72.  
  73. public Person(String name, double money) {
  74. this.setName(name);
  75. this.setMoney(money);
  76. this.products = new ArrayList<>();
  77. }
  78.  
  79. public String getName() {
  80. return this.name;
  81. }
  82.  
  83. public void setName(String name) {
  84. validateName(name);
  85. this.name = name;
  86. }
  87.  
  88.  
  89. public double getMoney() {
  90. return this.money;
  91. }
  92.  
  93. public void setMoney(double money) {
  94. validateNumber(money);
  95. this.money = money;
  96. }
  97.  
  98. public void buyProduct(Product product) {
  99. if (product == null) {
  100. throw new IllegalArgumentException();
  101. }
  102. if (this.getMoney() - product.getCost() < 0) {
  103. throw new IllegalArgumentException(String.format("%s can't afford %s", this.name, product.getName()));
  104. } else {
  105. this.products.add(product);
  106. this.setMoney(this.getMoney() - product.getCost());
  107. System.out.println(String.format("%s bought %s", this.name, product.getName()));
  108. }
  109.  
  110. }
  111.  
  112. @Override
  113. public String toString() {
  114.  
  115. if(this.products.isEmpty()){
  116. return String.format("%s – Nothing bought",this.getName());
  117. }
  118. return this.getName() + " - " +
  119. this.products
  120. .stream()
  121. .map(Product::getName)
  122. .collect(Collectors.joining(", "));
  123. }
  124.  
  125. @Override
  126. public void validateNumber(double num) {
  127. if (num < 0) {
  128. throw new IllegalArgumentException("Money cannot be negative");
  129. }
  130. }
  131.  
  132. @Override
  133. public void validateName(String name) {
  134. if (name == null || name.trim().isEmpty()) {
  135. throw new IllegalArgumentException("Name cannot be empty.");
  136. }
  137. }
  138. }
  139.  
  140. Product ------------->
  141.  
  142. package E3_ShoppingSpree;
  143.  
  144. public class Product implements ValidateData {
  145. private String name;
  146. private double cost;
  147.  
  148. public Product(String name, double cost) {
  149. this.setName(name);
  150. this.setCost(cost);
  151. }
  152.  
  153. public String getName() {
  154. return this.name;
  155. }
  156.  
  157. private void setName(String name) {
  158. validateName(name);
  159. this.name = name;
  160. }
  161.  
  162. public double getCost() {
  163.  
  164. return this.cost;
  165. }
  166.  
  167. private void setCost(double cost) {
  168. validateNumber(cost);
  169. this.cost = cost;
  170. }
  171.  
  172. @Override
  173. public void validateNumber(double num) {
  174. if (num < 0) {
  175. throw new IllegalArgumentException("Money cannot be negative");
  176. }
  177. }
  178.  
  179. @Override
  180. public void validateName(String name) {
  181. if (name == null || name.trim().isEmpty()) {
  182. throw new IllegalArgumentException("Name cannot be empty.");
  183. }
  184. }
  185. }
  186. Validation ------>
  187.  
  188.  
  189. package E3_ShoppingSpree;
  190.  
  191. public interface ValidateData {
  192.  
  193. public void validateNumber (double num);
  194. public void validateName (String name);
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement