Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- public class Person {
- private String name;
- private double money;
- private List<Product> products;
- public Person(String name, double money) {
- this.setName(name);
- this.setMoney(money);
- this.products = new ArrayList<>();
- }
- public String getName() {
- return this.name;
- }
- private void setName(String name) {
- if (name == null || name.trim().isEmpty()) {
- throw new IllegalArgumentException("Name cannot be empty");
- }
- this.name = name;
- }
- private void setMoney(double money) {
- if (money < 0) {
- throw new IllegalArgumentException("Money cannot be negative");
- }
- this.money = money;
- }
- public void buyProduct(Product product) {
- if (this.money - product.getCost() >= 0) {
- products.add(product);
- }
- }
- }
- package shoppingSpree;
- public class Product {
- private String name;
- private double cost;
- public Product(String name, double cost) {
- this.setName(name);
- this.setCost(cost);
- }
- public String getName() {
- return this.name;
- }
- public void setName(String name) {
- if (name == null || name.trim().isEmpty()) {
- throw new IllegalArgumentException("Name cannot be empty");
- }
- this.name = name;
- }
- public double getCost() {
- return this.cost;
- }
- public void setCost(double cost) {
- if (cost < 0) {
- throw new IllegalArgumentException("Money cannot be negative");
- }
- this.cost = cost;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement