Advertisement
Guest User

ShoppingSpree

a guest
Jun 29th, 2016
767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.80 KB | None | 0 0
  1. package p04ShoppingSpree;
  2.  
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.util.ArrayList;
  8. import java.util.LinkedHashSet;
  9.  
  10. public class ShoppingSpree {
  11.  
  12.     public static void main(String[] args) throws IOException {
  13.         try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
  14.             StringBuilder sb = new StringBuilder();
  15.             LinkedHashSet<Person> personsSet = new LinkedHashSet<>();
  16.             ArrayList<Product> productsSet = new ArrayList<>();
  17.  
  18.             String line1 = reader.readLine();
  19.             String[] people;
  20.             String line2 = reader.readLine();
  21.             String[] products;
  22.  
  23.             if (line1.contains(";")) {
  24.                 people = line1.split(";");
  25.             } else {
  26.                 people = line1.split("=");
  27.             }
  28.  
  29.             if (line2.contains(";")) {
  30.                 products = line2.split(";");
  31.             } else {
  32.                 products = line2.split("=");
  33.             }
  34.  
  35.             for (int i = 0; i < people.length; i++) {
  36.                 String name;
  37.                 int money;
  38.                 if (people[i].contains("=")) {
  39.                     String[] peopleSplit = people[i].split("=");
  40.                     name = peopleSplit[0];
  41.                     money = Integer.parseInt(peopleSplit[1]);
  42.                     personsSet.add(new Person(name, money));
  43.                 } else {
  44.                     name = people[i];
  45.                     money = Integer.parseInt(people[i + 1]);
  46.                     personsSet.add(new Person(name, money));
  47.                     break;
  48.                 }
  49.             }
  50.  
  51.             for (int i = 0; i < products.length; i++) {
  52.                 String product;
  53.                 int price;
  54.                 if (products[i].contains("=")) {
  55.                     String[] productSplit = products[i].split("=");
  56.                     product = productSplit[0];
  57.                     price = Integer.parseInt(productSplit[1]);
  58.                     productsSet.add(new Product(product, price));
  59.                 } else {
  60.                     product = products[i];
  61.                     price = Integer.parseInt(products[i + 1]);
  62.                     productsSet.add(new Product(product, price));
  63.                     break;
  64.                 }
  65.             }
  66.  
  67.             String command = reader.readLine();
  68.  
  69.             while (!command.equals("END")) {
  70.                 String[] buyProducts = command.split(" ");
  71.                 String name = buyProducts[0];
  72.                 String product = buyProducts[1];
  73.  
  74.                 for (Person person : personsSet) {
  75.                     if (person.getName().equals(name)) {
  76.                         for (Product product1 : productsSet) {
  77.                             if (product1.getProduct().equals(product)) {
  78.                                 person.addToBag(product1.getProduct(), product1.getPrice());
  79.                             }
  80.                         }
  81.                     }
  82.                 }
  83.  
  84.  
  85.                 command = reader.readLine();
  86.             }
  87.             for (Person person : personsSet) {
  88.                 person.getPurchases();
  89.             }
  90.  
  91.         } catch (IllegalArgumentException iae) {
  92.             System.out.println(iae.getMessage());
  93.         }
  94.     }
  95. }
  96.  
  97.  
  98. class Person {
  99.  
  100.     private String name;
  101.     private int money;
  102.     private ArrayList<String> bag;
  103.  
  104.  
  105.     public Person(String name, int money) {
  106.         this.setName(name);
  107.         this.setMoney(money);
  108.         this.bag = new ArrayList<>();
  109.  
  110.     }
  111.  
  112.  
  113.     public String getName() {
  114.         return this.name;
  115.     }
  116.  
  117.     public int getMoney() {
  118.         return this.money;
  119.     }
  120.  
  121.     private int bagSize() {
  122.         return this.bag.size();
  123.     }
  124.  
  125.     public void addToBag(String product, int money) {
  126.         if (this.money >= money) {
  127.             System.out.printf("%s bought %s\n", this.name, product);
  128.             this.payProduct(money);
  129.             this.bag.add(product);
  130.         } else {
  131.             System.out.printf("%s can't afford %s\n", this.name, product);
  132.         }
  133.  
  134.     }
  135.  
  136.     private void payProduct(int money) {
  137.         this.money -= money;
  138.     }
  139.  
  140.     public void getPurchases() {
  141.         if (bagSize() == 0) {
  142.             System.out.printf("%s - Nothing bought\n", this.name);
  143.         } else {
  144.             StringBuilder sb = new StringBuilder();
  145.             sb.append(String.format("%s - ", this.name));
  146.             for (String product : this.bag) {
  147.                 sb.append(String.format("%s, ", product));
  148.             }
  149.  
  150.             sb.deleteCharAt(sb.length() - 2);
  151.             System.out.println(sb.toString());
  152.         }
  153.     }
  154.  
  155.     private void setName(String name) {
  156.         if (name.length() == 0) {
  157.             throw new IllegalArgumentException("Name cannot be empty");
  158.         }
  159.         this.name = name;
  160.     }
  161.  
  162.     private void setMoney(int money) {
  163.         if (money < 0) {
  164.             throw new IllegalArgumentException("Money cannot be negative");
  165.         }
  166.         this.money = money;
  167.     }
  168.  
  169.  
  170. }
  171.  
  172.  
  173. class Product {
  174.  
  175.     private String product;
  176.     private int price;
  177.  
  178.     public Product(String product, int price) {
  179.         this.setProduct(product);
  180.         this.setPrice(price);
  181.     }
  182.  
  183.     public String getProduct() {
  184.         return this.product;
  185.     }
  186.  
  187.     public int getPrice() {
  188.         return this.price;
  189.     }
  190.  
  191.     private void setProduct(String product) {
  192.         if (product.isEmpty()) {
  193.             throw new IllegalArgumentException("Name cannot be empty");
  194.         }
  195.         this.product = product;
  196.     }
  197.  
  198.     private void setPrice(int price) {
  199.         if (price < 0) {
  200.             throw new IllegalArgumentException("Money cannot be negative");
  201.         }
  202.         this.price = price;
  203.     }
  204.  
  205.  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement