Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. import Test.Person;
  2. import Test.Product;
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.util.LinkedHashMap;
  8.  
  9. public class Main {
  10. public static void main(String[] args) throws IOException {
  11. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  12. var persons = new LinkedHashMap<String, Person>();
  13. var products = new LinkedHashMap<String, Product>();
  14. String[] peopleInfo = reader.readLine().split(";");
  15. String[] productInfo = reader.readLine().split(";");
  16. for (String p : peopleInfo) {
  17. try {
  18. Person person = new Person(p.split("=")[0],
  19. Double.parseDouble(p.split("=")[1]));
  20. persons.putIfAbsent(person.getName(), person);
  21. } catch (IllegalArgumentException iae) {
  22. System.out.println(iae.getMessage());
  23. return;
  24. }
  25. }
  26.  
  27. for (String p : productInfo) {
  28. try {
  29. Product product = new Product(p.split("=")[0],
  30. Double.parseDouble(p.split("=")[1]));
  31. products.putIfAbsent(product.getName(), product);
  32. } catch (IllegalArgumentException iae) {
  33. System.out.println(iae.getMessage());
  34. return;
  35. }
  36. }
  37. String[] inputArgs = reader.readLine().split(" ");
  38. while (!"end".equalsIgnoreCase(inputArgs[0])) {
  39. String person = inputArgs[0];
  40. String product = inputArgs[1];
  41. try {
  42. if (persons.containsKey(person)) {
  43. persons.get(person).buyProduct(products.get(product));
  44. }
  45. } catch (IllegalStateException ise) {
  46. System.out.println(ise.getMessage());
  47. }
  48. inputArgs = reader.readLine().split(" ");
  49. }
  50. for (Person person : persons.values()) {
  51. StringBuilder sb = new StringBuilder();
  52. sb.append(person.getName()).append(" - ");
  53. if (person.getProducts().isEmpty()) {
  54. sb.append("Nothing bought");
  55. } else {
  56. String prefix = "";
  57. for (Product product : person.getProducts()) {
  58. sb.append(prefix);
  59. prefix = ", ";
  60. sb.append(product.getName());
  61. }
  62. }
  63. System.out.println(sb.toString());
  64. }
  65. }
  66. }
  67.  
  68. // Person Class
  69.  
  70. package Test;
  71.  
  72. import java.util.ArrayList;
  73. import java.util.List;
  74.  
  75. public class Person {
  76. private String name;
  77. private double money;
  78. private List<Product> products;
  79.  
  80. public Person(String name, double money) {
  81. this.setName(name);
  82. this.setMoney(money);
  83. this.products = new ArrayList<>();
  84. }
  85.  
  86. public String getName() {
  87. return name;
  88. }
  89.  
  90. public void setName(String name) {
  91. //IAM: VALIDATION
  92. if (!(name == null || name.trim().isEmpty())) {
  93. this.name = name;
  94. } else {
  95. throw new IllegalArgumentException("Name cannot be empty");
  96. }
  97. }
  98.  
  99. public double getMoney() {
  100. return money;
  101. }
  102.  
  103. public void setMoney(double money) {
  104. //IAM: VALIDATION
  105. if (money >= 0) {
  106. this.money = money;
  107. } else {
  108. throw new IllegalArgumentException("Money cannot be negative");
  109. }
  110. }
  111.  
  112. public List<Product> getProducts() {
  113. return products;
  114. }
  115.  
  116. public void setProducts(List<Product> products) {
  117. this.products = products;
  118. }
  119.  
  120. public void buyProduct(Product product) {
  121. if (this.money >= product.getCost()) {
  122. products.add(product);
  123. System.out.println(this.getName() + " bought " + product.getName());
  124. this.money -= product.getCost();
  125. } else {
  126. String MESSAGE = this.getName() + " can't afford " + product.getName();
  127. throw new IllegalStateException(MESSAGE);
  128. }
  129. }
  130. }
  131.  
  132. // Product class
  133.  
  134. package Test;
  135.  
  136. // 3 intervala ne sa 0 len
  137. // Money text is not cost
  138. // Linked Hash map - ordera
  139. // return sled catch
  140.  
  141. public class Product {
  142. private String name;
  143. private double cost;
  144.  
  145. public Product(String name, double cost) {
  146. this.setName(name);
  147. this.setCost(cost);
  148. }
  149.  
  150. public String getName() {
  151. return name;
  152. }
  153.  
  154. public void setName(String name) {
  155. //IAM: VALIDATION
  156. if (!(name == null || name.trim().isEmpty())) {
  157. this.name = name;
  158. } else {
  159. throw new IllegalArgumentException("Name cannot be empty");
  160. }
  161. }
  162.  
  163. public double getCost() {
  164. return cost;
  165. }
  166.  
  167. public void setCost(double money) {
  168. //IAM: VALIDATION
  169. if (money >= 0) {
  170. this.cost = money;
  171. } else {
  172. throw new IllegalArgumentException("Money cannot be negative");
  173. }
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement