1 interface PriceParams { 2 public static final int CHILDRENS = 2; 3 public static final int REGULAR = 0; 4 public static final int NEW_RELEASE = 1; 5 } 6 7 class ChildrensPrice extends Price { 8 double getCharge(int daysRented) 9 { 10 double result = 1.5; 11 if(daysRented > 3) 12 result += (daysRented - 3)*1.5; 13 return result; 14 } 15 int getPriceCode() 16 { 17 return CHILDRENS; 18 } 19 } 20 21 class NewReleasePrice extends Price { 22 // NewRelease movies get 2 frequent renter points 23 int getFrequentRenterPoints(int daysRented) 24 { 25 return (daysRented > 1) ? 2 : 1; 26 } 27 double getCharge(int daysRented) 28 { 29 return daysRented * 3; 30 31 } 32 int getPriceCode() 33 { 34 return NEW_RELEASE; 35 } 36 } 37 38 class RegularPrice extends Price { 39 double getCharge(int daysRented) 40 { 41 double result = 0.0; 42 result += 2; 43 if (daysRented > 2) 44 result += (daysRented - 2) * 1.5; 45 return result; 46 } 47 48 int getPriceCode() 49 { 50 return REGULAR; 51 } 52 53 54 } 55 56 abstract class Price implements PriceParams{ 57 int getFrequentRenterPoints(int daysRented) 58 { 59 return 1; 60 } 61 abstract int getPriceCode(); 62 abstract double getCharge(int daysRented); 63 public static Price getInstance(int priceCode) 64 { 65 Price result = null; 66 switch (priceCode) { 67 case CHILDRENS: 68 result = new ChildrensPrice(); 69 break; 70 case REGULAR: 71 result = new RegularPrice(); 72 break; 73 case NEW_RELEASE: 74 result = new NewReleasePrice(); 75 break; 76 default: 77 throw new IllegalArgumentException(); 78 } 79 return result; 80 } 81 } 82