1 57 58 package shop; 59 60 import javax.ejb.CreateException ; 61 import javax.ejb.EJBException ; 62 import javax.ejb.SessionBean ; 63 import javax.ejb.SessionContext ; 64 68 public class ShoppingCartBean implements SessionBean { 69 private javax.ejb.SessionContext mySessionCtx = null; 70 71 private int fieldTotal = 0; 72 73 private String fieldFirstName; 74 private String fieldLastName; 75 private Address fieldAddress; 76 private String fieldCustomerNumber; 77 78 private Order fieldOrder; 79 80 public Item addItem(String itemNumber, String itemName, int itemQuantity) 81 throws javax.ejb.EJBException , InvalidItemException, OutOfStockException { 82 Item item = new Item(); 83 84 if (itemQuantity > 10) 85 throw new OutOfStockException( 86 "There are less then '" 87 + itemQuantity 88 + "' pieces of '" 89 + itemName 90 + "' in stock"); 91 92 if ((itemNumber == null) 93 || (itemName == null) 94 || (itemNumber.equals("")) 95 || (itemName.equals(""))) 96 throw new InvalidItemException( 97 "Invalid item: Number = '" + itemNumber + "' Name = '" + itemName + "'"); 98 99 item.setNumber(itemNumber); 100 item.setName(itemName); 101 item.setQuantity(itemQuantity); 102 item.setPrice(100); 104 fieldOrder.addItem(itemNumber, item); 105 106 fieldTotal += item.getPrice(); 107 108 return item; 109 } 110 111 115 public void ejbActivate() throws javax.ejb.EJBException { 116 } 117 118 public void ejbCreate() 119 throws javax.ejb.CreateException , javax.ejb.EJBException { 120 fieldFirstName = null; 121 fieldLastName = null; 122 fieldAddress = null; 123 fieldCustomerNumber = null; 124 fieldOrder = null; 125 } 126 127 public void ejbCreate( 128 String firstName, 129 String lastName, 130 Address address, 131 String customerNumber) 132 throws javax.ejb.CreateException , javax.ejb.EJBException , shop.CreateException { 133 fieldFirstName = firstName; 134 fieldLastName = lastName; 135 fieldAddress = address; 136 fieldCustomerNumber = customerNumber; 137 138 fieldOrder = new Order(customerNumber); 139 } 140 141 145 public void ejbPassivate() throws javax.ejb.EJBException { 146 } 147 151 152 public void ejbRemove() throws javax.ejb.EJBException { 153 } 154 159 160 public void setSessionContext(javax.ejb.SessionContext ctx) 161 throws javax.ejb.EJBException { 162 mySessionCtx = ctx; 163 } 164 165 public SubmitOrderResult submitOrder( 166 CreditCardInfo creditCardInfo, 167 AirMilesContainer airMilesContainer) 168 throws javax.ejb.EJBException { 169 long orderConfirmationNumber = System.currentTimeMillis(); 170 fieldOrder.setConfirmationNumber(orderConfirmationNumber); 171 fieldOrder = null; 173 airMilesContainer.addMiles(1000); 174 175 return new SubmitOrderResult(airMilesContainer, orderConfirmationNumber); 176 } 177 178 public void emptyOrder(String customerNumber) throws javax.ejb.EJBException { 179 fieldOrder.empty(); 181 fieldTotal = 0; 182 } 183 } 184 | Popular Tags |