//
//  Student.m
//  StudentManager
//
//  Created by Rich on 2/8/11.
//  Copyright 2011 Swarthmore College. All rights reserved.
//

#import "Student.h"


@implementation Student

@synthesize studentName;
@synthesize gradyear;
@synthesize deptMajor;


- (NSString *)studentName
{
	if (!studentName) studentName = [[NSString alloc] init];
	return studentName;
}


#pragma mark -
#pragma mark Initializers and Memory Management

- (id)initWithName:(NSString *)aName
		 deptMajor:(NSString *)aMajor
          gradyear:(int)aYear
{
	if (self = [super init]) {
		self.studentName = aName;
		self.deptMajor = aMajor;
		self.gradyear = aYear;
	}
	
	return self;
}


- (id)init
{
	return [self initWithName:@"" deptMajor:@"" gradyear:0];
}


+ (Student *)studentWithName:(NSString *)aName
				   deptMajor:(NSString *)aMajor
					gradyear:(int)aYear
{
	Student *student = [[Student alloc] initWithName:aName deptMajor:aMajor gradyear:aYear];	
	return [student autorelease];
}



- (void)dealloc
{
	[studentName release];
	[super dealloc];
}

#pragma mark -

// compare two students
- (NSComparisonResult)compare:(Student *)otherStudent
{
	NSComparisonResult result = [self.studentName compare:otherStudent.studentName];
	if (result == NSOrderedSame) {
		result = [[NSNumber numberWithInt:self.gradyear] compare:[NSNumber numberWithInt:otherStudent.gradyear]];
		if (result == NSOrderedSame) {
			result = [self.deptMajor compare:otherStudent.deptMajor];
		}
	}
	return result;
}


- (NSString *)description
{
	return [NSString stringWithFormat:@"%@ (%d): %@", studentName, gradyear, deptMajor];
}


#pragma mark -
#pragma mark NSCoding

- (void)encodeWithCoder:(NSCoder *)aCoder
{
	//Archive each instance variable using its variable name
	[aCoder encodeObject:studentName forKey:@"studentName"];
	[aCoder encodeObject:deptMajor forKey:@"deptMajor"];
	[aCoder encodeInt:gradyear forKey:@"gradyear"];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
	[super init];
	
	self.studentName = [aDecoder decodeObjectForKey:@"studentName"];
	self.deptMajor = [aDecoder decodeObjectForKey:@"deptMajor"];
	self.gradyear = [aDecoder decodeIntForKey:@"gradyear"];
	
	return self;
}



@end
