Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "Cup.h"
  2. #import "CupOverflowException.h"
  3. #import "CupWarningException.h"
  4. #import <Foundation/NSException.h>
  5. #import <Foundation/NSString.h>
  6.  
  7. @implementation Cup
  8. -(id) init {
  9.     self = [super init];
  10.  
  11.     if ( self ) {
  12.         [self setLevel: 0];
  13.     }
  14.  
  15.     return self;
  16. }
  17.  
  18. -(int) level {
  19.     return level;
  20. }
  21.  
  22. -(void) setLevel: (int) l {
  23.     level = l;
  24.  
  25.     if ( level > 100 ) {
  26.         // throw overflow
  27.         NSException *e = [CupOverflowException
  28.             exceptionWithName: @"CupOverflowException"
  29.             reason: @"The level is above 100"
  30.             userInfo: nil];
  31.         @throw e;
  32.     } else if ( level >= 50 ) {
  33.         // throw warning
  34.         NSException *e = [CupWarningException
  35.             exceptionWithName: @"CupWarningException"
  36.             reason: @"The level is above or at 50"
  37.             userInfo: nil];
  38.         @throw e;
  39.     } else if ( level < 0 ) {
  40.         // throw exception
  41.         NSException *e = [NSException
  42.             exceptionWithName: @"CupUnderflowException"
  43.             reason: @"The level is below 0"
  44.             userInfo: nil];
  45.         @throw e;
  46.     }
  47. }
  48.  
  49. -(void) fill {
  50.     [self setLevel: level + 10];
  51. }
  52.  
  53. -(void) empty {
  54.     [self setLevel: level - 10];
  55. }
  56.  
  57. -(void) print {
  58.     printf( "Cup level is: %i\n", level );
  59. }
  60. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement