home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / EODEV.Z / Rental.m < prev    next >
Encoding:
Text File  |  1996-08-23  |  2.4 KB  |  117 lines

  1. /*
  2.         Copyright (c) 1996, NeXT Software, Inc.
  3.         All rights reserved.
  4.  
  5.         You may freely copy, distribute and reuse the code in this example.
  6.         NeXT disclaims any warranty of any kind, expressed or implied,
  7.         as to its fitness for any particular use.
  8. */
  9. #import <BusinessLogic/Rental.h>
  10. #import <BusinessLogic/Unit.h>
  11. #import <BusinessLogic/Customer.h>
  12. #import <BusinessLogic/Product.h>
  13. #import <BusinessLogic/Fee.h>
  14.  
  15. @implementation Rental
  16.  
  17. - (void)awakeFromInsertionInEditingContext:(EOEditingContext *)ctx
  18. {
  19.     if(!dateOut) {
  20.         dateOut = [[NSCalendarDate calendarDate] retain];
  21.     }
  22. }
  23.  
  24. - (NSArray *)fees
  25. {
  26.     return fees;
  27. }
  28.  
  29. - (BOOL)isOut
  30. {
  31.     return dateReturned ? NO: YES;
  32. }
  33.  
  34. - (BOOL)isOverdue
  35. {
  36.  
  37.     if( dateReturned )
  38.         return NO;
  39.  
  40.  
  41.     return ([[self dateDue] compare:[NSDate date]] == NSOrderedAscending);
  42. }
  43.  
  44. - (NSCalendarDate *)dateDue
  45. {
  46.     int checkOutLength;
  47.  
  48.     checkOutLength = [[[[unit product] rentalTerms] valueForKey:@"checkOutLength"] intValue];
  49.     return [dateOut dateByAddingYears:0 months:0 days:checkOutLength hours:0 minutes:0 seconds:0];
  50. }
  51.  
  52. - (NSString *)isOverdueString
  53. {
  54.     return [self isOverdue] ? @"Yes" : @"No";
  55. }
  56.  
  57. - (Unit *)unit
  58. {
  59.     return unit;
  60. }
  61.  
  62. - (void)feePaid
  63. {
  64.     unsigned i,c;
  65.  
  66.     if( !dateReturned )
  67.         return;
  68.  
  69.     for(i=0, c=[fees count]; i < c; i++) {
  70.         if(![[fees objectAtIndex:i] isPaid])
  71.             return;
  72.     }
  73.  
  74.     // All fees are paid, the tape is back, I can go away.
  75.     // Bye bye world !
  76.     [[self editingContext] deleteObject:self];
  77.  
  78. }
  79.  
  80. - (void)returnProduct
  81. {
  82.     [self willChange];
  83.  
  84.     if(![self isOverdue]) {
  85.         dateReturned = [[NSCalendarDate calendarDate] retain];
  86.         [self feePaid]; // To eventually delete myself
  87.     } else {
  88.         id fee;
  89.  
  90.         dateReturned = [[NSCalendarDate calendarDate] retain];
  91.         fee = [[Fee alloc] initLateFeeWithDateDue:[self dateDue] rental:self];
  92.  
  93.         [self addObject:fee toBothSidesOfRelationshipWithKey:@"fees"];
  94.         [[self editingContext] insertObject:fee];
  95.     }
  96. }
  97.  
  98. - (void)dealloc
  99. {
  100.     [dateOut autorelease];
  101.     [dateReturned autorelease];
  102.     [customer autorelease];
  103.     [fees autorelease];
  104.     [unit autorelease];
  105.     [super dealloc];
  106. }
  107.  
  108. - (NSException *)validateForSave
  109. {
  110.     NSException *error = [customer validateForSave];
  111.     return error ? error : [super validateForSave];  // pass the check on to the EOClassDesciption
  112. }
  113.  
  114. - (NSString *)description { return [self eoDescription]; }
  115.  
  116. @end
  117.