5kov

Shopping Spree

Jun 30th, 2023
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. //=====================================================================
  2. package ShoppingSpree;
  3.  
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Scanner;
  7. import java.util.concurrent.atomic.AtomicInteger;
  8.  
  9. public class Main {
  10. public static void main(String[] args) {
  11. Scanner scanner = new Scanner(System.in);
  12. List<Person> personList = new ArrayList<>();
  13. List<Product> productList = new ArrayList<>();
  14. String[] inputArray = scanner.nextLine().split(";");
  15. for (int i = 0; i < inputArray.length; i++) {
  16. try {
  17. String[] temp = inputArray[i].split("=");
  18. Person person = new Person(temp[0], Double.parseDouble(temp[1]));
  19. personList.add(person);
  20. } catch (Exception e) {
  21. System.out.println(e.getMessage());
  22. System.exit(0);
  23. }
  24. }
  25. inputArray = scanner.nextLine().split(";");
  26. for (int i = 0; i < inputArray.length; i++) {
  27. try {
  28. String[] temp = inputArray[i].split("=");
  29. Product product = new Product(temp[0], Double.parseDouble(temp[1]));
  30. productList.add(product);
  31. } catch (Exception e) {
  32. System.out.println(e.getMessage());
  33. System.exit(0);
  34. }
  35. }
  36. String input = scanner.nextLine();
  37. while(!input.equals("END")) {
  38. String[] inputArray2 = input.split(" ");
  39. for (int i = 0; i < personList.size(); i++) {
  40. if (personList.get(i).getName().equals(inputArray2[0])) {
  41. for (int j = 0; j < productList.size(); j++) {
  42. if (productList.get(j).getName().equals(inputArray2[1])) {
  43. Person temp = personList.get(i);
  44. temp.buyProduct(productList.get(j));
  45. personList.set(i, temp);
  46. }
  47. }
  48. }
  49. }
  50. input = scanner.nextLine();
  51. }
  52. personList.stream().forEach(e -> {
  53. System.out.print(e.getName() + " - ");
  54. if (e.getProducts().size() == 0) {
  55. System.out.print("Nothing bought");
  56. } else {
  57. AtomicInteger counter = new AtomicInteger();
  58. e.getProducts().stream().forEach(t -> {
  59. System.out.print(t.getName());
  60. if (counter.get() < e.getProducts().size() - 1) {
  61. System.out.print(", ");
  62. }
  63. counter.getAndIncrement();
  64. });
  65. }
  66. System.out.println("");
  67. });
  68. }
  69. }
  70. //=======================================================================================
  71.  
  72. package ShoppingSpree;
  73.  
  74. import java.util.ArrayList;
  75. import java.util.List;
  76.  
  77. public class Person {
  78. private String name;
  79. private double money;
  80. private List<Product> products;
  81.  
  82. public Person(String name, double money) {
  83. this.setName(name);
  84. this.setMoney(money);
  85. this.products = new ArrayList<>();
  86. }
  87.  
  88. private void setName(String name) {
  89. if (name == null || name.length() < 1 || name.trim().isEmpty()) {
  90. throw new IllegalArgumentException("Name cannot be empty");
  91. } else {
  92. this.name = name;
  93. }
  94. }
  95.  
  96. private void setMoney(double money) {
  97. if (money < 0) {
  98. throw new IllegalArgumentException("Money cannot be negative");
  99. } else {
  100. this.money = money;
  101. }
  102. }
  103.  
  104. public void buyProduct (Product product) {
  105. if (this.money >= product.getCost()) {
  106. this.products.add(product);
  107. this.money = this.money - product.getCost();
  108. System.out.printf("%s bought %s\n", this.name, product.getName());
  109. } else {
  110. System.out.printf("%s can't afford %s", this.name, product.getName());
  111. }
  112. }
  113.  
  114. public String getName() {
  115. return name;
  116. }
  117.  
  118. public List<Product> getProducts() {
  119. return products;
  120. }
  121. }
  122. //=============================================================================================
  123. package ShoppingSpree;
  124.  
  125. public class Product {
  126. private String name;
  127. private double cost;
  128.  
  129. public Product(String name, double cost) {
  130. this.name = name;
  131. this.cost = cost;
  132. }
  133.  
  134. private void setName(String name) {
  135. if (name == null || name.length() < 1 || name.trim().isEmpty()) {
  136. throw new IllegalArgumentException("Name cannot be empty");
  137. } else {
  138. this.name = name;
  139. }
  140. }
  141.  
  142. private void setCost(double cost) {
  143. if (cost < 0) {
  144. throw new IllegalArgumentException("Money cannot be negative");
  145. } else {
  146. this.cost = cost;
  147. }
  148. }
  149.  
  150. public String getName() {
  151. return name;
  152. }
  153.  
  154. public double getCost() {
  155. return cost;
  156. }
  157. }
  158.  
Advertisement
Add Comment
Please, Sign In to add comment