Advertisement
Guest User

Untitled

a guest
Mar 11th, 2020
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. package E03_ShoppingSpree;
  2.  
  3. public class Product {
  4. private String name;
  5. private double cost;
  6.  
  7. public Product(String name, double cost) {
  8. this.setName(name);
  9. this.setCost(cost);
  10. }
  11.  
  12. public String getName() {
  13. return name;
  14. }
  15.  
  16. private void setName(String name) {
  17. if (name == null || name.trim().isEmpty()){
  18. throw new IllegalArgumentException("Name cannot be empty");
  19. }
  20. this.name = name.trim();
  21. }
  22.  
  23. public double getCost() {
  24. return cost;
  25. }
  26.  
  27. private void setCost(double cost) {
  28. if (this.cost < 0){
  29. throw new IllegalArgumentException("Money cannot be negative");
  30. }
  31. this.cost = cost;
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement