home *** CD-ROM | disk | FTP | other *** search
- /*
- Copyright (c) 1996, NeXT Software, Inc.
- All rights reserved.
-
- You may freely copy, distribute and reuse the code in this example.
- NeXT disclaims any warranty of any kind, expressed or implied,
- as to its fitness for any particular use.
- */
- #import <BusinessLogic/Rental.h>
- #import <BusinessLogic/Unit.h>
- #import <BusinessLogic/Customer.h>
- #import <BusinessLogic/Product.h>
- #import <BusinessLogic/Fee.h>
-
- @implementation Rental
-
- - (void)awakeFromInsertionInEditingContext:(EOEditingContext *)ctx
- {
- if(!dateOut) {
- dateOut = [[NSCalendarDate calendarDate] retain];
- }
- }
-
- - (NSArray *)fees
- {
- return fees;
- }
-
- - (BOOL)isOut
- {
- return dateReturned ? NO: YES;
- }
-
- - (BOOL)isOverdue
- {
-
- if( dateReturned )
- return NO;
-
-
- return ([[self dateDue] compare:[NSDate date]] == NSOrderedAscending);
- }
-
- - (NSCalendarDate *)dateDue
- {
- int checkOutLength;
-
- checkOutLength = [[[[unit product] rentalTerms] valueForKey:@"checkOutLength"] intValue];
- return [dateOut dateByAddingYears:0 months:0 days:checkOutLength hours:0 minutes:0 seconds:0];
- }
-
- - (NSString *)isOverdueString
- {
- return [self isOverdue] ? @"Yes" : @"No";
- }
-
- - (Unit *)unit
- {
- return unit;
- }
-
- - (void)feePaid
- {
- unsigned i,c;
-
- if( !dateReturned )
- return;
-
- for(i=0, c=[fees count]; i < c; i++) {
- if(![[fees objectAtIndex:i] isPaid])
- return;
- }
-
- // All fees are paid, the tape is back, I can go away.
- // Bye bye world !
- [[self editingContext] deleteObject:self];
-
- }
-
- - (void)returnProduct
- {
- [self willChange];
-
- if(![self isOverdue]) {
- dateReturned = [[NSCalendarDate calendarDate] retain];
- [self feePaid]; // To eventually delete myself
- } else {
- id fee;
-
- dateReturned = [[NSCalendarDate calendarDate] retain];
- fee = [[Fee alloc] initLateFeeWithDateDue:[self dateDue] rental:self];
-
- [self addObject:fee toBothSidesOfRelationshipWithKey:@"fees"];
- [[self editingContext] insertObject:fee];
- }
- }
-
- - (void)dealloc
- {
- [dateOut autorelease];
- [dateReturned autorelease];
- [customer autorelease];
- [fees autorelease];
- [unit autorelease];
- [super dealloc];
- }
-
- - (NSException *)validateForSave
- {
- NSException *error = [customer validateForSave];
- return error ? error : [super validateForSave]; // pass the check on to the EOClassDesciption
- }
-
- - (NSString *)description { return [self eoDescription]; }
-
- @end
-