//
//  Controller.m
//  ATM
//
//  David J. Malan
//  Harvard University
//  dmalan@harvard.edu
//
//  Simulates an ATM (that only accepts deposits).
//

#import "Controller.h"

@implementation Controller

@synthesize account=_account;
@synthesize amount=_amount;
@synthesize balanceLabel=_balanceLabel;
@synthesize depositLabel=_depositLabel;

- (void)awakeFromNib
{
    // create account
    Account *account = [[Account alloc] init];
    self.account = account;
    [account release];
    
    // show UI
    [self show];
}

- (IBAction)clear:(id)sender
{
    // clear input
    self.amount = 0;
    [self show];
}

- (IBAction)deposit:(id)sender
{
    // deposit amount
    self.account.balance += self.amount;

    // clear input
    self.amount = 0;
    [self show];
}

- (IBAction)digit:(id)sender
{
    // append digit to amount
    UIButton *b = (UIButton *)sender;
    self.amount = self.amount * 10 + b.tag;
    [self show];
}

- (void)show
{
    // show balance
    self.balanceLabel.text = [NSString stringWithFormat:@"$%llu", self.account.balance];

    // show input
    self.depositLabel.text = [NSString stringWithFormat:@"$%llu", self.amount];
}

- (void)dealloc
{
    [_account release];
    [_balanceLabel release];
    [_depositLabel release];
    [super dealloc];
}

@end
