IDL Constructed Types   «Prev  Next»

Using structs, sequences, enums - Exercise

Java code example

Course project: Using structs, sequences, and enums


Objective: Write Java code based on IDL with structs, sequences, and enums.

Exercise scoring

You will receive 10 points for this exercise. This exercise is auto-scored; when you have completed the exercise, click the Submit button to receive full credit.

Background/overview

You will be writing the BookStore service implementation. This time the IDL has been filled out with structures and sequences.
You are given two working servers that place object references into the Naming Service upon startup. You are also given the shell of another service that is itself a client to the first two servers. You will need to modify the client program to access both servers.
All initialization of the objects is done for you. The IDL for this is:

module ECommerceModule {
 struct CreditInfo { 
  string cardnumber;
  string expdate;
  float   creditLimit;
 };
};

module BookShopModule {
 struct Book { 
  string title;
  string author;
 };
 typedef sequence<book> BookList;
 struct SaleInfo {
  Book bookTitle;
  float  discount;
  float  originalPrice;
 };
 typedef sequence<saleinfo> SaleInfoList;
 enum OrderStatus { BackOrdered ,Available ,Unavailable 
   ,Unknown };
 struct BookOrder {
  Book bk;
  long numCopies;
  float price;
  OrderStatus status;
 };
 typdef sequence<bookorder> BookOrderList;
 interface BookStoreInv {
  int getNumCopiesOfBook(in Book bk);
  boolean isBookInStock(in Book bk);
  Book getBook(in string title);
  BookList listAllBooks();
  void addCopies(in Book morecopies,in long qty);
  void removeCopies(in Book bk,in long qty);
  OrderStatus getOrderStatus(in BookOrder order);
  void satisfyOrders(inout BookOrderList books);
 };
 interface Pricer {
  attribute float standardMarkUp;
  float priceBook(in Book bk);
  void  priceOrders(inout BookOrderList order); 
  void  addSale(in SaleInfo sale);
  void  removeSale(in SaleInfo sale);
  boolean isBookOnSale(in Book bk,out float salePercentage);
  SaleInfoList listAllSales();
 };
 

 
 interface BookStore {
  readonly attribute string storeName;
  OrderStatus getBookStatus(in string title); 
  BookList listAllBooks(); 
  BookList listBooksForAuthor(in string author);
  SaleInfoList lookUpBookPrice(in BookList list);
  BookOrderList buyBooks(in BookList list,in
    ECommerceModule::CreditInfo info );
 };
};

Download files

First, download the Module4ExerciseA.zip file, and then unzip it into a work directory. It should create an exercise subdirectory and an exercise solution subdirectory.
Edit the env.bat file to reflect your local work directory.

Instructions

In this exercise, you will implement the BookStore server. Each method in the BookStore implementation uses the other two servers to get all the necessary information. Edit bookstoreserver.BookStoreImpl and fill in the following methods:
  1. String getStoreName()
  2. OrderStatus getBookStatus(String title)
  3. BookList listAllBooks()
  4. BookList listBooksForAuthor(String author)
  5. SaleInfoList lookUpBookPrice(BookList list)
  6. BookOrderList buyBooks(BookList list,CreditInfo info)

Hints

  1. Remember to run the IDL compiler on exer5.idl.
  2. Remember to run the Java compiler on all the Java code, including the code generated by the IDL compiler.
  3. Look at the classes BookShopModule.BookStoreInv and BookShopModule.Pricer to see the method signatures for all the operations. These are the operations you will have to call in your own implementation.
  4. Remember that CreditInfo is in the EcommerceModule package, so you will need to add an import statement to the class before it will compile.
  5. When using sequences, remember that they are simply creating arrays and filling the arrays. Also note that sequence elements should never be left null.
  6. Do not leave nulls in structs either, because they cause marshalling problems

Completing the exercise

When you have completed the exercise, click the Submit button.