CartBeanLocal.java
package cart;
import java.util.List;
import javax.ejb.Local;
@Local
public interface CartBeanLocal {
public void initialize(String person) throws Exception;
public void initialize(String person, String id)
throws Exception;
public void addBook(String title);
public void removeBook(String title) throws Exception;
public List<String> getContents();
public void remove();
}
CartBean.java
package cart;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;
@Stateful
public class CartBean implements CartBeanLocal {
String customerName; String customerId;
List<String> contents;
public void initialize(String person, String id)
throws Exception { if (person == null) {
throw new Exception("Null person not allowed.");
} else {
customerName = person;
}
if ( person=="ABC" && id=="123") {
customerId = id; } else {
throw new Exception("Invalid id: " + id);
}
contents = new ArrayList<String>();
}
public void addBook(String title) {
contents.add(title);
}
public void removeBook(String title) throws Exception {
boolean result = contents.remove(title); if (result == false) {
throw new Exception(title + " not in cart.");
}
}
public List<String> getContents() {
return contents;
}
@Remove public void remove() { contents = null;
}
}
0 Comments