Advertisement
Guest User

Untitled

a guest
Mar 11th, 2020
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. package E03_ShoppingSpree;
  2.  
  3. import java.util.*;
  4.  
  5. public class Main {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. String[] input = scanner.nextLine().split(";");
  9.  
  10. Map<String, Person> peopleByName = new HashMap<>();
  11. Map<String, Product> productByName = new HashMap<>();
  12.  
  13. for (int i = 0; i < input.length; i++) {
  14. String[] people = input[i].split("=");
  15. String name = people[0];
  16. double money = Double.parseDouble(people[1]);
  17.  
  18. Person person = new Person(name, money);
  19. peopleByName.putIfAbsent(name, person);
  20. }
  21. input = scanner.nextLine().split(";");
  22.  
  23. for (int i = 0; i < input.length; i++) {
  24. String[] products = input[i].split("=");
  25. String name = products[0];
  26. double cost = Double.parseDouble(products[1]);
  27. Product product = new Product(name, cost);
  28. productByName.putIfAbsent(name, product);
  29. }
  30. String line;
  31.  
  32. while (!"END".equals(line = scanner.nextLine())) {
  33. String[] tokens = line.split("\\s+");
  34. String personName = tokens[0];
  35. String productName = tokens[1];
  36.  
  37. try {
  38. peopleByName.get(personName).buyProduct(productByName.get(productName));
  39. System.out.printf("%s bought %s%n", personName, productName);
  40.  
  41. } catch (IllegalArgumentException iae) {
  42. System.out.println(iae.getMessage());
  43. }
  44. }
  45.  
  46. for (Person value : peopleByName.values()) {
  47. System.out.println(value.toString());
  48. }
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement