home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / EODEV.Z / Unit.m < prev    next >
Encoding:
Text File  |  1996-08-23  |  1.7 KB  |  72 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/Unit.h>
  10. #import <BusinessLogic/Product.h>
  11. #import <BusinessLogic/Rental.h>
  12.  
  13. @implementation Unit
  14.  
  15. - (Product *)product
  16. {
  17.     return product;
  18. }
  19.  
  20. - (NSException *)validateForSave
  21. {
  22.     // this method will be called before we are inserted or updated in the database
  23.     unsigned rentalsOut = 0;
  24.     NSEnumerator *rentalsEnumerator = [rentals objectEnumerator];
  25.     Rental *next;
  26.  
  27.     while(next = [rentalsEnumerator nextObject]) {
  28.         rentalsOut += [next isOut] ? 1 : 0;
  29.         if(rentalsOut > 1)
  30.             return [NSException validationExceptionWithFormat:@"The unit %@ of product %@ may not be rented twice", unitID, [product name]];
  31.     }
  32.  
  33.     return [super validateForSave];  // pass the check on to the EOClassDesciption
  34. }
  35.  
  36. - (BOOL)isAvailableForRent
  37. {
  38.     NSEnumerator *rentalsEnumerator = [rentals objectEnumerator];
  39.     Rental *next;
  40.  
  41.     while(next = [rentalsEnumerator nextObject])
  42.         if([next isOut]) return NO;
  43.  
  44.     return YES;
  45. }
  46.  
  47. - (NSString *)inStockString
  48. {
  49.     return [self isAvailableForRent] ? @"Yes" : @"No";
  50. }
  51.  
  52. - (void)dealloc
  53. {
  54.     [unitID release];
  55.     [notes release];
  56.     [dateAcquired release];
  57.     [product release];
  58.     [rentals release];
  59.     [super dealloc];
  60. }
  61.  
  62. - (void)awakeFromInsertionInEditingContext:(EOEditingContext *)editingContext
  63. {
  64.     // set our defaults
  65.     if(!dateAcquired) 
  66.         dateAcquired = [[NSCalendarDate calendarDate] retain];
  67. }
  68.  
  69. - description { return [self eoDescription]; }
  70.  
  71. @end
  72.