desislava_topuzakova

Untitled

Nov 3rd, 2021
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. package shopping_spree;
  2.  
  3. import java.util.HashMap;
  4. import java.util.LinkedHashMap;
  5. import java.util.Map;
  6. import java.util.Scanner;
  7. import java.util.stream.Collectors;
  8.  
  9. public class Main {
  10. public static void main(String[] args) {
  11. Scanner scan = new Scanner(System.in);
  12.  
  13. String[] people = scan.nextLine().split(";");
  14.  
  15. Map<String, Person> peopleInfo = new LinkedHashMap<>();
  16. Map<String, Product> productsInfo = new HashMap<>();
  17.  
  18. for (String element : people) {
  19. String[] personData = element.split("=");
  20. String name = personData[0];
  21. double money = Double.parseDouble(personData[1]);
  22.  
  23. try {
  24. Person person = new Person(name, money);
  25. peopleInfo.put(person.getName(), person);
  26. } catch (IllegalArgumentException ex) {
  27. System.out.println(ex.getMessage());
  28. return;
  29. }
  30. }
  31.  
  32. String[] products = scan.nextLine().split(";");
  33.  
  34. for (String element : products) {
  35. String[] productData = element.split("=");
  36. String name = productData[0];
  37. double cost = Double.parseDouble(productData[1]);
  38.  
  39. try {
  40. Product product = new Product(name, cost);
  41. productsInfo.put(product.getName(), product);
  42. } catch (IllegalArgumentException ex) {
  43. System.out.println(ex.getMessage());
  44. return;
  45. }
  46. }
  47.  
  48. String line = scan.nextLine();
  49.  
  50. while (!line.equals("END")) {
  51. String[] tokens = line.split("\\s+");
  52. String person = tokens[0];
  53. String product = tokens[1];
  54.  
  55. try {
  56. peopleInfo.get(person).buyProduct(productsInfo.get(product));
  57. System.out.printf("%s bought %s%n", person, product);
  58. } catch (IllegalArgumentException ex) {
  59. System.out.println(ex.getMessage());
  60. }
  61.  
  62. line = scan.nextLine();
  63. }
  64.  
  65. for (Person person : peopleInfo.values()) {
  66. System.out.print(person.getName() + " - ");
  67. if (person.getProducts().isEmpty()) {
  68. System.out.println("Nothing bought");
  69. } else {
  70. System.out.println(person.getProducts().stream()
  71. .map(Product::getName)
  72. .collect(Collectors.joining(", ")));
  73. }
  74. }
  75.  
  76. }
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment