⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dvdmembershipcard.java

📁 这是个JAVA类程序
💻 JAVA
字号:
// Solution for Lab' 9.  

package mediaStore;

/**
 * The class DvdMembershipCard inherits from MembershipCard. 
 * 
 * A DVD card (object) has access to an array of films created in the active
 * MediaStore object.  This is so that transactions on a DVD card can be
 * specific to a particular DVD.  
 * 
 * @author D. M. Etheridge.  
 * @version 2.0, dated 2 December 2008.    
 */

import java.io.Serializable;

public class DvdMembershipCard extends MembershipCard implements Serializable {

    // Declare instance variables. 
    // The (test) array of DVDs available on loan to this card.  
    private Item[ ] dvds;
    // An array to store DVDs currently on loan against this card.  
    // Note: maxOnLoan is inherited from the parent.  
    private Item[ ] dvdsOnLoan = new Item[ maxOnLoan ];
    // An instance variable associated with an Item.  
    private Item dvd = null;

    /**
     * Constructor for objects of class DvdMembershipCard. 
     * 
     * @param max  The maximum number of DVDs allowed on loan against this card.  
     * @param dvds  The array of DVDs available for loan.  
     */
    public DvdMembershipCard( int max, Item[ ] dvds ) {

        // The parameter max is passed to the parent constructor. 
        super( max );
        this.dvds = dvds;

    } // End of constructor.  
    
    /**
     * This method searches the array of available DVDs for a DVD by its
     * catalogue number.  If the catalogue number doesn't exist, a suitable
     * message is output.  
     * 
     * @param catNo  A String representing the DVD's catalogue number.  
     * @return dvd  A reference to a DVD object.  
     */
    public Item findDvd( String catNo ) {
        
        // Scan the array of DVDs for the one required.  
        for ( int i = 0; i < dvds.length; i++ )
        {
            if ( ( dvds[ i ].getCatNo( ) ).equals( catNo) )
            {
                dvd = dvds[ i ];
                System.out.println( "Found DVD: " + dvd.getCatNo( ) + " " + dvd.getTitle( ) );
            } // End of if block.  
        } // End of for loop.  
        
        if ( dvd != null )
        {
            return dvd;
        } // End of if block.  
        else
        {
            System.out.println( "No such catalogue number." );
            return null;
        } // End of else block.  
        
    } // End of findDvd.      
    
    /**
     * This method returns the array of DVDs available for loan.  
     * 
     * @return dvds  A reference to the array of DVDs available for loan.  
     */    
    public Item[ ] getDvds( ) {
        
        return dvds;
        
    } // End of getDvds.  
    
    /**
     * This method takes a specific DVD on loan.  It overrides the method
     * in the parent class. 
     * 
     * @param catNo  The catalogue number of the DVD to take on loan.  
     */    
    public void takeItemOnLoan( String catNo ) throws ItemLimitException {
        
        // Find out if the dvd exists before attempting a transaction.  
        dvd = findDvd( catNo );
        if ( dvd == null )
        {
            System.out.println( "No such dvd; please try again." );
        }
        else // Carry out the transaction.  
        {
            if ( noOnLoan + 1 <= maxOnLoan )
            {
                System.out.println( "You are taking 1 DVD on loan." );
                System.out.println( "This transaction is acceptable and " +
                          "increases the number\n of DVDs that you have " +
                          "on loan by 1." );
                noOnLoan = noOnLoan + 1;
                dvdsOnLoan[ noOnLoan - 1 ] = dvd;
                System.out.println( "You are taking " + dvd.getCatNo( ) + " " + dvd.getTitle( ) + " on loan." );
                }
                else
                {
                    System.out.println( "This transaction is not acceptable " +
                          "because it exceeds the maximum number\n" +
                          "of DVDs that you are permitted to have on loan." );
                    throw new ItemLimitException( "This transaction is not acceptable " +
                          "because it exceeds the maximum number\n" +
                          "of DVDs that you are permitted to have on loan by ", 
                          ( maxOnLoan - noOnLoan + 1 ) );
            }
        }        
        
    } // End of takeItemsOnLoan.  
    
    /**
     * This method finds the position of a DVD on loan in the array of DVDs
     * on loan.  
     *  
     * @param catNo  The catalogue number of the DVD.  
     * @return position  The position of the DVD in the array of DVDs on loan.  
     */    
    public int findPosition( String catNo ) {
        
        // Declare a local variable to save the posiiton of the DVD sought for.  
        int position = 0;
        
        // Find out if the DVD exists.  
        dvd = findDvd( catNo );
        if ( dvd == null )
        {
            System.out.println( "No such dvd; please try again." );
        }
        else // Find its position.  
        {
            // Find the position of the required DVD in the array of DVDs on loan.  
            // Scan the array of DVDs on loan for the one required.  
            for ( int i = 0; i < getNoOnLoan( ); i++ )
            {
                if ( ( dvdsOnLoan[ i ].getCatNo( ) ).equals( catNo) )
                {
                    System.out.println( "The position in the array of DVDs on loac is: " + ( i + 1) );
                    position = ++i;
                } // End of if block.  
            } // End of for loop.  
            System.out.println( "The value of position is: " + position );            
        } // End of else block.  
        
        return position;
        
    } // End of findPosition.  
    
    /**
     * This method returns the array of DVDs currently on loan.  
     * 
     * @return dvdsOnLoan  The array of DVDs currently on loan.  
     */    
    public Item[ ] getDvdsOnLoan( ) {
        
        return dvdsOnLoan;
        
    } // Enf of getDvdsOnLoan.  
    
    /**
     * This method returns a DVD by removing it from the member's array of DVD
     * out on loan.  The method moves all other DVDs along the array, to the
     * left, so that no spaces are left between elements of the array but one
     * or more spaces may be left between the last DVD on loan and the end of
     * the array.  
     * 
     * @param  index  The DVD's index position in the array.  
     */
    public void returnItem( String catNo ) {

        // Find out if the DVD exists before attempting a transaction.  
        dvd = findDvd( catNo );
        if ( dvd == null )
        {
            System.out.println( "No such dvd; please try again." );
        }
        else // Carry out the return.  
        {   
            dvdsOnLoan[ findPosition( catNo ) - 1 ] = null;
            System.out.println( "Item " + catNo + " " + " has been returned." );
            noOnLoan = noOnLoan - 1;

        } // End of else block. 
        
        for ( int i = 0; i < ( dvdsOnLoan.length - 1 ); i ++ )
        {
            if ( dvdsOnLoan[ i ] == null )
            {
                for ( int j = i; j < ( dvdsOnLoan.length - 1 ); j ++ )
                {
                    dvdsOnLoan[ j ] = dvdsOnLoan[ j + 1 ];
                }
            }
            else
            {
                // Don't move any object references along.  
            }
        } // Enf of for loop.          
        
    } // End of returnItems.      
    
} // End of class definition for DvdMembershipCard.  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -