Advertisement
RnD

Java product code exception

RnD
Mar 6th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.03 KB | None | 0 0
  1. package electronicsequipmentdemo;
  2.  
  3. public class Product {
  4.     private String productCode;
  5.     private double pricePerUnit;
  6.    
  7.     public Product(String productCode, double pricePerUnit) throws InvalidProductCodeException{
  8.         if(productCode.length()==7){
  9.             char[] temp = (productCode.substring(0, 2)).toCharArray();
  10.            
  11.             for(char ch : temp){
  12.                 if(!Character.isLetter(ch)){
  13.                     throw new InvalidProductCodeException();
  14.                 }
  15.             }
  16.            
  17.             if(productCode.charAt(2) != '/'){
  18.                 throw new InvalidProductCodeException();
  19.             }
  20.            
  21.             temp = (productCode.substring(3, 7)).toCharArray();
  22.            
  23.             for(char ch : temp){
  24.                 if(!Character.isDigit(ch)){
  25.                     throw new InvalidProductCodeException();
  26.                 }
  27.             }
  28.            
  29.             this.productCode = productCode;
  30.             this.pricePerUnit = pricePerUnit;
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement