Advertisement
Guest User

Product

a guest
Feb 25th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.82 KB | None | 0 0
  1. package shoppingSpree;
  2.  
  3. public class Product {
  4.  
  5.     private String name;
  6.     private int cost;
  7.  
  8.     public Product(String name, int cost) {
  9.         this.setName(name);
  10.         this.setCost(cost);
  11.     }
  12.  
  13.     public String getName() {
  14.         return name;
  15.     }
  16.  
  17.     public int getCost() {
  18.         return cost;
  19.     }
  20.  
  21.     @Override
  22.     public String toString() {
  23.         return this.getName();
  24.     }
  25.  
  26.     private void setName(String name) {
  27.         if (name.length() < 1 || name.charAt(0) == ' ') {
  28.             throw new IllegalArgumentException("Name cannot be empty");
  29.         }
  30.         this.name = name;
  31.     }
  32.  
  33.     private void setCost(int cost) {
  34.         if (cost < 0) {
  35.             throw new IllegalArgumentException("Money cannot be negative");
  36.         }
  37.         this.cost = cost;
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement