SHARE
TWEET

ShoppingSpree

elvirynwa Jul 10th, 2019 117 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. public class Person {
  5.     private String name;
  6.     private double money;
  7.     private List<Product> products;
  8.  
  9.     public Person(String name, double money) {
  10.         this.setName(name);
  11.         this.setMoney(money);
  12.         this.products = new ArrayList<>();
  13.     }
  14.  
  15.     public String getName() {
  16.         return this.name;
  17.     }
  18.  
  19.     private void setName(String name) {
  20.         if (name == null || name.trim().isEmpty()) {
  21.             throw new IllegalArgumentException("Name cannot be empty");
  22.         }
  23.         this.name = name;
  24.     }
  25.  
  26.     private void setMoney(double money) {
  27.         if (money < 0) {
  28.             throw new IllegalArgumentException("Money cannot be negative");
  29.         }
  30.         this.money = money;
  31.     }
  32.  
  33.     public void buyProduct(Product product) {
  34.         if (this.money - product.getCost() >= 0) {
  35.             products.add(product);
  36.         }
  37.     }
  38. }
  39.  
  40.  
  41. package shoppingSpree;
  42.  
  43. public class Product {
  44.     private String name;
  45.     private double cost;
  46.  
  47.     public Product(String name, double cost) {
  48.         this.setName(name);
  49.         this.setCost(cost);
  50.     }
  51.  
  52.     public String getName() {
  53.         return this.name;
  54.     }
  55.  
  56.     public void setName(String name) {
  57.         if (name == null || name.trim().isEmpty()) {
  58.             throw new IllegalArgumentException("Name cannot be empty");
  59.         }
  60.         this.name = name;
  61.     }
  62.  
  63.     public double getCost() {
  64.         return this.cost;
  65.     }
  66.  
  67.     public void setCost(double cost) {
  68.         if (cost < 0) {
  69.             throw new IllegalArgumentException("Money cannot be negative");
  70.         }
  71.         this.cost = cost;
  72.     }
  73.  
  74. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top