Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.LinkedHashMap;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. public class Main {
  9.  
  10. public static void main(String[] args) throws IOException {
  11.  
  12. BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
  13.  
  14. LinkedHashMap<String, Person> people = new LinkedHashMap<>();
  15. LinkedHashMap<String, Product> products = new LinkedHashMap<>();
  16.  
  17. try {
  18. String[] clients = console.readLine().split(";");
  19. for (int i = 0; i < clients.length; i++) {
  20. String[] nameMoney = clients[i].split("=");
  21. String name = nameMoney[0];
  22. double money = Double.parseDouble(nameMoney[1]);
  23.  
  24. Person person = new Person(name, money);
  25. people.put(name, person);
  26.  
  27. }
  28.  
  29. String[] consumables = console.readLine().split(";");
  30. for (int i = 0; i < consumables.length; i++) {
  31. String[] productCost = consumables[i].split("=");
  32. String product = productCost[0];
  33. double cost = Double.parseDouble(productCost[1]);
  34. Product pro = new Product(product, cost);
  35. products.put(product, pro);
  36. }
  37.  
  38. while (true) {
  39. String[] input = console.readLine().split("\\s+");
  40. if (input[0].equals("END")) {
  41. break;
  42. }
  43.  
  44. String client = input[0];
  45. String product = input[1];
  46.  
  47. Person person = people.get(client);
  48. Product pro = products.get(product);
  49. person.buyProduct(pro);
  50.  
  51. }
  52.  
  53. for (Person person : people.values()) {
  54. if (person.getProducts().isEmpty()){
  55. System.out.printf("%s - Nothing bought%n", person.getName());
  56. } else {
  57. System.out.printf("%s - %s%n",
  58. person.getName(),
  59. String.join(", ", person.getProducts()));
  60. }
  61. }
  62. } catch (IllegalArgumentException ex) {
  63. System.out.println(ex.getMessage());
  64. }
  65. }
  66. }
  67. class Person {
  68.  
  69. private String name;
  70. private double money;
  71. private List<String> 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 List<String> getProducts() {
  80. return products;
  81. }
  82.  
  83. protected String getName() {
  84. return name;
  85. }
  86.  
  87. private void setName(String name) {
  88. if (name.isEmpty() || name.contentEquals(" ")) {
  89. throw new IllegalArgumentException("Name cannot be empty");
  90. }
  91. this.name = name;
  92. }
  93.  
  94. private double getMoney() {
  95. return money;
  96. }
  97.  
  98. private void setMoney(double money) {
  99. if (money < 0) {
  100. throw new IllegalArgumentException("Money cannot be negative");
  101. }
  102. this.money = money;
  103. }
  104.  
  105.  
  106. public void buyProduct(Product product) {
  107. if (this.getMoney() >= product.getCost()) {
  108. this.products.add(product.getName());
  109. this.money -= product.getCost();
  110. boughtProduct(product);
  111. }else {
  112. noMoney(product);
  113. }
  114. }
  115.  
  116. private void boughtProduct(Product product) {
  117. System.out.printf("%s bought %s%n", this.name, product.getName());
  118. }
  119.  
  120. private void noMoney(Product product){
  121. System.out.printf("%s can't afford %s%n", this.getName(), product.getName());
  122. }
  123. }
  124. class Product {
  125.  
  126. private String name;
  127. private double cost;
  128.  
  129. public Product(String name, double cost) {
  130. this.setName(name);
  131. this.setCost(cost);
  132. }
  133.  
  134. public String getName() {
  135. return name;
  136. }
  137.  
  138. public void setName(String name) {
  139. if (name.isEmpty() || name.contentEquals(" ")){
  140. throw new IllegalArgumentException("Name cannot be empty");
  141. }
  142. this.name = name;
  143. }
  144.  
  145. public double getCost() {
  146. return cost;
  147. }
  148.  
  149. public void setCost(double cost) {
  150. if (cost <= 0){
  151. throw new IllegalArgumentException("Money cannot be negative");
  152. }
  153. this.cost = cost;
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement