mellowdeep

shopping spree

Jun 30th, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.73 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.         setName(name);
  68.         setMoney(money);
  69.         this.shoppingBag = new ArrayList<>();
  70.     }
  71.  
  72.     public String getName() {
  73.         return name;
  74.     }
  75.     public String setName(String name){
  76.         if (name.isEmpty() || name.contentEquals(" ")) {
  77.             throw new IllegalArgumentException("Name cannot be empty");
  78.         }
  79.         return this.name = name;
  80.     }
  81.  
  82.     public double getMoney() {
  83.         return money;
  84.     }
  85.    
  86.     public double setMoney(double money){
  87.         if (money < 0) {
  88.             throw new IllegalArgumentException("Money cannot be negative");
  89.         }
  90.         return this.money = money;
  91.     }
  92.  
  93.     public List<Product> getShoppingBag() {
  94.         return shoppingBag;
  95.     }
  96.    
  97.     public void broughtProduct(Product product){
  98.         if (product.getCoast() > this.money) {
  99.             System.out.printf("%s can't afford %s%n",this.name,product.getName());
  100.         }else{
  101.             shoppingBag.add(product);
  102.             this.money -= product.getCoast();
  103.             System.out.printf("%s bought %s%n",this.name,product.getName());
  104.         }
  105.     }
  106.  
  107.     @Override
  108.     public String toString() {
  109.         if (shoppingBag.size() > 0) {
  110.             StringBuilder sb = new StringBuilder(shoppingBag.toString());
  111.             return String.format("%s - %s", this.name,sb.toString().substring(1, sb.length()-1));
  112.         }else{
  113.             return String.format("%s - Nothing bought",this.name);
  114.         }
  115.        
  116.     }
  117.    
  118. }
  119.  
  120. class Product {
  121.  
  122.     private String name;
  123.     private double cost;
  124.  
  125.     public Product(String name, double cost) {
  126.         setName(name);
  127.         setCost(cost);
  128.     }
  129.  
  130.     public String getName() {
  131.         return name;
  132.     }
  133.    
  134.     public String setName(String name){
  135.         if (name.isEmpty() || name.contentEquals(" ")) {
  136.             throw new IllegalArgumentException("Name cannot be empty");
  137.         }
  138.         return this.name = name;
  139.     }
  140.  
  141.     public double getCoast() {
  142.         return cost;
  143.     }
  144.    
  145.     public double setCost(double cost){
  146.         if (cost < 0) {
  147.             throw new IllegalArgumentException("Money cannot be negative");
  148.         }
  149.         return this.cost  = cost;
  150.     }
  151.  
  152.     @Override
  153.     public String toString() {
  154.         return this.name;
  155.     }
  156.    
  157.    
  158. }
Advertisement
Add Comment
Please, Sign In to add comment