Advertisement
Tsuki11

Untitled

Jul 6th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. Person -----
  2.  
  3. private void setMoney(double money) {
  4. if (money < 0) {
  5. throw new IllegalArgumentException("Money cannot be negative");
  6. }
  7. this.money = money;
  8. }
  9.  
  10. public void buyProduct(Product product) {
  11. if (product == null) {
  12. throw new IllegalArgumentException();
  13. }
  14. if (this.money - product.getCost() < 0) {
  15. throw new IllegalArgumentException(String.format("%s can't afford %s", this.name, product.getName()));
  16. }
  17. this.products.add(product);
  18. this.setMoney(this.money - product.getCost());
  19. System.out.println(String.format("%s bought %s", this.name, product.getName()));
  20.  
  21. }
  22.  
  23.  
  24. Main-----
  25.  
  26.  
  27. try {
  28. getPeople();
  29. getProducts();
  30.  
  31. String input;
  32. while (!"end".equalsIgnoreCase(input = reader.readLine())) {
  33. String[] tokens = input.split("\\s+");
  34. String name = tokens[0];
  35. String productName = tokens[1];
  36. if (people.containsKey(name) && products.containsKey(productName)) {
  37. Person person = people.get(name);
  38. Product product = products.get(productName);
  39.  
  40. person.buyProduct(product);
  41. }
  42. }
  43. } catch (IllegalArgumentException iae) {
  44. System.out.println(iae.getMessage());
  45. } catch (IOException e) {
  46. e.printStackTrace();
  47. }
  48. people.values()
  49. .forEach(System.out::println);
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement