Advertisement
Guest User

Untitled

a guest
Jul 5th, 2016
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. package Prob04;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.*;
  7.  
  8. class Person {
  9. private String name;
  10. private int money;
  11. private List<Product> bag;
  12.  
  13. public Person(String name, int money) {
  14. this.setName(name);
  15. this.setMoney(money);
  16. this.bag = new ArrayList<>();
  17. }
  18.  
  19. public String getName() {
  20. return name;
  21. }
  22.  
  23. public void setName(String name) {
  24. if(name.isEmpty()){
  25. throw new IllegalArgumentException("Name cannot be empty");
  26. }
  27. this.name = name;
  28. }
  29.  
  30. public int getMoney() {
  31. return money;
  32. }
  33.  
  34. public void setMoney(int money) {
  35. if(money < 0){
  36. throw new IllegalArgumentException("Money cannot be negative");
  37. }
  38.  
  39. this.money = money;
  40. }
  41.  
  42. public List<Product> getBag() {
  43. return bag;
  44. }
  45.  
  46. public void setBag(Product product) {
  47. this.bag.add(product);
  48. }
  49.  
  50. @Override
  51. public String toString() {
  52. String str = String.format("%s - ", this.name);
  53. if(this.bag.size() > 0){
  54. for(Product pr : this.bag){
  55. str += pr.getName() + ", ";
  56. }
  57. str = str.substring(0, str.length() - 2);
  58. }
  59. else{
  60. str = str + "Nothing bought";
  61. }
  62. return str;
  63. }
  64. }
  65.  
  66. class Product {
  67. private String name;
  68. private int cost;
  69.  
  70. public Product(String name, int cost) {
  71. this.setName(name);
  72. this.setCost(cost);
  73. }
  74.  
  75. public String getName() {
  76. return name;
  77. }
  78.  
  79. public void setName(String name) {
  80.  
  81. if(name.isEmpty()){
  82. throw new IllegalArgumentException("Name cannot be empty");
  83. }
  84. this.name = name;
  85. }
  86.  
  87. public int getCost() {
  88. return cost;
  89. }
  90.  
  91. public void setCost(int cost) {
  92.  
  93. if(cost < 0){
  94. throw new IllegalArgumentException("Money cannot be negative");
  95. }
  96.  
  97. this.cost = cost;
  98. }
  99. }
  100.  
  101. public class Prob04Main {
  102. public static void main(String[] args) throws IOException {
  103. BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
  104. String clientsLine = scanner.readLine();
  105. String productsLine = scanner.readLine();
  106. String productAndClientLine = scanner.readLine();
  107. String[] splitClients = clientsLine.split(";");
  108. String[] splitProducts = productsLine.split(";");
  109. List<Person> clientsList = new ArrayList<>();
  110. List<Product> productsList = new ArrayList<>();
  111.  
  112. try {
  113. for (int i = 0; i < splitClients.length; i++) {
  114. String[] splitClient = splitClients[i].split("=");
  115. String name = splitClient[0];
  116. int money = Integer.parseInt(splitClient[1]);
  117. Person person = new Person(name, money);
  118. clientsList.add(person);
  119. }
  120.  
  121. for (int i = 0; i < splitProducts.length; i++) {
  122. if (!(splitClients[i].isEmpty() || splitClients[i] == null)) {
  123. String[] splitProduct = splitProducts[i].split("=");
  124. String name = splitProduct[0];
  125. int cost = Integer.parseInt(splitProduct[1]);
  126. Product product = new Product(name, cost);
  127. productsList.add(product);
  128. }
  129. }
  130. }
  131. catch(IllegalArgumentException ex){
  132. System.out.println(ex.getMessage());
  133. }
  134.  
  135. while(!productAndClientLine.equals("END")){
  136. String[] splitTheLine = productAndClientLine.split(" ");
  137. String client = splitTheLine[0];
  138. String product = splitTheLine[1];
  139.  
  140. for (Person p : clientsList){
  141. if(p.getName().equals(client)){
  142. for(Product pr : productsList){
  143. if(pr.getName().equals(product)){
  144. if(p.getMoney() >= pr.getCost()){
  145. try {
  146. p.setMoney(p.getMoney() - pr.getCost());
  147. }
  148. catch(IllegalArgumentException ex){
  149. System.out.println(ex.getMessage());
  150. }
  151. p.setBag(pr);
  152. System.out.println(p.getName() + " bought " + pr.getName());
  153. }
  154. else{
  155. System.out.println(p.getName() + " can't afford " + pr.getName());
  156. }
  157. }
  158. }
  159. }
  160. }
  161.  
  162. productAndClientLine = scanner.readLine();
  163. }
  164.  
  165. for(Person p : clientsList){
  166. System.out.println(p.toString());
  167. }
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement