www.pudn.com > Java_UML_library.rar > Title.java


// 
//  Unified Library Application 
//  Case study in Unified Modeling Language Toolkit 
// 
//  Title.java: represents a book or magazine title. A title 
//  can exist in many physical items and can have reservations 
//  connected to it. 
// 
//  Copyright (c) 1998 John Wiley & Sons, Inc. All rights reserved. 
//  Reproduction or translations of this work beyond that permitted 
//  in Section 117 of the 1976 United States Copyright Act without 
//  the express written permission of the copyright owner is unlawful. 
//  Requests for further information should be addressed to Permissions 
//  Department, John Wiley & Sons, Inc. The purchaser may make back-up 
//  copies for his/her own use only and not for distribution or resale. 
//  The Publisher assumes no responsibility for errors, omissions, or 
//  damages, caused by the use of these programs of from the use of the 
//  information contained herein. 
 
 
package bo; 
import util.ObjId; 
import db.*; 
import java.io.*; 
import java.util.*; 
 
public class Title extends Persistent 
{ 
    public final static int TYPE_BOOK = 1; 
    public final static int TYPE_MAGAZINE = 2; 
    private String name; 
    private String isbn; 
    private String author; 
    private int type; 
    private Vector items = new Vector(); 
    private Vector reservations = new Vector(); 
    public Title() 
    { 
    } 
    public Title(String n, String a, String isbnno, int t) 
    { 
        name = n; 
        author = a; 
        isbn = isbnno; 
        type = t; 
    } 
    public static Title findOnName(String name) 
    { 
        Title i = (Title) iterate("bo.Title",true); 
        while (i != null) 
        { 
            if (i.getTitle().toUpperCase().equals(name.toUpperCase())) 
            { 
                return i; 
            } 
            else 
                i = (Title) iterate("bo.Title",false); 
        } 
        return null; 
    } 
    public static Title findOnAuthor(String author) 
    { 
        Title i = (Title) iterate("bo.Title",true); 
        while (i != null) 
        { 
            if (i.getAuthor().toUpperCase().equals(author.toUpperCase())) 
            { 
                return i; 
            } 
            else 
                i = (Title) iterate("bo.Title",false); 
        } 
        return null; 
    } 
    public static Title findOnISBN(String isbn) 
    { 
        Title i = (Title) iterate("bo.Title",true); 
        while (i != null) 
        { 
            if (i.getISBN().toUpperCase().equals(isbn.toUpperCase())) 
            { 
                return i; 
            } 
            else 
                i = (Title) iterate("bo.Title",false); 
        } 
        return null; 
    } 
    public static Title iterateTitle(boolean init) 
    { 
        return (Title) iterate("bo.Title",init); 
    } 
    public static Title getTitle(ObjId id) 
    { 
        return (Title) Persistent.getObject(id); 
    } 
 
    public String getTitle() { return name; } 
    public String getAuthor() { return author; } 
    public String getISBN() { return isbn; } 
    public int getType() { return type; } 
    public String getTypeAsString() { 
        if (type == TYPE_BOOK) 
            return new String("Book"); 
        if (type == TYPE_MAGAZINE) 
            return new String("Magazine"); 
        return null; 
    } 
    public void setTitle(String t) { name = t; } 
    public void setAuthor(String a) { author = a; } 
    public void setISBN(String i) { isbn = i; } 
    public void setType(int t) { type = t; } 
 
    public void addItem(ObjId itemid) 
    { 
        if (items.size() <= 25) 
            items.addElement(itemid); 
    } 
    public int getNoItems() 
    { 
        return items.size(); 
    } 
    public Item getItem(int index) 
    { 
        if (index < items.size()) 
        { 
            ObjId id = (ObjId) items.elementAt(index); 
            return (Item) Persistent.getObject(id); 
        } 
        else 
            return null; 
    } 
    public void removeItemAtIndex(int index) 
    { 
        if (index < items.size()) 
        { 
            items.removeElementAt(index); 
        } 
    } 
    public int getNoReservations() 
    { 
        return reservations.size(); 
    } 
    public Reservation getReservation(int index) 
    { 
        if (index < reservations.size()) 
        { 
            ObjId oid = (ObjId) reservations.elementAt(index); 
            return (Reservation) Persistent.getObject(oid); 
        } 
        else 
            return null; 
    } 
    public void addReservation(ObjId id) 
    { 
        reservations.addElement(id); 
    } 
    public void removeReservation(ObjId id) 
    { 
        reservations.removeElement(id); 
    } 
    public Item getItemWithId(int itemid) 
    { 
        for (int i = 0; i < items.size(); i++) 
        { 
            ObjId id = (ObjId) items.elementAt(i); 
            Item it = (Item) Persistent.getObject(id); 
            if (it.getId() == itemid) 
                return it; 
        } 
        return null; 
    } 
 
    public void write(RandomAccessFile out) 
        throws IOException 
    { 
        out.writeUTF(name); 
        out.writeUTF(isbn); 
        out.writeUTF(author); 
        out.writeInt(type); 
        out.writeInt(items.size()); 
        for (int i = 0; i < items.size(); i++) 
        { 
            ObjId id = (ObjId) items.elementAt(i); 
            id.write(out); 
        } 
        out.writeInt(reservations.size()); 
        for (int i = 0; i < reservations.size(); i++) 
        { 
            ObjId id = (ObjId) reservations.elementAt(i); 
            id.write(out); 
        } 
    } 
    public void read(RandomAccessFile in) 
        throws IOException 
    { 
        name = in.readUTF(); 
        isbn = in.readUTF(); 
        author = in.readUTF(); 
        type = in.readInt(); 
        int noItems = in.readInt(); 
        items.removeAllElements(); 
        for (int i = 0; i < noItems; i++) 
        { 
            ObjId objid = new ObjId(); 
            objid.read(in); 
            items.addElement(objid); 
        } 
        int noReservations = in.readInt(); 
        reservations.removeAllElements(); 
        for (int i = 0; i < noReservations; i++) 
        { 
            ObjId objid = new ObjId(); 
            objid.read(in); 
            reservations.addElement(objid); 
        } 
    } 
 
 
}