mellowdeep

Shopping spree

Jun 29th, 2016
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. package pr04ShoppingSpree;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.HashMap;
  7. import java.util.LinkedHashMap;
  8. import java.util.Map;
  9.  
  10. public class Pr04ShoppingSpree {
  11.  
  12.     public static void main(String[] args) throws IOException {
  13.  
  14.         try (BufferedReader reader = new BufferedReader(new InputStreamReader(
  15.                 System.in));) {
  16.  
  17.             Map<String, Product> priceList = new HashMap<>();
  18.             Map<String, Person> personList = new LinkedHashMap<>();
  19.             String[] firstLine = reader.readLine().trim().split(";");
  20.             for (String tokens : firstLine) {
  21.                 String[] people = tokens.split("=");
  22.                 String name = people[0];
  23.                 double money = Double.valueOf(people[1]);
  24.                 Person person = new Person(name, money);
  25.                 personList.put(name, person);
  26.             }
  27.             String[] secondLine = reader.readLine().trim().split(";");
  28.             for (String tokens : secondLine) {
  29.                 String[] products = tokens.split("=");
  30.                 String name = products[0];
  31.                 double cost = Double.valueOf(products[1]);
  32.                 Product product = new Product(name, cost);
  33.                 priceList.put(name, product);
  34.             }
  35.             String input = reader.readLine();
  36.             while (!input.equals("END")) {
  37.                 String[] params = input.split("\\s+");
  38.                 String personName = params[0];
  39.                 String productName = params[1];
  40.                 personList.get(personName).broughtProduct(
  41.                         priceList.get(productName));
  42.                 input = reader.readLine();
  43.             }
  44.  
  45.             for (String person : personList.keySet()) {
  46.                 System.out.println(personList.get(person));
  47.             }
  48.         } catch (IllegalArgumentException e) {
  49.             System.out.println(e.getMessage());
  50.         }
  51.  
  52.     }
  53.  
  54. }
  55.  
  56.  
  57. import java.util.ArrayList;
  58. import java.util.List;
  59.  
  60. class Person {
  61.  
  62.     private String name;
  63.     private double money;
  64.     private List<Product> shoppingBag;
  65.  
  66.     public Person(String name, double money) {
  67.         if (name.isEmpty() || name.contains("\\W+")) {
  68.             throw new IllegalArgumentException("Name cannot be empty");
  69.         }
  70.         if (money < 0) {
  71.             throw new IllegalArgumentException("Money cannot be negative");
  72.         }
  73.        
  74.         this.name = name;
  75.         this.money = money;
  76.         this.shoppingBag = new ArrayList<>();
  77.     }
  78.  
  79.     public String getName() {
  80.         return name;
  81.     }
  82.  
  83.     public double getMoney() {
  84.         return money;
  85.     }
  86.  
  87.     public List<Product> getShoppingBag() {
  88.         return shoppingBag;
  89.     }
  90.    
  91.     public void broughtProduct(Product product){
  92.         if (product.getCoast() > this.money) {
  93.             System.out.printf("%s can't afford %s%n",this.name,product.getName());
  94.         }else{
  95.             shoppingBag.add(product);
  96.             this.money -= product.getCoast();
  97.             System.out.printf("%s bought %s%n",this.name,product.getName());
  98.         }
  99.     }
  100.  
  101.     @Override
  102.     public String toString() {
  103.         if (shoppingBag.size() > 0) {
  104.             StringBuilder sb = new StringBuilder(shoppingBag.toString());
  105.             return String.format("%s - %s", this.name,sb.toString().substring(1, sb.length()-1));
  106.         }else{
  107.             return String.format("%s - Nothing bought",this.name);
  108.         }
  109.        
  110.     }
  111.    
  112. }
  113. class Product {
  114.  
  115.     private String name;
  116.     private double coast;
  117.  
  118.     public Product(String name, double coast) {
  119.         this.name = name;
  120.         this.coast = coast;
  121.     }
  122.  
  123.     public String getName() {
  124.         return name;
  125.     }
  126.  
  127.     public double getCoast() {
  128.         return coast;
  129.     }
  130.  
  131.     @Override
  132.     public String toString() {
  133.         return this.name;
  134.     }
  135.    
  136.    
  137. }
Advertisement
Add Comment
Please, Sign In to add comment