Advertisement
IvaAnd

ShoppingSpree

Nov 8th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. package ShoppingSpree;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Scanner;
  7.  
  8. public class Main {
  9.  
  10.  
  11. public static void main(String[] args) {
  12.  
  13. Scanner scanner = new Scanner(System.in);
  14.  
  15.  
  16. List<Person> people = new ArrayList<>();
  17. List<Product> products = new ArrayList<>();
  18.  
  19.  
  20. String[] tokens = scanner.nextLine().split(";");
  21. try {
  22. getPerson(people, tokens);
  23.  
  24. tokens = scanner.nextLine().split(";");
  25.  
  26. getProducts(products, tokens);
  27.  
  28. }catch (IllegalArgumentException exception){
  29. System.out.println(exception.getMessage());
  30. }
  31.  
  32.  
  33. tokens = scanner.nextLine().split("\\s+");
  34.  
  35. buyProducts(scanner, people, products, tokens);
  36.  
  37. for (Person person : people) {
  38. System.out.println(person.toString());
  39. }
  40.  
  41.  
  42. }
  43.  
  44. private static void buyProducts(Scanner scanner, List<Person> people, List<Product> products, String[] tokens) {
  45. while (!tokens[0].equals("END")) {
  46.  
  47. String name = tokens[0];
  48. String productName = tokens[1];
  49.  
  50.  
  51. try {
  52. for (Person currentPerson : people) {
  53. if (currentPerson.getName().equals(name)) {
  54. for (Product currProduct : products) {
  55. if (currProduct.getName().equals(productName)) {
  56. currentPerson.buyProduct(currProduct);
  57. System.out.println(currentPerson.getName() + " bought " + currProduct.getName());
  58. }
  59. }
  60. }
  61. }
  62.  
  63. } catch (IllegalArgumentException ex) {
  64. System.out.println(ex.getMessage());
  65. }
  66.  
  67. tokens = scanner.nextLine().split("\\s+");
  68. }
  69. }
  70.  
  71. private static void getProducts(List<Product> products, String[] tokens) {
  72. for (String token : tokens) {
  73. String[] element = token.split("=");
  74. Product product = new Product(element[0], Double.parseDouble(element[1]));
  75. products.add(product);
  76. }
  77. }
  78.  
  79. private static void getPerson(List<Person> people, String[] tokens) {
  80. for (String token : tokens) {
  81. String[] element = token.split("=");
  82. Person person = new Person(element[0], Double.parseDouble(element[1]));
  83. people.add(person);
  84. }
  85. }
  86.  
  87. private static void buyProduct(Map<String, Person> people, Map<String, Product> products, String person, String product) {
  88. try {
  89. people.get(person).buyProduct(products.get(product));
  90. System.out.println(person + " bought " + product);
  91. } catch (IllegalArgumentException ex) {
  92. System.out.println(ex.getMessage());
  93. }
  94. }
  95. }
  96.  
  97.  
  98.  
  99.  
  100. package ShoppingSpree;
  101.  
  102. import java.util.ArrayList;
  103. import java.util.List;
  104. import java.util.stream.Collectors;
  105.  
  106. public class Person {
  107. private String name;
  108. private double money;
  109. private List<Product> products;
  110.  
  111.  
  112. public Person(String name, double money) {
  113. this.setName(name);
  114. this.setMoney(money);
  115. this.products = new ArrayList<>();
  116. }
  117.  
  118.  
  119. private void setName(String name) {
  120. Validator.nameIsValid(name);
  121. this.name = name;
  122.  
  123.  
  124. }
  125.  
  126. private void setMoney(double money) {
  127. Validator.moneyAreValid(money);
  128. this.money = money;
  129. }
  130.  
  131. public String getName() {
  132. return this.name;
  133. }
  134.  
  135. public void buyProduct(Product product) {
  136. if (product.getCost() > this.money) {
  137. throw new IllegalArgumentException(name
  138. + " can't afford " + product.getName());
  139. }
  140. this.products.add(product);
  141. this.money -= product.getCost();
  142. }
  143.  
  144. @Override
  145. public String toString() {
  146. String output = products.isEmpty() ? "can't afford Kafence"
  147. : this.products.stream()
  148. .map(Product::getName)
  149. .collect(Collectors.joining(", "));
  150.  
  151. return this.name + " - " + output;
  152.  
  153. }
  154. }
  155.  
  156.  
  157. package ShoppingSpree;
  158.  
  159. import java.util.LinkedHashMap;
  160. import java.util.Map;
  161.  
  162. public class Product {
  163.  
  164. private String name;
  165. private double cost;
  166.  
  167.  
  168.  
  169. public Product(String name, double cost) {
  170. this.setName(name);
  171. this.setCost(cost);
  172. }
  173.  
  174.  
  175. private void setName(String name) {
  176. Validator.nameIsValid(name);
  177. this.name = name;
  178. }
  179.  
  180. private void setCost(double cost) {
  181. Validator.moneyAreValid(cost);
  182. this.cost = cost;
  183.  
  184. }
  185.  
  186. public String getName() {
  187. return name;
  188. }
  189.  
  190. public double getCost() {
  191. return cost;
  192. }
  193. }
  194.  
  195.  
  196.  
  197. package ShoppingSpree;
  198.  
  199. public class Validator {
  200.  
  201. public static boolean moneyAreValid(double amount){
  202.  
  203. if (amount < 0){
  204. throw new IllegalArgumentException("Money cannot be negative");
  205. }
  206. return true;
  207. }
  208.  
  209. public static boolean nameIsValid(String name){
  210.  
  211. if (name.trim().isEmpty() || name.trim()==null){
  212. throw new IllegalArgumentException
  213. ("Name cannot be empty");
  214. }
  215. return true;
  216. }
  217.  
  218. }
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement