Section 40.3. Generate an Interleaved Sequence


40.3. Generate an Interleaved Sequence

We now extend the generator to randomly determine when RentalItemModels are returned. That way, rentals and returns are interleaved. By the end of a test sequence, all the RentalItemModels will be returned, with none outstanding. We also calculate the cost of a rental.

Whenever a RentalModel is made, we simply add a record of it to a list. Then we randomly choose between making a new rental with a client or making a return.

The makeTables() part of the class Generate2 refines the method originally given in Listing 40.2 and continues to use some of the methods that are shown in Listing 40.2. The new version of makeTables() in class Generate2, as shown in Listing 40.5, takes a parameter to specify how many random trials to use. For each trial, a random choice is made between the possible actions.

If a rental action is chosen, a random RentalItemModel is chosen in rental(). If any of that RentalItemModel are available for rent, a rental is made of a random subset, through the call to rentalSome() of class RentalItemModel, as shown in the revised class in Listing 40.6. A table is then generated to represent that rental, using the RentalModel, which is shown in Listing 40.7. This rental is added to the list of rentals, providing a source of possible returns.

If a return action is chosen, one of the RentalModels on the returns list is selected for return in returns() in class Generate2 in Listing 40.5. The appropriate table is then generated.

Listing 40.5. Generate2.java
 public class Generate2 extends CustomRunner {     private List returns = new ArrayList();     // ...     private void makeTables(int trials) {         for (int i = 0; i < trials; i++) {             int choice = random.nextInt(100);             if (choice < 60)                rent();             else                returns();         }         while (returns.size() > 0)             returns();     }     private void rent() {         RentalItemModel rentalItem = getHireItem();         if (rentalItem.none())             return;         ClientModel client = startTransaction();         RentalModel rental = rentalItem.rentSome(client,1);         returns.add(rental);         addRow(rental.rentalRow());         addRow("pay with cash $ | "+rental.getCost());         addRow("complete transaction");     }     private ClientModel startTransaction() {         ClientModel client = getClient();         addTable("begin transaction for client | "+client.getName()+         " | staff | Bill");         return client;     }     private void returns() {         if (returns.size() == 0)             return;         ClientModel client = startTransaction();         RentalModel rental = getReturn();         addRow(rental.returnRow());         addRow("complete transaction");     }     private RentalModel getReturn() {         int index = random.nextInt(returns.size());         RentalModel rental = (RentalModel)returns.get(index);         returns.remove(index);         return rental;     }     private ClientModel getClient() {         return clients[random.nextInt(clients.length)];     } } 

Listing 40.6. RentalItemModel.java (version 2)
 public class RentalItemModel {     // ...     public RentalModel rentSome(ClientModel client, int days) {         int rentedCount = count;         if (count > 1)             rentedCount = Generate2.random.nextInt(count-1)+1;         count = count - rentedCount;         return new RentalModel(client,this,rentedCount,days);     }     public boolean none() {         return count == 0;     }     public String getName() {         return name;     } } 

Listing 40.7. RentalModel.java
 public class RentalModel {    private ClientModel client;    private RentalItemModel rentalItem;    private int count;    private int days;    public RentalModel(ClientModel client, RentalItemModel hireItem,           int count, int days) {       this.client = client;       this.rentalItem = hireItem;       this.count = count;       this.days = days;    }    public String rentalRow() {        return "rent | "+count+" | | "+rentalItem.getName()+" | for | "+               days+" days";    }    public String getCost() {        return new Money(count*days*rentalItem.getDailyRate()).toString();    }    public String returnRow() {        return "return items | "+count+" | | "+rentalItem.getName();    } } 



    Fit for Developing Software. Framework for Integrated Tests
    Fit for Developing Software: Framework for Integrated Tests
    ISBN: 0321269349
    EAN: 2147483647
    Year: 2005
    Pages: 331

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net