30.01.2015 Views

des enterprise beans - Département Informatique

des enterprise beans - Département Informatique

des enterprise beans - Département Informatique

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Un bean session pour faire vos courses<br />

Date today = Calendar.getInstance().getTime();<br />

if(_expirationDate.before(today)) {<br />

throw new CardExpiredException("Date d’expiration : " +<br />

_expirationDate);<br />

}<br />

// termine le processus d’achat<br />

// déclenche PurchaseProblemException si une erreur se produit<br />

}<br />

CartBean inclut une méthode toString() pour imprimer le CartBean et le nom<br />

du propriétaire de la carte.<br />

// méthode imprimant le CartBean et nom du propriétaire de la carte<br />

public String toString() {<br />

return "CartBean[name=" + _cardHolderName + "]";<br />

}<br />

Classe Item<br />

L’exemple CartBean utilise une classe Item. Item est publique et étend<br />

java.io.Serializable ; <strong>des</strong> données sérialisées peuvent être transmises sur<br />

le câble :<br />

package shoppingcart;<br />

public class Item implements java.io.Serializable {<br />

private String _title;<br />

private float _price;<br />

public Item(String title, float price) {<br />

_title = title;<br />

_price = price;<br />

}<br />

public String getTitle() {<br />

return _title;<br />

}<br />

public float getPrice() {<br />

return _price;<br />

}<br />

}<br />

Exceptions<br />

Il y a trois classes exception dans l’exemple Cart. Toutes sont <strong>des</strong><br />

extensions de la classe Java Exception :<br />

public class ItemNotFoundException extends Exception {<br />

public ItemNotFoundException(String message) {<br />

super(message);<br />

}<br />

}<br />

public class PurchaseProblemException extends Exception {<br />

public PurchaseProblemException(String message) {<br />

super(message);<br />

}<br />

D é veloppement <strong>des</strong> <strong>beans</strong> session 9-15

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!